Re: foreach loop from MySQL select query to HTML select list

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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
*/


-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux