如何将标题放在一个 while 循环中,仅用于单个结果而不是重复结果


How Can I Put the Heading in a while loop only for individual result not for Duplicate results?

Vehicle num    total_amount
TEST V 1234    500
TEST V 1234    500
TEST V 1234    500
TEST w 785     1000
TEST w 785     1000
TEST Z 589     700
TEST Z 589     700
TEST Z 589     700

但我想喜欢这个。

Vehicle num    total_amount
TEST V 1234    500
TEST V 1234    500
TEST V 1234    500

Vehicle num    total_amount
TEST w 785     1000
TEST w 785     1000

Vehicle num    total_amount
TEST Z 589     700
TEST Z 589     700
TEST Z 589     700

我想显示每个组车辆的上述标题。

  <?php
#other code
$track = null;
$total = 0;
while ($fet_all = mysql_fetch_array($query_all)) :?>    
  <?php if ($track !== null && $track !== $fet_all['truckk_number']): ?>
    <tr>
        <td colspan="3">Vehicle No:</td>
        <td><?php echo $track; ?></td>
    </tr>
    <tr>
        <td colspan="3">Total:</td>
        <td><?php echo $total; ?></td>
    </tr>
    <?php $total = $fet_all['total']; ?>
  <?php else: ?>
    <?php $total += $fet_all['total'];  ?>
  <?php endif; ?>
  <tr class="accord-content even bg-gray" style="color:#698602 !important;">
    <td><?php echo $fet_all['id']; ?></td>
    <td><?php echo $fet_all['ownername']; ?></td>
    <td><?php echo $fet_all['truckk_number']; ?></td>
    <td><?php echo $fet_all['total']; ?></td>
  </tr>
  <?php $track = $fet_all['truckk_number']; //add this line ?>
<?php endwhile; ?>
<?php if ($total > 0): ?>
    <tr>
        <td colspan="3">Vehicle No:</td>
        <td><?php echo $track; ?></td>
    </tr>
    <tr>
        <td colspan="3">Total:</td>
        <td><?php echo $total; ?></td>
    </tr>
<?php endif; ?>
$v = false;
while ($row = someReadFunction()) {
    if ($row["vehicle"] != $v) echo "Write header here...";
    echo implode (" ", $row); // Or your preferred way to display it.
    $v = $row["vehicle"];
};
(?<='n)(([^'n]*)(?:'n'2)*)

你可以试试这个。替换为 Vehicle num total_amount'n$1'n'n 。只需去除标题或删除第一行即可。请参阅演示。

https://www.regex101.com/r/fJ6cR4/10

$re = "/(?<=''n)(([^''n]*)(?:''n''2)*)/i"; 
$str = "TEST V 1234    500'nTEST V 1234    500'nTEST V 1234    500'nTEST w 785     1000'nTEST w 785     1000'nTEST Z 589     700'nTEST Z 589     700'nTEST Z 589     700"; 
$subst = "Vehicle num    total_amount'n'1'n'n"; 
$result = preg_replace($re, $subst, $str);