I have:
$selectedItem = '100,200,300';
And mysql load:
<select multiple="multiple" name="product_id">
<?php
$query = mysql_query("SELECT * FROM products ORDER BY id");
while($row = mysql_fetch_array($query)){
echo '<option value="$row[id]">$name</option>';
}
?>
</select>
And I need $selectedItem (three values) to be:
<option value="$row[id]" selected>$row[id]</option>
How can I do that? I just can’t find the solution when the selectedItem more than 1.
You should make your
$selectedItemvariable an array (and name it plural since it can have multiple values; just my added preference =P):Doing this, you’ll be able to check if the current row’s
idis in the array or not (within_array()) and then “select” that row:Alternatively, if you can’t “define” your list of selected items as an array and you will only have a comma-separated string, you can use
explode()to turn it into an array:This will split the
$selectedItemvariable by commas into an array stored into$selectedItems(usable with the above code).