显示供应商提供的类别

时间:2018-04-15 13:30:08

标签: php html

我有两张桌子:tbl_supplier& tbl_categories

我想在select html

中的下拉列表php选项中显示供应商提供的所有类别

示例:

 Supplier1 > Category1, Category2, Category3

 Supplier2 > Category2, Category4

2 个答案:

答案 0 :(得分:0)

假设您的tbl_supplier包含一个sid主键,然后该键存在于tbl_categories中以跟踪供应商,而cat是保存类别名称的列,则查询将如下所示:  Select c.cat from tbl_supplier as t, tbl_categories as c where t.sid=c.sid and t.sid='supplier-1' 供应商1的示例:

<label>Supplier1:</label>
            <select name="supplier-1">
            <option>All</option>
                <?php 
                $query = mysqli_query($conn,"Select c.cat from tbl_supplier as t, tbl_categories as c where t.sid=c.sid and t.sid='supplier-1'");
                while($row=mysqli_fetch_array($query)){
                    echo "<option>".$row['cat']."</option>";
                }
                ?>
            </select>

但是,如果您想动态添加多个供应商下拉列表,那么您需要编写更复杂的查询。  希望我帮忙!

答案 1 :(得分:0)

您需要第三个表格,每个供应商/类别组合一行:

供应商

| id | name       |
|----|------------|
| 1  | category 1 |
| 2  | category 2 |
| 3  | category 3 |

分类

| id | Supplier_id | categ_id |
|----|-------------|----------|
| 1  | 1           | 1        |
| 2  | 1           | 2        |
| 3  | 2           | 2        |
| 4  | 2           | 3        |

Supplier_categories

INNER JOIN

然后,您可以通过对三个表执行SELECT Supplier.id, Category.name FROM Supplier INNER JOIN Supplier_categories on Supplier_categories.Supplier_id = Supplier.id INNER JOIN Category ON Category.id = Supplier_categories.categ_id WHERE Supplier.id = 1 //(for example) 来检索每个供应商类别的列表,并设置如下条件:

result= u.getPass().getBytes();