View Single Post
Old Apr 7th, 2008, 4:53 PM   #1
Abyss
Newbie
 
Join Date: Apr 2005
Posts: 18
Rep Power: 0 Abyss is on a distinguished road
Iterate Drop down boxes?

Hello all! I'm currently writing some code for an online query system. From my page, users are able to select field names from mysql drop down boxes and generally structure the query in plain english. So for example, when they select 'greater than' in a text box I have the value set to '>'.

At present, I've had to duplicate the code for the drop down boxes many times. This makes the code look more complicated than what it is and generally untidy. I was wondering if there is a way to use a loop to generate 5 drop box boxes at once, giving a unique name to each such as Col1, Col2 etc.

I've found the task more difficult since the fields in the drop down boxes are being retrieved from a mySQL database.

Ideally, I would have a button which could generate new drop down boxes at the user's discretion.

Here is the code I'm using to create to drop down menus at present.

<select name="Col1">

<?php
$i = 0;
while ($i < mysql_num_fields($result)){
  $fieldname = mysql_field_name($result, $i);
  echo '<option value="'.$fieldname.'">'.$fieldname.'</option>';
  $i++;
  }
?>
</select><p>

Suggestions/help would be appreciated as always!
Abyss is offline   Reply With Quote