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

时间:2017-02-25 03:48:53

标签: php html mysql sql html-table

我有以下工作来从sql表打印数据。 但是,我有两个目标。

  1. 还有一个creditcardTotal字段,我也想用它来制作dailyTotal值。这就是我真正想要打印的内容。但是,我不想在sql端有一个dailyTotal值,因为数据会回来一段时间,我不想手动更新数据库。

  2. 一旦我得到dailyTotal工作,我想将它们添加为monthlyTotal值。

  3. 老实说,如果我能弄明白如何从.$data['CashTotal']中获取数据。我可能想知道如何自己计算一切。

    最终目标是建立一个PHP表,显示一个月(每天)的日期和每日总计,然后显示底部的总计。

    我无法弄清楚要搜索的术语,告诉我我想要做什么。

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

    日期员工CashTotal

    2017-01-04 Jon 931.25

    2017-01-05罗宾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泰勒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周杰伦271.5

    2017-01-31 Ellen 230.25

2 个答案:

答案 0 :(得分:0)

在你用来生成表格的循环中,你可以计算$ data [&#39; CashTotal&#39;]的运行总和。然后用它来进行全面展示。

$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 :(得分:0)

SandBox

日期员工CashTotal

2017-01-04 Jon 931.25

2017-01-05罗宾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泰勒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周杰伦271.5

2017-01-31 Ellen 230.25

9549