检索内部的产品ID

时间:2017-12-29 11:32:39

标签: javascript php

如何检索product_id内的modal_product_extension()

返回modal_product_extension('', 'Shoes')
提前致谢!! :)

这是我的代码:

function show_products() {
        GLOBAL $db_conn;
        $search_query="SELECT p.*, pe.* FROM tblproduct p JOIN (SELECT p.product_id, MIN(pe.product_extension_id) AS product_extension_id FROM tblproduct p LEFT JOIN tblproduct_extension pe ON pe.product_id = p.product_id GROUP BY product_id) product_unique LEFT JOIN tblproduct_extension pe ON pe.product_extension_id = product_unique.product_extension_id WHERE p.product_id = product_unique.product_id"; 
        $query = mysqli_query($db_conn, $search_query);

        while($row = mysqli_fetch_array($query)) {
            $status = ($row['product_stocks'] == 0) ? '<label class="label label-danger">Out of stocks</label>' : '<label class="label label-success">In stocks</label>';
            ?>  
                <tr>
                    <td><?=$row['product_name']?></td>
                    <td><?=$row['product_brand']?></td>
                    <td>category</td>
                    <td>&#8369;<?=number_format($row['product_price'], 2)?></td>
                    <td><?=$row['product_size']?></td>
                    <td><?=$row['product_stocks']?></td>
                    <td><?=$status?></td>
                    <td align="center">
                        <a onclick="modal_product_extension('<?=$row['product_id']?>', '<?=$row['category_name']?>')">
                            <i class="fa fa-plus fa-lg"></i>
                        </a>
                    </td>
                    <td align="center">
                        <a onclick="modal_add_image('<?=$row['product_id']?>', '<?=$row['product_brand']?>')">
                            <i class="fa fa-picture-o fa-lg"></i>
                        </a>
                    </td>
                    <td align="center"><a onclick="edit_product_by_id('<?=$row['product_id']?>')"><i class="fa fa-pencil fa-lg"></i></a></td>
                    <td align="center"><a onclick="delete_product_by_id('<?=$row['product_id']?>')"><i class="fa fa-times fa-lg"></i></a></td>
                </tr>
            <?php
        }
    }

2 个答案:

答案 0 :(得分:1)

您应该为product_id字段使用别名并引用PHP代码中的别名,因为字段名称不是唯一的(它位于两个连接的表中):

所以你的功能变成了:

function show_products() {
        GLOBAL $db_conn;
        $search_query="SELECT p.product_id as P_ID, p.*, pe.* FROM tblproduct p JOIN (SELECT p.product_id, MIN(pe.product_extension_id) AS product_extension_id FROM tblproduct p LEFT JOIN tblproduct_extension pe ON pe.product_id = p.product_id GROUP BY product_id) product_unique LEFT JOIN tblproduct_extension pe ON pe.product_extension_id = product_unique.product_extension_id WHERE p.product_id = product_unique.product_id"; 
        $query = mysqli_query($db_conn, $search_query);

        while($row = mysqli_fetch_array($query)) {
            $status = ($row['product_stocks'] == 0) ? '<label class="label label-danger">Out of stocks</label>' : '<label class="label label-success">In stocks</label>';
            ?>  
                <tr>
                    <td><?=$row['product_name']?></td>
                    <td><?=$row['product_brand']?></td>
                    <td>category</td>
                    <td>&#8369;<?=number_format($row['product_price'], 2)?></td>
                    <td><?=$row['product_size']?></td>
                    <td><?=$row['product_stocks']?></td>
                    <td><?=$status?></td>
                    <td align="center">
                        <a onclick="modal_product_extension('<?=$row['P_ID']?>', '<?=$row['category_name']?>')">
                            <i class="fa fa-plus fa-lg"></i>
                        </a>
                    </td>
                    <td align="center">
                        <a onclick="modal_add_image('<?=$row['product_id']?>', '<?=$row['product_brand']?>')">
                            <i class="fa fa-picture-o fa-lg"></i>
                        </a>
                    </td>
                    <td align="center"><a onclick="edit_product_by_id('<?=$row['product_id']?>')"><i class="fa fa-pencil fa-lg"></i></a></td>
                    <td align="center"><a onclick="delete_product_by_id('<?=$row['product_id']?>')"><i class="fa fa-times fa-lg"></i></a></td>
                </tr>
            <?php
        }
    }

答案 1 :(得分:0)

获取sql语句并运行它以确保您没有返回多个product_id,这就是关联数组未按预期工作的原因。

解决方案是为它提供一个未在同一查询中使用的别名