简体   繁体   English

使用 php 和 sql 使用数据库中的值填充下拉框

[英]Populate a drop down box with values from database using php and sql

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.我的数据库中有一个名为 items 的表,其中有一个名为 sizes 的属性。 This maps to a table called sizes which lists the different sizes with different prices etc.这映射到一个名为 sizes 的表,该表列出了具有不同价格等的不同尺寸。

I simply want to create a drop down box within the items web page which lists the sizes associated with that item.我只是想在项目 web 页面中创建一个下拉框,其中列出了与该项目关联的尺寸。 How can I use php to fetch the sizes and display them in a drop down box?如何使用 php 获取尺寸并将其显示在下拉框中?

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 .我有一个 items 表,它有一个item_idsize_id的复合键,它映射到 sizes 表,它有一个主键size_id

I have tried to find information from the.net but to no avail.我试图从 .net 中查找信息,但无济于事。

Thanks谢谢

You can use HTML's SELECT and its OPTION something like:您可以使用 HTML 的SELECT及其OPTION ,例如:

<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.当然,你可以在上面的sql查询中加入一个ORDER BY...进行排序。 Then depending on your form method, you can access this by using $_POST["mySelect"] or $_GET["mySelect"]然后根据您的表单方法,您可以使用$_POST["mySelect"]$_GET["mySelect"]访问它

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..您可以编写两个查询,也可以使用 join 一次从两个表中获取值。但是为此我想知道您在 items 表和 sizes 表之间使用什么类型的映射。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM