SQL 查询 - 数组但特定列只显示 1 次
SQL Query - Array but Specific Column only show 1 time
型号
$sql = "
SELECT op.name
, op.price
, op.quantity
, o.order_cancel_remark
FROM order_product op
LEFT
JOIN `order` o
ON o.order_id = op.order_id
LEFT
JOIN `order_product_history` oph
ON oph.`order_product_id` = op.`order_product_id`
AND oph.`current_status` = 1
LEFT
JOIN `order_status` os
ON os.`order_status_id` = oph.`order_status_id`
AND os.`language_id` = 1
WHERE o.order_id = '" . (int)$order_id . "'";
$query = $this->db->query($sql);
return $query->rows;
控制器
$data['orderP_results'][] = array(
'name' => $order_price_result['name'] ,
'price' => number_format($order_price_result['map'],2,'.',''),
'quantity' => $order_price_result['quantity'],
'sku' => $order_price_result['sku'],
'order_cancel_remark' => $order_price_result['order_cancel_remark'],
'total' => number_format($order_price_result['map'] * $order_price_result['quantity'],2,'.',''),
);
查看页面
{% if orderP_results %}
{% for orderP_result in orderP_results %}
{{orderP_result.order_cancel_remark}}
<table class="d">
<tr>
<th> <legend style="padding-top:10px;">Product Image</legend></th>
<th> <legend style="padding-top:10px;">Product Name</legend></th>
<th> <legend style="padding-top:10px;">Product SKU</legend></th>
</tr>
<tr>
<td> <img src="{{orderP_result.image}}"></td>
<td> <b class="color-bold">{{orderP_result.name}}</b> </td>
<td> SKU: <b class="color-bold">{{orderP_result.sku}}</b></td>
</tr>
</table>
我想要的:"Order Cancel Remark" 只显示 1 次而不是多次(取决于行数)
示例(这就是我想要的):https://prnt.sc/rwgi8u
在您看来,从 for 循环中删除 order_cancel_remark 并将其添加到外部以仅显示一次
{% if orderP_results %}
{{orderP_result[0].order_cancel_remark}}
{% for orderP_result in orderP_results %}
<table class="d">
<tr>
<th> <legend style="padding-top:10px;">Product Image</legend></th>
<th> <legend style="padding-top:10px;">Product Name</legend></th>
<th> <legend style="padding-top:10px;">Product SKU</legend></th>
</tr>
<tr>
<td> <img src="{{orderP_result.image}}"></td>
<td> <b class="color-bold">{{orderP_result.name}}</b> </td>
<td> SKU: <b class="color-bold">{{orderP_result.sku}}</b></td>
</tr>
</table>
仅供参考,您的查询在功能上与以下内容相同:
SELECT op.name
, op.price
, op.quantity
, o.order_cancel_remark
FROM order_product op
JOIN `order` o
ON o.order_id = op.order_id
WHERE o.order_id = '" . (int)$order_id . "'";
型号
$sql = "
SELECT op.name
, op.price
, op.quantity
, o.order_cancel_remark
FROM order_product op
LEFT
JOIN `order` o
ON o.order_id = op.order_id
LEFT
JOIN `order_product_history` oph
ON oph.`order_product_id` = op.`order_product_id`
AND oph.`current_status` = 1
LEFT
JOIN `order_status` os
ON os.`order_status_id` = oph.`order_status_id`
AND os.`language_id` = 1
WHERE o.order_id = '" . (int)$order_id . "'";
$query = $this->db->query($sql);
return $query->rows;
控制器
$data['orderP_results'][] = array(
'name' => $order_price_result['name'] ,
'price' => number_format($order_price_result['map'],2,'.',''),
'quantity' => $order_price_result['quantity'],
'sku' => $order_price_result['sku'],
'order_cancel_remark' => $order_price_result['order_cancel_remark'],
'total' => number_format($order_price_result['map'] * $order_price_result['quantity'],2,'.',''),
);
查看页面
{% if orderP_results %}
{% for orderP_result in orderP_results %}
{{orderP_result.order_cancel_remark}}
<table class="d">
<tr>
<th> <legend style="padding-top:10px;">Product Image</legend></th>
<th> <legend style="padding-top:10px;">Product Name</legend></th>
<th> <legend style="padding-top:10px;">Product SKU</legend></th>
</tr>
<tr>
<td> <img src="{{orderP_result.image}}"></td>
<td> <b class="color-bold">{{orderP_result.name}}</b> </td>
<td> SKU: <b class="color-bold">{{orderP_result.sku}}</b></td>
</tr>
</table>
我想要的:"Order Cancel Remark" 只显示 1 次而不是多次(取决于行数)
示例(这就是我想要的):https://prnt.sc/rwgi8u
在您看来,从 for 循环中删除 order_cancel_remark 并将其添加到外部以仅显示一次
{% if orderP_results %}
{{orderP_result[0].order_cancel_remark}}
{% for orderP_result in orderP_results %}
<table class="d">
<tr>
<th> <legend style="padding-top:10px;">Product Image</legend></th>
<th> <legend style="padding-top:10px;">Product Name</legend></th>
<th> <legend style="padding-top:10px;">Product SKU</legend></th>
</tr>
<tr>
<td> <img src="{{orderP_result.image}}"></td>
<td> <b class="color-bold">{{orderP_result.name}}</b> </td>
<td> SKU: <b class="color-bold">{{orderP_result.sku}}</b></td>
</tr>
</table>
仅供参考,您的查询在功能上与以下内容相同:
SELECT op.name
, op.price
, op.quantity
, o.order_cancel_remark
FROM order_product op
JOIN `order` o
ON o.order_id = op.order_id
WHERE o.order_id = '" . (int)$order_id . "'";