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

如何在表示表上方放置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>';

我正在寻找将求和值放在HTML表格上的简单,最实用的方法。 p>

通常,循环可以在循环结束后生成总和,该总和低于所有结果的表。 当桌子很长时,我们需要每次向下滚动以查看销售总额。 我需要运行两个循环还是有任何技巧可以做到这一点? 任务似乎很简单。 在下面的代码中,我汇总了所有订单和总销售额的数量: p>

  $ lp = 1;  $总= 0;  $ total_qt = 0; 
while($ record = mysql_fetch_array($ result)){
 $ total + = $ record ['Total']; 
 $ total_qt + = $ record ['Qt']; 
echo'&lt;  ; tr style =“background-color:'。((next($ color)== true)?current($ color):reset($ color))。'”&gt; 
&lt; td&gt;  '。  $ lp ++。  '&lt; / td&gt; 
&lt; td class =“bolder”&gt;  '。  $ record ['姓名']。  “&LT; / TD&GT; 
&LT; TD&GT;  '。  $ record ['Product']。  '&lt; / td&gt;'; 
if($ invoice ==“on”)echo'&lt; td style =“font-size:11px”&gt;'  。  $ record ['Invoice']。  '&lt; / td&gt;'; 
echo'&lt; td&gt;  '。  $ record ['despatched']。  “&LT; / TD&GT; 
&LT; TD&GT;  '。  $ record ['Qt']。  “&LT; / TD&GT; 
&LT; TD&GT;  '。  $ record ['Price']。  '&lt; / td&gt; 
&lt; td class =“align_right”&gt;  '。  $ record ['Total']。  '&lt; / td&gt;'; 
 
echo'&lt; / tr&gt;'; 
} 
echo'&lt; / table&gt;&lt; div class =“stat_total”&gt; Qt:'。$ total_qt。' 总计:&amp; pound;'。$ total。'&lt; / div&gt;  &lt; / div&gt;'; 
  code>  pre> 
  div>

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

By using the SUM() SQL function you'll be able to access the sums without needing to run PHP:

SQL

SELECT SUM(Total) FROM Orders