Nested while loop runs only once

后端 未结 2 1199

Nested \"while\" loop runs only one time, only one div is displayed, can\'t find any mistakes, 0 errors in \"error_log

相关标签:
2条回答
  • 2021-01-26 13:04

    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>';
                }
            }
        }
    }
    }
    
    0 讨论(0)
  • 2021-01-26 13:05

    Looks like your outermost while is only fetching one record

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

    Get rid of the outermost while if you only fetch one record...

    $row = mysqli_fetch_array($sqlresult);// remove closing } too 
    
    0 讨论(0)
提交回复
热议问题