php - 在列中查找总计

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!

我有一个日志`,用于保存员工的日志记录(赚取的金额等)和分隔数据的代码 到每个员工ID下分组的表中: p>

  Empid:0001 
 -----------------------  ---- \ N |  Logid | 小时| 支付| 
 --------------------------- 
 |  1001 |  10 |  50 | 
 --------------------------- 
 |  1002 |  2 |  10 | 
 --------------------------- 
 
Empid:0003 
 -----------  ---------------- \ N |  Logid | 小时| 支付| 
 --------------------------- 
 |  1003 |  3 |  9 | 
 --------------------------- 
 |  1004 |  6 |  18 | 
 --------------------------- 
  code>  pre> 
 
 

我管理了 使用以下半伪代码: p>

  $ query = mysql_query(“SELECT * FROM`log` ORDER BY empid”); 
 $ id = 0; 
 \  nwhile($ list = mysql_fetch_assoc($ query)){
 if($ id!= $ list ['logid']){
 create header(Logid,Hours,Pay)
 $ id = $ list ['logid  ']; 
} 
为empid添加每个数据行
} 
  code>  pre> 
 
 

但现在我想添加Pay列的总数并放入 它在每个表的底部为每个empid。 p>

通过在while循环中输入代码$ total_pay = $ total_pay + $ list ['pay']我可以得到总薪水,但我无法弄清楚我怎么能够 在底部显示总数。 p>

非常感谢您的任何建议! p> div>

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.

This should do it. You basically sum up until the id is changing.

$sum = 0;
while ($list = mysql_fetch_assoc($query)) {    
    if ($id != $list['logid']) {
          //create the totals using $sum !!!
          // after that re-set sum to 0
          $sum = 0;
          //create header (Logid, Hours, Pay)
          $id = $list['logid'];
    }
    $sum += $list['Pay'];
    //add each data row for the empid
}

Also...

Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.