sql array string is not unserializing (properly, at least?) - php

I have a column in my sql table for sizes, and it's supposed to be a drop down menu that displays the sizes and then links you to a new sql row; however, nothing shows up in the options box and there's nothing displayed in the source code. This is what the code looks like:
<form>
<select onchange="window.location.href=this.form.URL.options[this.form.URL.selectedIndex].value" name="URL">
<?php
$size = unserialize($row['size']);
foreach ($size as $key => $value) {
echo "<option value='$value'>$key</option>\n";
}
?>
</select>
</form>
My arrays look as such:
"a:12:{s:3:"1/8";s:27:"legendproduct.php?id=101300";s:3:"1
/4";s:27:"legendproduct.php?id=101101";s:3:"3
/8";s:27:"legendproduct.php?id=101102";s:3:"1
/2";s:27:"legendproduct.php?id=101023";s:3:"3
/4";s:27:"legendproduct.php?id=101024";s:1:"1";
s:27:"legendproduct.php?id=101025";s:5:"1
1/4";s:27:"legendproduct.php?id=101026";s:5:"1
1/2";s:27:"legendproduct.php?id=101027";s:1:"2";
s:27:"legendproduct.php?id=101028";s:5:"2
1/2";s:27:"legendproduct.php?id=101109";s:1:"3";
s:27:"legendproduct.php?id=101110";s:1:"4";
s:27:"legendproduct.php?id=101111";}"
What am I doing wrong? Any help would be appreciated. Thanks.

Related

Distinguishing multiple items in a select multiple with PHP

I have a form on my website where one can select multiple other editors for a post.
The list of qualified editors is echoed by PHP, which works. But when multiple are selected HTML makes it into one list of the ids. F.E. If Jay (id = 4) and Sam (id = 9) are selected. The received value will be $_POST['editors'] = 49.
My code:
<select multiple class="editors" name="editors" id="editors">
<?php
//Gebruikers ophalen
$editorArr = getEditors();
foreach($editorArr as $editor){
echo "<option value='".$editor['id']."'>".$editor['email'].' - '.$editor['type']."</option>";
}
?>
</select>
and the handling PHP
$editors = htmlentities($_POST['editors']);
Okay! Try this:
<select class="editors" name="editors[]" id="editors" multiple>
<?php
//Gebruikers ophalen
$editorArr = getEditors();
foreach ($editorArr as $editor) {
echo "<option value='".$editor['id']."'>".$editor['email'].' - '.$editor['type']."</option>";
}
?>
</select>
The name should be an array so it can treat all selected values separately.
Hope it helps

input (radio) value consists of ~2000 bytes, is there a better way?

I have a MySQL database that stores items (goods and services), and along with each item, terms (terms and conditions) will also be stored, which can be roughly 2000 bytes.
http://i.imgur.com/7t3cvSE.png
This is my basic set up
$term_options = array();
$term_options[] = "None";
$term_options[] = "massive string containing 2000 bytes or more";
...
foreach ($term_options as $term) {
?>
<label class="term">
<input type="radio" name="terms" value="<?php echo $term; ?>">
<?php echo $term; ?>
</label>
<?php
}
The above code does exactly what I want, but it feels wrong to have a massive value within a radio input (which may contain Unicode characters). Am I wrong to worry?
Before, I used a SELECT menu with option values equal to the index position of each array item:
?>
<select name="terms">
<?php
$i = 0;
for ($i = 0; $i < count($term_options); $i++) {
?>
<option><?php echo $i; ?></option>
<?php
}
?>
</select>
<?php
Then my $_POST would look something like this:
$terms = $term_options[$_POST['terms']];
It worked nicely until it came to my update form which should display the currently selected values. I wasn't sure how to compare the isset value with something generated via array.
It's simple to do this with static values, e.g.:
<option <?php if (isset($row['x']) && $row['x'] === 'x') echo 'selected'; ?>>x</option>
but while creating each option in a foreach loop, I have no idea what to do and the below doesn't work:
<option <?php if (isset($row['x']) && $row['x'] === $term_options[$i]) echo 'selected'; ?>><?php echo $i; ?></option>
$i++;
Yes, you are right in your worries. If this page is going to display many of these items and each item has a massive string like this, the final page is going to be huge. An this page will be sent by your server to the client browser. Bigger the page, bigger the time.
You are retrieving these items from a MySQL database. Each item probably has a primary key. Why don't you just use this primary key as part of the name of the radio buttons and then you may retrieve again just the selected item?
Something like
<input type="radio" name="rb_1">
where this "1" appended to "rb_" is the key of an item. Then you just need to break this string, recover the primary key and search if to recover the huge string.
This may generate another database operation, of course. But this will be nothing when compared with the transmission of a huge page with, say, 100 times 2k items.

submit all users values from form into db

