Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   PHP (http://www.programmingforums.org/forum29.html)
-   -   Iterate Drop down boxes? (http://www.programmingforums.org/showthread.php?t=15566)

Abyss Apr 7th, 2008 4:53 PM

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!


All times are GMT -5. The time now is 4:56 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC