嵌套的while"循环只运行一次,只显示一个div,找不到任何错误,0个错误在error_log".欢迎提出建议.
我需要第二个 sql 连接,因为 $connectToStore 使 mysqli 连接到单独的数据库.
$items 看起来像 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);如果 (mysqli_num_rows($sqlresult) > 0) {而 ($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);而 ($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 = expand("-", $value);//使用连字符(-) 作为分隔符将产品 ID 与其数量分开$product_id = $id_quantity_pair[0];//获取产品ID$sqlProduct = "SELECT * FROM products WHERE id='$product_id'";$sqlresult = mysqli_query($connectToStore, $sqlProduct);而 ($pp_row = mysqli_fetch_array($sqlresult)) {$tickets .='<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">票号' .$row['ticket_id'] .'</span><span class="order-no left-span">订单号' .$row['order_id'] .'</span><span class="left-span">交易号' .$rowTr['txn_id'] .'</span><span class="product-region left-span">区域:' .$pp_row['product_region'] .'</span><div class="ticket-right"><span class="created">Created: ' .$row['date'] .'</span>
<div class="ticket-bottom"><div class="bottom-holder"><span class="details"><span class="subject-txt">类别:</span>'.$row['category'] .'</span><span class="details"><span class="subject-txt">主题:</span>'.$row['subject'] .'</span><span class="details desc">"' . $row['description'] . '"</span>
<div class="removeticketcontainer"><span class="removeticket">X</span>
';}}}}}
解决了.答案属于 @ckimbrell.问题看起来像是您正在用仅返回 1 行的新结果覆盖变量 $sqlresult.尝试在嵌套循环中更改它."
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");如果 (mysqli_num_rows($sqlresult) > 0) {而 ($row = mysqli_fetch_array($sqlresult)) {$sqlStore = "SELECT * FROM transactions WHERE order_id='".$row['order_id']."'";$sqlStoreDBResult = mysqli_query($connectToStore, $sqlStore);而 ($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 = expand("-", $value);//使用连字符(-) 作为分隔符将产品 ID 与其数量分开$product_id = $id_quantity_pair[0];//获取产品ID$sqlProduct = "SELECT * FROM products WHERE id='$product_id'";$sqlresult2 = mysqli_query($connectToStore, $sqlProduct);而 ($pp_row = mysqli_fetch_array($sqlresult2)) {$tickets .='<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">票号' .$row['ticket_id'] .'</span><span class="order-no left-span">订单号' .$row['order_id'] .'</span><span class="left-span">交易号' .$rowTr['txn_id'] .'</span><span class="product-region left-span">区域:' .$pp_row['product_region'] .'</span><div class="ticket-right"><span class="created">Created: ' .$row['date'] .'</span>
<div class="ticket-bottom"><div class="bottom-holder"><span class="details"><span class="subject-txt">类别:</span>'.$row['category'] .'</span><span class="details"><span class="subject-txt">主题:</span>'.$row['subject'] .'</span><span class="details desc">"' . $row['description'] . '"</span>
<div class="removeticketcontainer"><span class="removeticket">X</span>
';}}}}}
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.
I need second sql connection, because $connectToStore makes mysqli connection with separate database.
$items looks like 1-2,2-5,9-1
I've checked and div that's displayed uses data which is in the first row of the table
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>';
}
}
}
}
}
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 循环只运行一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!