【问题标题】:condition checking inside the foreach loop and printing multiple result with in one rowforeach 循环内的条件检查并在一行中打印多个结果
【发布时间】:2018-09-07 22:51:37
【问题描述】:

搜索参数后我有一个结果数组,我想打印该结果。使用 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>";

}

【问题讨论】:

  • foreach($res as $key =&gt; $row) enter code here{ ?这是什么....肯定不是有效的PHP。
  • 请多解释。没看清楚。你的预期结果是什么?请同时附上预期结果。
  • var_dump($res)?

标签: php html html-table


【解决方案1】:

您需要制作额外的数组来对您的行进行分组,例如:

$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>';
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-09
    • 1970-01-01
    • 2019-09-04
    • 2017-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多