嵌套的while循环仅运行一次

嵌套的while循环仅运行一次

本文介绍了嵌套的while循环仅运行一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嵌套的 同时循环仅运行一次,仅显示一个 div,找不到任何错误, 0 个错误 error_log 。随时建议。

Nested "while" loop runs only one time, only one div is displayed, can't find any mistakes, 0 errors in "error_log". Feel free to suggest.

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

I need second sql connection, because $connectToStore makes mysqli connection with separate database.

$ items看起来像1-2,2-5,9-1

ve选中并显示的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>';
                }
            }
        }
    }
}


推荐答案

已解决。答案属于 @ckimbrell 。 问题似乎是您用新结果仅返回1行覆盖了变量$ sqlresult。尝试在嵌套循环中更改它。

Solved it. Answer belongs to @ckimbrell. "The problem looks like you are overwriting the variable $sqlresult with a new result that only returns 1 row. Try to change that in the nested loop."

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>';
            }
        }
    }
}
}

这篇关于嵌套的while循环仅运行一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 03:21