douben1891 2012-09-29 16:15
浏览 18
已采纳

如何在表示表上方放置sum值(来自MySQL使用PHP)?

I am looking for simple, the best practical way of placing summed value above HTML table.

Normally loops can generate sums after the loop is finished that is below the table of all results. It may be very difficult when the table is very long and we need to scroll down every time to see the sum of sales. Do I need to run two loops or is there any trick to do that? Task seems very simple. In below code I am summing Quantities from all orders and Total Sales values:

$lp=1; $total=0; $total_qt=0;
while ($record = mysql_fetch_array($result)){
$total += $record['Total'];
$total_qt += $record['Qt'];
echo '<tr style="background-color:'. ((next($colour) == true) ? current($colour) :     reset($colour)).'">
<td> ' . $lp++ . '</td>
<td class="bolder"> ' . $record['Name'] . '</td>
<td> ' . $record['Product'] . '</td>';
if ($invoice=="on") echo '<td style="font-size:11px">' . $record['Invoice'] . '</td>';
echo '<td> ' . $record['despatched'] . '</td>
<td> ' . $record['Qt'] . '</td>
<td> ' . $record['Price'] . '</td>
<td class="align_right"> ' . $record['Total'] . '</td>';

echo '</tr>';
}
echo '</table><div class="stat_total">Qt: '.$total_qt.'  Total: &pound;'.$total.'</div>    </div>';
  • 写回答

2条回答 默认 最新

  • dongwen5870 2012-09-29 16:21
    关注

    You could use a variable to store your output while you are in the loop, rather than echo it, and when you finished the loop then echo the stored results with both totals before and after them. Like this:

    $lp=1; $total=0; $total_qt=0;
    $output = ''; //here you temporarily save your output
    while ($record = mysql_fetch_array($result)){
    $total += $record['Total'];
    $total_qt += $record['Qt'];
    $output .= '<tr style="background-color:'. ((next($colour) == true) ? current($colour) :     reset($colour)).'">
    <td> ' . $lp++ . '</td>
    <td class="bolder"> ' . $record['Name'] . '</td>
    <td> ' . $record['Product'] . '</td>';
    if ($invoice=="on") $output .= '<td style="font-size:11px">' . $record['Invoice'] . '</td>';
    $output .= '<td> ' . $record['despatched'] . '</td>
    <td> ' . $record['Qt'] . '</td>
    <td> ' . $record['Price'] . '</td>
    <td class="align_right"> ' . $record['Total'] . '</td>';
    
    $output .= '</tr>';
    }
    //Now you can put a div with the Total both at the start and end of the table 
    echo '<div class="stat_total">Qt: '.$total_qt.'  Total: &pound;'.$total.'</div>';
    echo $output . '</table>';
    echo '<div class="stat_total">Qt: '.$total_qt.'  Total: &pound;'.$total.'</div>    </div>';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题