I’m trying to combine 2 things… creating drop list for SIZE from mysql table (this works great - code below)… second, once size has been selected and posted into db, i’d like it to display that selected option in the drop down / with all the other options still there if the user needs to change it. Here’s the code I have so far.
generate a list of drop downs for SIZE from db - this works great.
$roster=mysql_query("SELECT * FROM table WHERE id='".$_SESSION['id']."'");
while($roster_row=mysql_fetch_assoc($roster)){
echo '<td><select name="p_size[]"><option value="dv" >Select Jersey Size</option>';
$sql = "SELECT * FROM size";
$result = mysql_query($sql);
if (!$result) {
die(mysql_error());
}
$p_size = array();
while (false !== ($sizes = mysql_fetch_assoc($result))) {
$p_size[$sizes['id']] = $sizes['size'];
}
foreach ($p_size as $key => $value) {
echo '<option name="p_size['.$key.']" value="'.$value.'"';
// if statement here
echo '>';
echo "$value</option>";
$p_size=$_POST['p_size'];
}
echo '</select>';
echo '</td></tr>';
}
*** this works like a charm
After the form is submitted p_size updates correctly in table - however what i’d like to do is be able to have the size that was registered in db be shown as selected when the drop down list is re-displayed…
I have used this IF statement when i was just echoing the selected size after a POST - and it worked great, but now I need it to echo selected if it is registered in the db.
if (isset ($_POST['p_size']) && in_array ($value, $_POST['p_size'])) echo ' selected';
one way to do it is (assuming you don’t want to use AJAX)
add an onchange on the <select> which calls a function that sends the selected option to the same page in a query string appended to the url.
at the top of the web page you get php to check if a parameter has been sent to the page.
if it has you then add the parameter (the selected option) to the database.
you then retrieve the options from the database which will now include the sent option.
you then loop through the retrieved options to populate the <select> and as you do, if the current option = the sent parameter you set its selected attribute to “selected”
after you have looped through the options, you then unset() the parameter.
$roster = mysql_query("SELECT * FROM table WHERE id='".$_SESSION['id']."'") or die(mysql_error());
// if no record found
if(count($roster) == 0) die('No roster found!');
// i assume the `id` field is unique then only one row should be selected with id value stored in the session
// so no need to loop on it
$roster_row = mysql_fetch_assoc($roster);
echo '<td><select name="p_size[]"><option value="dv" >Select Jersey Size</option>';
$sql = "SELECT * FROM size";
$result = mysql_query($sql) or die(mysql_error());
if($roster_row && count($result) >= 1){
$p_size = array();
while (false !== ($sizes = mysql_fetch_assoc($result))){
$selected = ($roster_row['size'] == $value) ? ' selected=""' : '';
echo '<option value="'.$sizes['id'].'"' . $selected . '>' . $sizes['size'] . '</option>';
}
}
echo '</select>';
echo '</td></tr>';