嵌套while循环只运行一次

时间:2016-08-30 16:56:44

标签: php html database mysqli while-loop

嵌套“”循环只运行一次,只显示一个 div,找不到任何错误, 0 错误“的 error_log中”。随意建议。

我需要第二个sql连接,因为 $ connectToStore 使mysqli连接到单独的数据库。

$项目看起来像1-2,2-5,9-1

我已经检查并且显示的div使用了表格第一行中的数据

PHP

include("connect.php");

$sql = "SELECT * FROM sp_tickets WHERE user_id='foo'";
$sqlresult = mysqli_query($connect, $sql);
if (mysqli_num_rows($sqlresult) > 0) {
    while ($row = mysqli_fetch_array($sqlresult)) {

    $sqlStore = "SELECT * FROM transactions WHERE order_id='".$row['order_id']."'";
    $connectToStore = mysqli_connect("localhost", "root", "root", "root_db");
    $sqlStoreDBResult = mysqli_query($connectToStore, $sqlStore);

    while ($rowTr = mysqli_fetch_array($sqlStoreDBResult)) {

        $id_str_array = $rowTr['items'];
        $id_str_array = rtrim($id_str_array, ",");
        $id_str_array = explode(',', $id_str_array);
        foreach ($id_str_array as $key => $value) {
            $id_quantity_pair = explode("-", $value); // Uses Hyphen(-) as delimiter to separate product ID from its quantity
            $product_id = $id_quantity_pair[0]; // Get the product ID

            $sqlProduct = "SELECT * FROM products WHERE id='$product_id'";
            $sqlresult = mysqli_query($connectToStore, $sqlProduct);
            while ($pp_row = mysqli_fetch_array($sqlresult)) {

                $tickets .= '<div class="holder">
                <div data-ticket="' . $ticket . '" class="ticket">
                    <div class="ticket-left">
                        <span class="product-ticket left-span">' . $pp_row['product_name'] . ' ' . $pp_row['product_platform'] . ' ' . $pp_row['product_type'] . ' ' . $pp_row['product_region'] . '</span>
                        <span class="ticket-no left-span">Ticket No. ' . $row['ticket_id'] . '</span>
                        <span class="order-no left-span">Order No. ' . $row['order_id'] . '</span>
                        <span class="left-span">Transaction No. ' . $rowTr['txn_id'] . '</span>
                        <span class="product-region left-span">Region: ' . $pp_row['product_region'] . '</span>
                    </div>
                    <div class="ticket-right">
                        <span class="created">Created: ' . $row['date'] . '</span>
                    </div>
                    <div class="ticket-bottom">
                        <div class="bottom-holder">
                            <span class="details"><span class="subject-txt">Category: </span>' . $row['category'] . '</span>
                            <span class="details"><span class="subject-txt">Subject: </span>' . $row['subject'] . '</span>
                            <span class="details desc">"' . $row['description'] . '"</span>
                        </div>
                    </div>
                </div>
                <div class="removeticketcontainer">
                    <span class="removeticket">X</span>
                </div>
             </div>';
                }
            }
        }
    }
}

2 个答案:

答案 0 :(得分:0)

看起来你的最外层只获取一条记录

 while ($row = mysqli_fetch_array($sqlresult)) {
 . . . . 
}

如果你只获取一条记录,那么摆脱最外面的......

$row = mysqli_fetch_array($sqlresult);// remove closing } too 

答案 1 :(得分:0)

解决了它。答案属于 @ckimbrell 。 &#34;问题看起来像是用一个只返回1行的新结果覆盖变量$ sqlresult。尝试在嵌套循环中更改它。&#34;

include("connect.php");

$sql = "SELECT * FROM sp_tickets WHERE user_id='foo'";
$sqlresult = mysqli_query($connect, $sql);

$connectToStore = mysqli_connect("localhost", "root", "root", "root_db");

if (mysqli_num_rows($sqlresult) > 0) {
while ($row = mysqli_fetch_array($sqlresult)) {

$sqlStore = "SELECT * FROM transactions WHERE order_id='".$row['order_id']."'";
$sqlStoreDBResult = mysqli_query($connectToStore, $sqlStore);

while ($rowTr = mysqli_fetch_array($sqlStoreDBResult)) {

    $id_str_array = $rowTr['items'];
    $id_str_array = rtrim($id_str_array, ",");
    $id_str_array = explode(',', $id_str_array);
    foreach ($id_str_array as $key => $value) {
        $id_quantity_pair = explode("-", $value); // Uses Hyphen(-) as delimiter to separate product ID from its quantity
        $product_id = $id_quantity_pair[0]; // Get the product ID

        $sqlProduct = "SELECT * FROM products WHERE id='$product_id'";
        $sqlresult2 = mysqli_query($connectToStore, $sqlProduct);
        while ($pp_row = mysqli_fetch_array($sqlresult2)) {

            $tickets .= '<div class="holder">
            <div data-ticket="' . $ticket . '" class="ticket">
                <div class="ticket-left">
                    <span class="product-ticket left-span">' . $pp_row['product_name'] . ' ' . $pp_row['product_platform'] . ' ' . $pp_row['product_type'] . ' ' . $pp_row['product_region'] . '</span>
                    <span class="ticket-no left-span">Ticket No. ' . $row['ticket_id'] . '</span>
                    <span class="order-no left-span">Order No. ' . $row['order_id'] . '</span>
                    <span class="left-span">Transaction No. ' . $rowTr['txn_id'] . '</span>
                    <span class="product-region left-span">Region: ' . $pp_row['product_region'] . '</span>
                </div>
                <div class="ticket-right">
                    <span class="created">Created: ' . $row['date'] . '</span>
                </div>
                <div class="ticket-bottom">
                    <div class="bottom-holder">
                        <span class="details"><span class="subject-txt">Category: </span>' . $row['category'] . '</span>
                        <span class="details"><span class="subject-txt">Subject: </span>' . $row['subject'] . '</span>
                        <span class="details desc">"' . $row['description'] . '"</span>
                    </div>
                </div>
            </div>
            <div class="removeticketcontainer">
                <span class="removeticket">X</span>
            </div>
         </div>';
            }
        }
    }
}
}