繁体   English   中英

如何将相同结果的输出与 PHP While 循环分开?

[英]How Can I Break the Output from same result a PHP While Loop?

例如,在 while 循环中,结果将按这样的车辆编号顺序显示

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

But i want to like this.

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

Vehicle No: TEST V 1234
Total amount: 1500

TEST w 785     1000
TEST w 785     1000

Vehicle No: TEST w 785
Total amount: 2000


TEST Z 589     700
TEST Z 589     700
TEST Z 589     700

Vehicle No: TEST Z 589
Total amount: 2100

我想使用php + mysql显示像dis一样如何解决这个问题,有人能告诉吗?

这是我的代码示例?

<table class="table table-table-striped accord-content even" width="100%" style="clear:both;" id="textttt">
  <thead>
    <tr class="bg-green" style="background:#DBB74E!important; color:#698602 !important" >
      <th>S No</th>
      <th>Owner Name</th>
      <th>Truck Number</th>
      <th>total</th>
    </tr>
  </thead>
  <?php

$query_all = mysql_query("select * from testing where ownername='" . $_POST['ownername'] . "' and dc_date between '".$weekenddate."'   and   '" . $_POST['datewe'] . "' and status='Verified' order by truckk_number ASC");

        while ($fet_all = mysql_fetch_array($query_all)) {  ?>
  <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   } ?>
</table>

我想显示明智的车辆总数和车辆编号。

因为你的问题没有代码,我这里用的是伪代码

Set track = null, total = 0
Loop through your collection
    If track is not null and current item vehicle_num is different from track
        Print current vehicle_num
        Print total
        Set total = 0
    else
        Update total = total + current total_amount
    endif
    Set track = current item vehicle_num
    Print current item info
endloop

对于您的代码,请考虑替换此部分:

while ($fet_all = mysql_fetch_array($query_all)) {  ?>
  <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   } ?>

对此:

<?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; ?>

你的查询是这样的......

SELECT *, SUM(total_amount)  FROM YOUR_TABLENAME  GROUP BY Vehicle_num_COLUMN_NAME

使用 GROUP BY 子句。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM