我在搜索参数后有一个结果数组,我想打印那个结果。当使用foreach循环打印结果时,我需要检查条件,如果购买日期相同,它们应该打印在同一行。我该如何实现呢?请帮帮我。谢谢你提前
这是我的代码:(我已在控制器中获得结果,然后我将结果附加到视图中)
foreach($res as $key => $row)
{
$i++;
$billfirst='';
$billend= $row['bill_date'];
if( $billfirst != $billend){
echo "<tr>
<td class='txt'> ".$i."</td>
<td class='txt'> ".$row['store']."</td>
<td class='txt'> ".$row['cust']."</td>
<td class='txt'> ".$row['name']."</td>
<td class='txt'> ".$row['net_amount']."</td>
<td class='txt'> ".$row['bill_date']."</td>
</tr>" ;
}
else {
echo "<tr><td class='txt'> ".$row['name']."</td>
<td class='txt'> ".$row['net_amount']."</td>
<td class='txt'> ".$row['bill_date']."</td>
</tr>";
}
$billfirst == $billend;
echo "</tbody></table>";
}
答案 0 :(得分:0)
您需要创建其他数组来对行进行分组,例如:
$arRows = array();
$billPrevious = "empty";
// save new grouped array
foreach($res as $key => $row) {
$arRows[$row['bill_date']][] = $row;
}
// show groups - in the way you want to group it visually in table
foreach ($arRows as $key => $arGroup) {
echo '<tr>';
$arName = '';
$arStore = '';
foreach ($arGroup as $keyInGroup => $row) {
$arName[] = $row['name'];
$arStore[] = $row['store'];
}
echo '<td>'.implode('<br>', $arName).'</td>';
echo '<td>'.implode('<br>', $arStore).'</td>';
echo '</tr>';
}