如何在代码点火器中单行显示相同的 id 数据?


public function get_orderProduct($loginuserID) {
$this->db->select('client_order.*,product.image_gallery,product.name');
$this->db->from('client_order');
$this->db->join('product','client_order.productID = product.productID','LEFT');
$this->db->where('client_order.clientID',$loginuserID);
$this->db->where('client_order.status',1);
$this->db->order_by('client_order.id','desc');
$sql = $this->db->get();
$result = $sql->result();
return $result;
}
<?php 
if(count($result) > 0){
$i=1;
foreach($result as $row){
$img = explode(",", $row->image_gallery);
?>
<tr>
<td><?php echo $i; ?></td>
<td><?php echo $row->orderID; ?></td>
<td><?php echo $row->name; ?></td>
</tr>
<?php
$i++;
}    
}else{
?>
<div class="alert alert-danger alert-dismissible fade show" role="alert">
No order available!
</div>
<?php
}
?>

我有两个订单 ID 相同的产品,我想在单行中显示相同的订单 ID 产品。现在,这里它显示在不同的不同行中。那么,如何在单行中显示?请帮助我。

谢谢

要解决此问题,您必须创建一个空数组来存储 orderid。在打印 orderid 之前,您必须使用in_array方法搜索数组,如果 orderid 存在,请忽略打印它,否则使用array_push方法打印并将 orderID 推送到数组。

<?php
$ids = array();
$i=1;
foreach($result as $row){
$img = explode(",", $row->image_gallery);
?>
<tr>
<td><?php echo $i; ?></td>
<td>
<?php 
if(!in_array($row->orderID,$ids))
{
echo $row->orderID;
array_push($ids,$row->orderID); 
}
?>
</td>
<td><?php echo $row->name; ?></td>
</tr>
<?php
$i++;
}  
?> 

最新更新