在循环mysql中加入内部

时间:2015-01-23 06:03:48

标签: php mysql

我有这些表格:

transactions

enter image description here

addonlist

enter image description here

addons

enter image description here

我希望echo addons上的所有项目,并查看表格addon_id中的addons是否与表格laddon_id中的addonlist相匹配给transac_id添加注释到跟踪的项目。

我有这段代码,我可以对addonlist中找到的项目进行注释,但echoes中的addons相同的项目。

    $getaddons = mysql_query("SELECT * FROM addons LIMIT 10");
    while($rows = mysql_fetch_assoc($getaddons)){
        $addonid = $rows['addon_id'];
        $addondesc = $rows['description'];
        $addonprice = $rows['price'];
        $addonstat = $rows['status'];
        $checkaddon = mysql_query("SELECT * FROM transactions t, addonlist al WHERE t.transac_id='44005' and t.transac_id=al.transac_id");
        while($rows = mysql_fetch_assoc($checkaddon)){
            $caddonid = $rows['laddon_id'];
            if(mysql_num_rows($checkaddon) and $addonid == $caddonid){
                echo "$addondesc already in your list"; // NOTE: item is already in your list
            }
        }
        echo "<strong>$addondesc </strong><button>Add to list</button>";
    }

这将显示(我的期望):

Coke 1 Litre - already in your list
Tuna Sandwich - already in your list
Hotdog Sanwich - add button
Chicken Sandwich - add button
Ham & Egg Sandwich - add button
Ham & Cheese Sandwich - add button
Grilled Cheese Burger - add button
Clubhouse Sandwich - add button
Goto - add button
Arrozcaldo - add button

但它显示的是什么:

Coke 1 Litre - already in your list
Coke 1 Litre - add button `// This wouldn't be appearing`
Tuna Sandwich - already in your list
Tuna Sandwich - add button `// This wouldn't be appearing`
Hotdog Sanwich - add button
Chicken Sandwich - add button
Ham & Egg Sandwich - add button
Ham & Cheese Sandwich - add button
Grilled Cheese Burger - add button
Clubhouse Sandwich - add button
Goto - add button
Arrozcaldo - add button

修改

如果我的database structure不好或者只是我的codes,请告诉我。

4 个答案:

答案 0 :(得分:3)

使用LEFT JOIN在一个查询中获取所有内容。对于不在addonlist中的插件,您NULL中的列会获得addonlist,您可以在循环中对其进行测试。

$getaddons = mysql_query("SELECT a.addon_id, a.description, a.price, a.status, l.laddon_id
                          FROM addons AS a
                          LEFT JOIN addonlist AS l ON a.addon_id = l.laddon_id AND l.transac_id = '44005'
                          LIMIT 10");
while ($rows = mysql_fetch_assoc($getaddons)) {
    $addonid = $rows['addon_id'];
    $addondesc = $rows['description'];
    $addonprice = $rows['price'];
    $addonstat = $rows['status'];
    if ($rows['laddon_id']) {
        echo "$addondesc already in your list";
    } else {
        echo "<strong>$addondesc </strong><button>Add to list</button>";
    }
}

由于您未使用该表中的任何内容,因此似乎无需加入transactions

答案 1 :(得分:0)

试试这个:

$getaddons = mysql_query("SELECT * FROM addons LIMIT 10");
while($rows = mysql_fetch_assoc($getaddons)){
    $addonid = $rows['addon_id'];
    $addondesc = $rows['description'];
    $addonprice = $rows['price'];
    $addonstat = $rows['status'];
    $checkaddon = mysql_query("SELECT * FROM transactions t, addonlist al WHERE t.transac_id='44005' and t.transac_id=al.transac_id");
    while($rows = mysql_fetch_assoc($checkaddon)){
        $caddonid = $rows['laddon_id'];
        if(mysql_num_rows($checkaddon) and $addonid == $caddonid){
            echo "$addondesc already in your list"; // NOTE: item is already in your list
        }else{
            echo "<strong>$addondesc </strong><button>Add to list</button>";
        }
    }
}

您的问题是您的循环在每次迭代中都添加了“添加到列表”行,而不是仅在列表中没有$ addondesc的迭代。

答案 2 :(得分:0)

您的代码中存在逻辑错误。即使插件项在列表中,也会在内部while循环完成后再次打印。

$getaddons = mysql_query("SELECT * FROM addons LIMIT 10");
while($rows = mysql_fetch_assoc($getaddons)){
    $addonid = $rows['addon_id'];
    $addondesc = $rows['description'];
    $addonprice = $rows['price'];
    $addonstat = $rows['status'];
    $checkaddon = mysql_query("SELECT * FROM transactions t, addonlist al WHERE t.transac_id='44005' and t.transac_id=al.transac_id");
    while($rows = mysql_fetch_assoc($checkaddon)){
        $caddonid = $rows['laddon_id'];
        if(mysql_num_rows($checkaddon) and $addonid == $caddonid){
            echo "$addondesc already in your list"; // NOTE: item is already in your list
        }else{
            echo "<strong>$addondesc </strong><button>Add to list</button>";
        }
    }
}

答案 3 :(得分:0)

尝试以下代码

$getaddons = mysql_query("SELECT * FROM addons a LEFT JOIN addonlist al ON a.addon_id = al.laddon_id WHERE a.status = 1");
        while($rows = mysql_fetch_assoc($getaddons)){
            if($rows['transac_id'] == "" || $rows['transac_id'] == NULL)
                $output = "<strong>{$rows['description']}</strong><button> - Add to list</button>";
            else
                $output = "<strong>{$rows['description']}</strong><button> - already in your list</button>";
            echo $output."<br/>";
        }