doukong1391 2013-02-22 12:55
浏览 12
已采纳

php - 在列中查找总计

I have a log` that saves log records (amount earned, etc) of employees and a code that separates the data into tables grouped under each employee id:

Empid: 0001
---------------------------
| Logid   | Hours   | Pay |
---------------------------
|  1001   | 10      | 50  |
---------------------------
|  1002   | 2       | 10  |
---------------------------

Empid: 0003
---------------------------
| Logid   | Hours   | Pay |
---------------------------
|  1003   | 3       | 9   |
---------------------------
|  1004   | 6       | 18  |
---------------------------

I managed this with the following semi-pseudocode:

$query = mysql_query("SELECT * FROM `log` ORDER BY empid");
$id = 0;

while ($list = mysql_fetch_assoc($query)) {
    if ($id != $list['logid']) {
          create header (Logid, Hours, Pay)
          $id = $list['logid'];
          }
    add each data row for the empid
}

But now I would like to add the total of the Pay column and put it at the bottom of each table for each empid.

By putting the code $total_pay = $total_pay + $list['pay'] in the while loop I can get the total pay but I can't figure out how I might be able to show the total at the bottom.

Would really appreciate any advice on this!

  • 写回答

2条回答 默认 最新

  • douyi9787 2013-02-22 13:37
    关注

    There are two ways that you can do this.

    PHP

    Keep a running total of all of the "pay" values, and add it into your table at the bottom. For example:

    $i=0;
    while ($list = mysql_fetch_assoc($query)) {   // for each row in your results
        if ($id != $list['EmployeeId']) {  // We only enter this loop if the EmployeeId doesn't equal $id. This can happen because either $id doesn't exist yet, or it doesn't match the previous EmployeeId
              $i++;  // increase $i by 1
              if($i>1) {  // Enter this loop only if $i is greater than or equal to 2 (if it is less than two, then this is our first time running this script, and adding a footer row wouldn't make any sense).
                  create footer (EmployeeId, Hours, Pay);  // Log Id is irrelevant here
              }
              //  reset your variables here
              $id = $list['EmployeeId'];  // set $id = the first or the new Employee ID
              $total_pay = $list['pay'];  // This is our first time for this Employee, so don't just add it to the running total
              create header (EmployeeId, Hours, Pay) // Create the top half of your table
        } else {  // The EmployeeId has been established: we only need to change the running total
              $total_pay = $total_pay + $list['pay'];
        }
        //  add a data row for each LogId. This executes every time we go through the loop
        create_normal_row(LogId, EmployeeId, Hours, Pay)
    }
    
    // At this point, both Employees have a header, and all data rows. However, we left the loop before we could add the last Employee's footer row
    // Let's add one more footer row for the last user
    create_footer (Logid, Hours, Pay);
    

    SQL

    MySQL has a function that does something very similar to what you are trying to do called ROLLUP. You can read more about it here:

    http://dev.mysql.com/doc/refman/5.0/en/group-by-modifiers.html

    Basically, you would change your query to work like this:

    SELECT LogId, EmployeeId, SUM(Hours), SUM(Pay) FROM `log` 
    GROUP BY empid, logid WITH ROLLUP
    

    This query will return a dataset that looks like this:

    ---------------------------------------
    | Logid   | EmployeeId| Hours   | Pay |
    ---------------------------------------
    |  1001   | 1         | 10      | 50  |
    ---------------------------------------
    |  1002   | 1         | 2       | 10  |
    ---------------------------------------
    |  NULL   | 1         | 12      | 60  |
    ---------------------------------------
    |  1003   | 2         | 3       | 9   |
    ---------------------------------------
    |  1004   | 2         | 6       | 18  |
    ---------------------------------------
    |  NULL   | 2         | 9       | 27  |
    ---------------------------------------
    |  NULL   | NULL      | 21      | 87  |
    ---------------------------------------
    

    Whenever $list['Logid'] is null, you know that you have a "total" row. Be careful though, this will add a "sum of all employees" row at the bottom of your dataset. If $list['EmployeeId'] is null, then you know you're in this "total" row.


    On a related note (I'm not sure if this is what you're asking for), you can show this stuff in a table by using HTML <table> elements.

    Each row would look like this:

    <table> <!-- shown at the beginning of each table -->
    <tr> <!-- shown at the beginning of each row -->
    <td> <!-- shown at the beginning of each table cell -->
    Your text goes here
    </td> <!-- shown at the end of each table cell -->
    <td>
    More text can go here
    </td>
    </tr> <!-- shown at the end  of each row -->
    </table> <!-- shown at the end of each table -->
    

    <tr>s can be repeated indefinitely within each <table>, and <td>s can be repeated within <tr>s.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?