[thelist] PHP populate dropdowns.

Tony Light TonyLight at blueyonder.co.uk
Fri Nov 29 12:52:01 CST 2002


--
[ Picked text/plain from multipart/alternative ]
Jack Guest  recently wrote:

> I'm doing a quick questionnaire using PHP/MYSQL.

> 5 of the questions use the same drop down menu which is simply a list of names in a table.

> How can I be most efficient in executing the query to get the names out and use them in the five questions? Here's the code i've got. This doesnt work because for each question I need to define a new $row variable - e.g $rowa, $rowb. Any ideas?


Jack - hopefully keeping within your style of coding, I suggest you populate an array from the database, then use the array to populate your drop downs.  The following should work...

<? $result = mysql_query("SELECT name FROM colleagues");
while ($row=mysql_fetch_row($result)){
$colleague[]=$row[0]; }
?>

<form name="form1" method="post" action="">
1) Who ?
<select name="colleagues1" size="1">
<? while (list($k,$v)=each($colleague)){?>
<option value="<?echo $v;?>"><?echo $v;?></option>
<? }?></select>

2) Where ?
<select name="colleagues2" size="1">
<? reset($colleague);
while (list($k,$v)=each($colleague)){?>
<option value="<?echo $v;?>"><?echo $v;?></option>
<? }?></select>

3) When ?
<select name="colleagues3" size="1">
<? reset($colleague);
while (list($k,$v)=each($colleague)){?>
<option value="<?echo $v;?>"><?echo $v;?></option>
<? }?></select>
</form>

There is obvious repetition here.  I use a class to output form selects - you can give it either a sql query or an array (plus a few other parameters such as the name, class, id, and the value you want selected, etc).  If anyone is interested, I will post it.

Regards,
    Tony.
--




More information about the thelist mailing list