显示包含来自另一个查询中的表的多行的下拉列表

时间:2013-08-14 21:45:45

标签: php mysql mysqli

IMG

我想为每个制造商项目显示AGREEMENT DISTRIBUTOR选择字段。协议分销商应仅包含分配给客户的分销商。每个客户都有协议。在上图中,对于ID为43的客户,我们与id / GroupID为1的协议。

 agreement table
 id    GroupID
 1     1
 2     2

distribgroup table
id    groupid    custid
1     1          43

根据“cust2distrib”表

将分销商分配给客户
       cust2distrib table

id    custid    distribid
1       43          11
2       43          10
3       43          9
4       41          11

 distributors table

id        name    
9        California    
10       Lincoln       
11       Atlanta      

用于抓取正确分销商的代码(有多个分销商。我们只想查看分配给客户的分销商(参见上面的“cust2distrib”表):

SELECT *, b.id as id, e.name2 as distribname, e.id as distribid, c.groupid as groupID     from agreement b
LEFT JOIN distribgroup c
ON c.groupid = b.GroupID
LEFT JOIN cust2distrib d
ON d.custid = c.custid
LEFT JOIN distributors e
ON e.id = d.distribid
WHERE c.groupid = $id

在上图中,我们还显示协议项目(项目编号,类型,折扣等)......

agreement_items
id    groupID    item_number    item_description    din    type      discount 
1        1        111                Oranges        5                  2
2        1        222                Bananas        3     Delivered    5

...通过这个PHP代码:

<?php
$sql = "SELECT * from agreement_items WHERE GroupID = $id";
try 
    { 

        $stmt = $db->prepare($sql); 
        $stmt->execute(); 
    } 
    catch(PDOException $ex) 
    { 

        die("Failed to run query: " . $ex->getMessage()); 
    } 


    $rows = $stmt->fetchAll(); 


//Check each record from your query
foreach($rows as $row) { 

?>
HTML displaying manufacturer item, distributor item number, type, discount,
ceiling price goes here.

**I ALSO NEED "AGREEMENT DISTRIBUTOR" TO BE PLACED HERE FOR EACH ITEM. 
Agreement Distributor needs to be a select input with the option to choose
between all distributors assigned to the customer of which the agreement belongs to**
<?
}

SO ...如果加利福尼亚州,林肯,亚特兰大 已分配给客户43 ,我正在查看属于的协议1对于客户43 ,我想在每个制造商项目的“协议分销商”下拉列表中显示加利福尼亚州,林肯,亚特兰大(在agreement_items表中找到)

任何帮助都会非常感激。谢谢!

0 个答案:

没有答案