Forum Moderators: coopster

Message Too Old, No Replies

Show "selected" in select list with stored options in DB

Show "selected" in select list with stored options in DB

         

corychauvin

2:09 pm on Feb 25, 2008 (gmt 0)

10+ Year Member



Hello,
I'm trying to achieve a situation where a user can add multiple options using a multiple select list. I can loop each selected value into the Database no problem into each their own row.

My problem is that after these values have been inserted and the user needs to come back and update or change the options i would like the multiple select list to
A: show all available options from options table.
B: highlight the "selected Options" that have stored in the DB

Show this is what i'm using to get all the values into the select list

MYSQL QUERY TO GET ALL AVAILABLE OPTIONS
mysql_select_db($database_siteConn, $siteConn);
$query_rsGetfeatures = "SELECT options.optionID, options.option FROM options_group INNER JOIN options ON options_group.optionID = options.optionID WHERE options_group.option_typeID = 1 AND formOption = 1";
$rsGetfeatures = mysql_query($query_rsGetfeatures, $siteConn) or die(mysql_error());
$row_rsGetfeatures = mysql_fetch_assoc($rsGetfeatures);
$totalRows_rsGetfeatures = mysql_num_rows($rsGetfeatures);

SELECT LIST

<select name="option[]" size="15" multiple id="option[]">
<?php do { ?>
<option value="<?php echo $row_rsGetfeatures['optionID']?>"><?php echo $row_rsGetfeatures['option']?></option>
<?php
} while ($row_rsGetfeatures = mysql_fetch_assoc($rsGetfeatures));
$rows = mysql_num_rows($rsGetfeatures);
if($rows > 0) {
$row_rsGetfeatures = mysql_fetch_assoc($rsGetfeatures);
}
?>
</select>

MYSQL QUERY TO GET STORED VALUES

mysql_select_db($database_siteConn, $siteConn);
$query_rsGetStored = "SELECT options.optionID, options.option FROM options_group WHERE options_group.vehicleID = '". $_SESSION['vehicleID'] . "' AND formOption = 1";
$rsGetStored = mysql_query($query_rsGetStored, $siteConn) or die(mysql_error());
$row_rsStored = mysql_fetch_assoc($rsGetStored);
$totalRows_rsGetStored = mysql_num_rows($rsGetStored);

So basically i need to modify the select list so that it shows the stored values as "selected" or highlighted. I've tried various suggestions i've found but nothing seems to do what i need?

Thanks,
Cory

eelixduppy

11:13 pm on Feb 25, 2008 (gmt 0)



I would query the database, make an array of the options that they specifically have turned on (call it $on, for example) and then where you query the database to retrieve all of the available options, you can echo the "selected" accordingly.

For example, something like this would do, working off what you already have there:


<select name="option[]" size="15" multiple id="option[]">
<?php do { ?>
<option value="<?php echo $row_rsGetfeatures['optionID']?>"
[b]<?php echo ([url=http://www.php.net/in-array]in_array[/url]($row_rsGetfeatures['optionID'], $on))? 'selected':''; ?>[/b]>
<?php echo $row_rsGetfeatures['option']?></option>
<?php
} while ($row_rsGetfeatures = mysql_fetch_assoc($rsGetfeatures));
$rows = mysql_num_rows($rsGetfeatures);
if($rows > 0) {
$row_rsGetfeatures = mysql_fetch_assoc($rsGetfeatures);
}

Please note that your code will break for the first iteration if you do not initialize $row_rsGetfeatures before this loop because it isn't first defined until the while() part. Also, the bold part in the code above--the code that I have added--utilizes the ternary operator. For more information please follow this link: [us2.php.net...]

good luck

corychauvin

1:38 am on Feb 26, 2008 (gmt 0)

10+ Year Member



Thank-you very much! Works like a charm!