This gives lists all people in the db and give a drop down for each one of them i want to make it so when i hit one submit button it enters individual values for each person.
so if you make yes for bobby no for mark and yes for dustin you can the pres submit and it will enter that for there values
$results = mysql_query("SELECT * FROM `$tabeluser` WHERE buss='$buss' ORDER BY id DESC LIMIT 1");
while($rows = mysql_fetch_array($results) )
{
fn = $_POST['firstname'];
echo $fn;
?>
<form>
<select name="check">
<option>no</option>
<option>yes</option>
</select>
<?php
<input type="submit" name="submit">
?>
<form>
<?php
}
mysql_query("INSERT INTO `$fn` (buss) VALUES ('$_POST[check]')");
First of all, you create a <form> and a submit button for each of the records you have. That is wrong, since you want to update multiple values at once.
What it should look like would be:
<form>
<?php
while($rows = mysql_fetch_array($results)) {
print '<select name="check[]"> .. </select>';
}
?>
<input type="submit" name="submit" />
</form>
Secondly, your code is formatted as if you are expecting to get $_POST[check] right after sending the code to the browser. That is not how PHP works. You need to separate the logic of having posted values, before printing the actual page contents. PHP is server side, which means that it won't get any data, unless the script is called with it from the beginning. So, that should look something like:
<?php
if (isset($_POST["check"])) {
// handle posted data.
}
else {
// show form
}
// or show form here, without an else, if you want to always show a form,
// even when you have posted values.
?>
Last but not least, you need to find a way to know which posted value belongs to each of your records. The way you have it now (<select name="check">') it will just return you one single value.
If you write it the way I intentionally did above (`) you will get all values, but still you won't be able to easily recognize which value is for each record.
Instead, you may want to do a final result of something like:
<?php
// get mysql records into an array (say $my_array)
if (isset($_POST["submit"])) {
foreach($my_array as $record) {
if (isset($_POST["select_of_id_".$record["id"])) {
// insert additional value into db
}
}
}
print '<form>';
foreach($my_array as $record) {
print '<select name="select_of_id_'.$record["id"].'">';
print '<option value="0">no</option><option value="1">yes</option>';
print '</select>';
}
print '<input type="submit" name="submit"/>';
print '</form>';
?>
Changes required in your code :-
<select name="check[]">
<option value="<?php echo $userId;?>_0">no</option>
<option value="<?php echo $userId;?>_1">yes</option>
</select>
You should make changes in you DB It help to easy maintaing your data.
Create new table where you can save multiple user check data with respective Post
for e.g post_id user_id check
101 111 0
101 112 1
How you can store data from you html
In you post array you will get check array in which you will get multiple check value like this
101_0 if no selected and 102_1 if yes selected. Now you need to explode this value.
$_POST['check'] = array(0 => `101_0`, 1 => 102_1);
Inside loop you can extract userid and respective checked value.
for e.g
$checked = explode('_','101_0');
$userId = $checked[0];
$check = $checked[1];

Using in_array in multidimensional array

Ill just explain by showing my code:
if($_POST)
{
for ($records = 1; $records <= $_POST['numberofrecords']; $records++)
{
if((!in_array($_POST['user'][$records], $assigned_users, true))||($_POST['user'][$records]==''))
{
$phonePost['user'] = $_POST['user'][$records];
$phonePost['id'] = $_POST['id'][$records];
$this->autoprov_model->update_phone_user($phonePost);
}
else
{
//other actions.....
}
etc....
$assigned_users is a query listing all IDs currently selected.
the relevant html is
<select name=user[<?=$lines;?>] style="position: relative; right: 120px;" onchange="submitform(this)">
<?php if($phone['user_id']=='')echo '<option value="">Unassigned</option>'?>
<?php foreach ($users_list as $user):?>
<?php if($user['id']==$phone['user'])$selected = 'selected="selected"'; else $selected = '';?>
<option value="<?=$user['id'];?>" <?=$selected?>><?=$user['name'];?></option>
<?php endforeach;?>
</select>
Whats happening is that I am posting all kinds of IDs (relevant to the $assigned_user array) but not actually in the array. and also when I post '' (blank) they never get updated and only reach the second section.
I ask here incase I am missing a trick with posting the values as arrays?
you should put name in quotes in your select tag. Like this: name="user[<?=$lines;?>]".
$assigned_users should be an array.
check existence with isset($_POST['user'][$records]) instead of comparing it with empty string, or simply try this: if((#in_array($_POST['user'][$records], $assigned_users, true)==false){.

post values from a multiselect list to a mysql query using php and jquery

Hi guys I am trying to post values which is getting number from another text box for MySQL select query but i am stuck can u please help me here is my code when I try to get result I cannot add comma(,) between values. also tried implode() and explode() function but the result only got number of array element please help me. I will be glad to try your ideas thanks.
on my sql query i get only row as a result which is my first select
thanks a lot for your help again guys
function exportselectionlist(){
var qcolumns=document.getElementById('selectionlist');
for (i=0; i < qcolumns.length; i++) {
qcolumns.options[i].selected = true;
}
document.selectionlist_form.submit();
}
<form id="selectionlist_form" action="xxx.php" method="post"
name="selectionlist_form">
<select id="selectionlist" style="width:300px;" multiple="multiple" size="4"
name="selectionlist[]">
<option value=""></option>
<option value=""></option>
<option value=""></option>
<option value=""></option>
</select>
<input type="submit" value="x" />
<a onclick="exportselectionlist()" href="javascript:;">Export</a>
</form>
//xxx.php
<?php foreach ($selectionlist as $value) {
$resultstr = array();
foreach ($selectionlist as $result)
$resultstr[] = $result;
echo $x=implode(",",$resultstr);
sql = mysql_query("SELECT * FROM table where idArticle in ('$x')");
Try changing your js function to:
function exportselectionlist() {
var qcolumns = document.getElementById('selectionlist');
for (i=0; i < qcolumns.length; i++) {
qcolumns.options[i].selected = true;
}
document.selectionlist_form.submit();
}
and then your "xxx.php" to:
$selectionlist = $_POST['selectionlist'];
echo implode(',', $selectionlist);
As a side note, your php code indicates to me that you have register_globals turned on? I would recommend turning that off in favor of creating the variable you need from the $_POST superglobal.

Categories