PHP: How to show data from database in a listbox

Hi everyone,

Firstly, I’m new to PHP.

I’m making an online store (backend and frontend) and I’m stucked in a probably simple detail. I have the products insert page done and working, and now I need to insert a product in a specific category and that’s my problem.

I have a lisbox where I want to show a list with all my categories. How do I show a category from a database in a listbox ?

Thanks in advance.

⊙ ⊙ say:good…[url=http://www.portrait2000.com]portarit2000

Hmmm. How did I miss this question?

Anyways, I’m assuming you have all the “items” in you table.
I’ll give you pseudo code, as I’m not exactly sure what your set up is.


do db connect, table select, and run the query to select items from the table, and get a $result resource.

echo "<select>";
for each item in your $result,
fetch item
and echo "<option>" . $item["itemName"] . "</option>";
end for
echo "</select>";

Hope this helps. :slight_smile:

Thanks a lot kdd. I’m going to try it now.

Cheers.

It works :slight_smile:

ThankS:D

Don’t forget you can always add the items underlying id or something as the ‘value’ attribute of each option - just modify the line to the following


and echo "<option value='" . $item["itemID"] . "'>" . $item["itemName"] . "</option>";

^ Yeah, I left that part out as an exercise to the reader. :pleased: haha