Re: foreach loop from MySQL select query to HTML select list
| From: | Jason Wong | Date: | Thu, 28 Nov 2002 06:28:54 +0000 |
| Subject: | Re: foreach loop from MySQL select query to HTML select list | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-23713@lists.php.net to get a copy of this message | ||
On Thursday 28 November 2002 14:21, Gavin Amm wrote:
> ok,
>
> i've looked around the web a bit more & it would appear you can't just use
> a foreach function, apparently you need to use a while{foreach{}}
> structure. i was hoping you could just use the foreach function?
>
> so this is the code now:
>
> <select name="deptsub_select">
> <option>-select-</option>
> <?PHP
> while ($row = mysql_fetch_array($result_deptsub)){
> foreach ($row as $value) {
> echo "<option value=\"$value\">$value</option>\n";
> }
> }
> ?>
> </select>
>
>
> but now the output HTML code is doubling up:
>
> <select name="dept">
> <option>-select-</option>
> <option value="audit">audit</option>
> <option value="audit">audit</option>
> <option value="consulting">consulting</option>
> <option value="consulting">consulting</option>
> ...
> </select>
mysql_fetch_array() by default returns an array that's associative AND numeric
(print_r($row) to see what it looks like).
Use mysql_fetch_assoc() instead.
--
Jason Wong -> Gremlins Associates -> www.gremlins.biz
Open Source Software Systems Integrators
* Web Design & Hosting * Internet & Intranet Applications Development *
/*
"There are things that are so serious that you can only joke about them"
- Heisenberg
*/