posted June 21, 2000 06:07 PM
Hi,
I've got a multiple select drop down box that is populated by PHP with values from a mysql db. Now when I am editing records, the PHP shows which values have already been selected, now what I need to do is to be able to change these values but I can't!I can add values to it but can't get rid of them.
Does anyone have a suggestion on this?
Here is the code I'm using:
________select statement________
<select name="cid[]" size="5" multiple>
<?php
$result_selected = mysql_query("select f.rid,f.cid,c.id,c.title from foodlookup f,category c where f.rid=$rid and c.id=f.cid;",$db);
$myrow = mysql_fetch_array($result_selected);
do {
printf("\t<option value=\"%s\" selected>%s\n",$myrow["id"],$myrow["title"]);
} while ($myrow = mysql_fetch_array($result_selected));
?>
<?php
//query for displaying your user information.
$result_master = mysql_query("select title,id, COUNT(rid) AS count FROM category LEFT JOIN foodlookup ON cid=id and rid=$rid GROUP BY id having count=0;",$db);
$myrow = mysql_fetch_array($result_master);
do{
printf("\t<option value=\"%s\">%s\n",$myrow["id"],$myrow["title"]);
} while ($myrow = mysql_fetch_array($result_master));
?>
</select>
_______ends________
I populate the select statement in two parts - one to find the selected values, and then the second part populates the ones that have not been selected. It may look a little cryptic but I was the only way I could get it to work!
_______submit_______
for ($i=0;$i<count($cid);$i++) {
$sql2 = "INSERT INTO FoodLookup (rid,cid) VALUES ('$rid','$cid[$i]')";
$result2 = mysql_query($sql2);
}
_______end_______
this submit to the database command runs through the values submitted (as it is a multiple select) and inserting them all - it works fine.
Any suggestions on this would be much appreciated
Dr Gonzo