Re: Populating multi-select list from mysql
| From: | Jason Wong | Date: | Fri, 21 Jun 2002 14:53:13 +0000 |
| Subject: | Re: Populating multi-select list from mysql | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-19957@lists.php.net to get a copy of this message | ||
On Friday 21 June 2002 10:31, bob wrote:
> Hi all,
>
> I'm having problems getting my multi-select list populated from a mysql
> table. There is a table called categories with 2 columns (id and category).
> I want to get all the items (category) and list them in the multi-select
> list box.
>
> This is the code I have so far:
>
> $sql = "SELECT category FROM categories";
>
> $result = mysql_query($sql, $connection);
>
> while ($row = mysql_fetch_array($result)) {
> $id = $row['id'];
> $category = $row['category'];
> $category_list .= "$category";
> }
> ?>
Here your $category_list will be a string like 'Category1Category2...', which
is probably not what you want.
>
> <select name=category_list_array[] multiple size=10>
> <?
> for ($i=0; $i<count($category_list); $i++) {
> ?>
> <option
> value=<?=$category_list[$i]?>><?=$category_list[$i]?></option>
>
> <?}?>
> </select>
>
>
> Now, if I echo $category_list to the page I can see I have the correct
> data, I just can't get it into the list box. I've seem to hit a wall on any
> different ways to try and make this work. What am I doing wrong?
Instead of doing two loops (while and for) just have a single while loop,
build your <option value...> inside the while loop.
--
Jason Wong -> Gremlins Associates -> www.gremlins.com.hk
Open Source Software Systems Integrators
* Web Design & Hosting * Internet & Intranet Applications Development *
/*
NOWPRINT. NOWPRINT. Clemclone, back to the shadows again.
- The Firesign Theater
*/