Nested while loop runs only once

后端 未结 2 1204

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 .= '
    ' . $pp_row['product_name'] . ' ' . $pp_row['product_platform'] . ' ' . $pp_row['product_type'] . ' ' . $pp_row['product_region'] . ' Ticket No. ' . $row['ticket_id'] . ' Order No. ' . $row['order_id'] . ' Transaction No. ' . $rowTr['txn_id'] . ' Region: ' . $pp_row['product_region'] . '
    Created: ' . $row['date'] . '
    Category: ' . $row['category'] . ' Subject: ' . $row['subject'] . ' "' . $row['description'] . '"
    X
    '; } } } } }

提交回复
热议问题