doufei2328 2017-02-25 03:48
浏览 28
已采纳

使用来自sql表的数据制作php / html表的术语,但php表值是从sql数据计算的

I've got the following working for printing the data from the sql table. However, I have two goals.

  1. There is a creditcardTotal field that I'd also like to use to make a dailyTotal value. That is what I really want to print. However, I do not want to have a dailyTotal value on the sql side as the data goes back for a while and I don't want to manually update the database.

  2. Once I get the dailyTotal working, I'd like to add those up for a monthlyTotal value.

Honestly, if I could figure out how to actually get the data out of .$data['CashTotal']. I could probably figure out how to calculate everything myself.

The ultimate goal is to have a a PHP table that shows the dates and the daily totals for a month (for each day) then a total at the bottom.

I can't figure out the term to search to show me what I'm wanting to do.

  $result = mysql_query("SELECT * from HP_Closing_Count WHERE '2017-01-01' <=     Date and Date < '2017-02-01'");

echo '<table border=2px>';  // opening table tag
echo'<th>Date</th>
<th>Employee</th>
<th>CashTotal</th>
'; //table headers



while($data = mysql_fetch_array($result))
{

// we are running a while loop to print all the rows in a table
echo'<tr>'; // printing table row
echo '<td>'.$data['Date'].'</td>
<td>'.$data['Employee'].'</td>
<td>'.$data['CashTotal'].'</td>
'; // we are looping all data to be printed till last row in the table
echo'</tr>'; // closing table row
}

echo '</table>';  //closing table tag

?>

Date Employee CashTotal

2017-01-04 Jon 931.25

2017-01-05 Robin 0

2017-01-07 Jon 383.5

2017-01-06 Jay 275.25

2017-01-08 Jon 417.5

2017-01-09 Jay 297.75

2017-01-10 Colwyn C 280.5

2017-01-11 Colwyn 344

2017-01-12 Ellen 374.5

2017-01-13 Jay 288.5

2017-01-14 Jay 377.75

2017-01-15 Jay 349.25

2017-01-16 Jay 376.5

2017-01-17 Colwyn 248.5

2017-01-18 Taylor 210.5

2017-01-19 COLWYN 247

2017-01-20 Jay 330.75

2017-01-21 Jay 475.75

2017-01-22 Jay 489.75

2017-01-23 Jay 344

2017-01-24 Parker 321.25

2017-01-25 Taylor 257.25

2017-01-26 Ellen 249

2017-01-27 Jay 318.25

2017-01-28 Jay 477

2017-01-29 Jay 382

2017-01-30 Jay 271.5

2017-01-31 Ellen 230.25

  • 写回答

2条回答 默认 最新

  • douzhao9608 2017-02-25 04:12
    关注

    Inside the loop you are using to generate the table you can tally a running sum of $data['CashTotal']. Then use that for your grand total display.

    $grandTotal = 0;
    while($data = mysql_fetch_array($result))
    {
    
    // we are running a while loop to print all the rows in a table
    echo'<tr>'; // printing table row
    echo '<td>'.$data['Date'].'</td>
    <td>'.$data['Employee'].'</td>
    <td>'.$data['CashTotal'].'</td>
    '; // we are looping all data to be printed till last row in the table
    echo'</tr>'; // closing table row
    $grandTotal += $data['CashTotal'];
    }
    echo '</table>';  //closing table tag
    echo 'Total: ' . $grandTotal;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 爬虫爬取网站的一些信息
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含
  • ¥15 麒麟V10桌面版SP1如何配置bonding
  • ¥15 Marscode IDE 如何预览新建的 HTML 文件
  • ¥15 K8S部署二进制集群过程中calico一直报错