the outer loop is a list, and the inner loop is a list that takes an item from the list as a condition. In this case, it is recommended to do two queries, and then do memory association processing.
<?
$order_list = [];
while($row = mysqli_fetch_array($data)){
$order_list[] = $row;
}
$product_data = $pdo->query(
"SELECT DISTINCT p.prod_id, p.icon FROM product AS p
JOIN product_order AS po ON p.prod_id = po.prod_id
JOIN user_order AS uo AND uo.id = '{$_SESSION['user-id']}' "
);
$sub_order_list=[];
while($row = mysqli_fetch_array($product_data)){
$sub_order_list[] = $row;
}
$order_map = [];
foreach($order_list as &$val){
print_r($val);
$order_map[$val['order_id']]=&$val;
}
foreach($sub_order_list as &$val){
$order=&$order_map[$val['order_id']];
if(isset($order['children'])){
$order['children'][]=$val;
}else{
$order['children']=[$val];
}
}
foreach($order_list as $val){
print_r($val);
}
it's strange that I answered it on my mobile phone last night. Thank you for inviting me. Your requirement is that you need to query the data of another table according to a certain value in one table to get detailed relevant information. Here you can consider using a linked table query, and you can learn about the join operation of mysql yourself. It is recommended to learn about it first and then refer to the sql provided by me.
<?php
$sql = ' select tOne.order_id as tOrder_id,tOne.user-id as tUser_id,po.*,uo.* select tableName tOne left join product_order as po on po.order_id = tOne.order_id left join user_order as uo on uo.id = tOne.user-id ';
$data = $pdo -> query($sql);
$html = '';
while($row = mysqli_fetch_array($data)){
$html .= '<img class="" src="'.$row['icon'].'">';
}
echo $html;
Ah, after a closer look, I found that you have already used join, so why not use more? Yesterday, I looked at it on my mobile phone. Because of the way you wrote the code, it looked very painful, so I didn't look at the code in detail.
finally, I hope the above content is helpful to you.
one subquery
"SELECT DISTINCT p.prod_id, p.icon FROM product AS p
JOIN product_order AS po ON p.prod_id = po.prod_id
JOIN user_order AS uo ON
po.order_id in (select order_id from PRODUCTTABLENAME)
AND
uo.id in (select user_id from PRODUCTTABLENAME)"
the result of such a query will come out, and then iterate through the table that outputs PRODUCTTABLENAME as your first circular query
use mysql connected table query, do not loop query, very slow
upstairs positive solution, the operation of the database should not be put in the loop statement, the table association query had better be done outside the loop. And you seem to be using a frame here, right? I remember that in the YII framework, there is " = $row ['order_id'];? >". With the framework, the division of MVC is still quite clear
.