RE: [PHP-DB] foreach loop from MySQL select query to HTML select list

From: Date: Fri, 29 Nov 2002 05:08:07 +0000
Subject: RE: [PHP-DB] foreach loop from MySQL select query to HTML select list
References: 1  Groups: php.db 
Request: Send a blank email to php-db+get-23730@lists.php.net to get a copy of this message
> hmm, interesting to know the mysql_fetch_array() by default returns an > array > that's associative AND numeric - very useful to know & something i'll have > to look into more, thanks Jason. > > Thanks also John, puting curley braces around the variables is a valuable > lesson - works wonderfully - cheers. > > i do have one more problem though, i need to run the loop a 2nd time later > in the page in exactly the same way, but nothing is returned. > i just copied & pasted the code from one section of the page to the next. > do i need to reset one of the vaiables? > > > <select name="dept"> > <option>-select-</option> > <?PHP > while($row = mysql_fetch_array($result_dept)){ > echo "<option > value=\"{$row['dept']}\">{$row['dept']}</option>\n"; > } > ?> > </select> Put the options into a variable and just echo it out where you need it. $options = "<option>-select-</option>\n"; while($row = mysql_fetch_row($result)) { $options .= "<option value=\"{$row[0]}\">{$row[0]}</option>\n"; } And wherever you need a select statement, just <select name="dept"> <?=$options?> ---John Holmes...

« previous php.db (#23730) next »