1

I want to have a drop down box that has a list of different sizes of item. I have a table called items in my database with an attribute inside it called sizes. This maps to a table called sizes which lists the different sizes with different prices etc.

I simply want to create a drop down box within the items web page which lists the sizes associated with that item. How can I use php to fetch the sizes and display them in a drop down box?

I have got items table which has a composite key of item_id and size_id which is mapped to sizes table which has a primary key of size_id.

I have tried to find information from the net but to no avail.

Thanks

1

2 Answers 2

4

You can use HTML's SELECT and its OPTION something like:

<select name="mySelect"> 
    <?php $result= mysql_query('SELECT * FROM items'); ?> 
    <?php while($row= mysql_fetch_assoc($result)) { ?> 
        <option value="<?php echo htmlspecialchars($row['your_column_name']);?>"> 
            <?php echo htmlspecialchars($row['your_column_name']); ?> 
        </option> 
    <?php } ?> 
</select> 

Of course, you can add a ORDER BY ... to the sql query above for sorting. Then depending on your form method, you can access this by using $_POST["mySelect"] or $_GET["mySelect"]

Sign up to request clarification or add additional context in comments.

Comments

0

You can write two queries or you can use join to fetch values from both the tables at a time.. But for that I would like to know what type of mapping are you using between items table and sizes table..

1 Comment

I have got items table which has a composite key of item_id and size_id which is mapped to sizes table which has a primary key of size_id

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.