Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php-在列中查找总数_Php - Fatal编程技术网

php-在列中查找总数

php-在列中查找总数,php,Php,我有一个日志,用于保存员工的日志记录(收入金额等),还有一个代码,用于将数据分为按每个员工id分组的表: Empid: 0001 --------------------------- | Logid | Hours | Pay | --------------------------- | 1001 | 10 | 50 | --------------------------- | 1002 | 2 | 10 | -----------------

我有一个日志,用于保存员工的日志记录(收入金额等),还有一个代码,用于将数据分为按每个员工id分组的表:

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

Empid: 0003
---------------------------
| Logid   | Hours   | Pay |
---------------------------
|  1003   | 3       | 9   |
---------------------------
|  1004   | 6       | 18  |
---------------------------
我用以下半伪代码管理了这一点:

$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
}
但是现在我想添加Pay列的总数,并将其放在每个empid的每个表的底部

通过将代码$total_pay=$total_pay+$list['pay']放入while循环中,我可以得到总薪酬,但我不知道如何在底部显示总薪酬


如果您能给我一些建议,我将不胜感激

这应该可以。你基本上是总结,直到id改变

$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
}
还有


。它们不再得到维护。看到了吗?相反,学习,并使用,或-将帮助您决定哪一个。如果您选择PDO,.

这应该可以。你基本上是总结,直到id改变

$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
}
还有


。它们不再得到维护。看到了吗?相反,学习,并使用,或-将帮助您决定哪一个。如果您选择PDO,.

有两种方法可以做到这一点

PHP 保持所有“支付”值的总计,并将其添加到底部的表中。例如:

$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有一个函数,它执行的操作与您尝试执行的操作非常类似,称为
ROLLUP
。您可以在此处阅读更多信息:

基本上,您可以将查询更改为如下方式:

SELECT LogId, EmployeeId, SUM(Hours), SUM(Pay) FROM `log` 
GROUP BY empid, logid WITH ROLLUP
---------------------------------------
| 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  |
---------------------------------------
<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 -->
此查询将返回如下所示的数据集:

SELECT LogId, EmployeeId, SUM(Hours), SUM(Pay) FROM `log` 
GROUP BY empid, logid WITH ROLLUP
---------------------------------------
| 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  |
---------------------------------------
<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 -->
每当
$list['Logid']
为空时,您就知道有一个“总计”行。不过要小心,这会在数据集底部添加“所有员工的总和”行。如果
$list['EmployeeId']
为空,则您知道您在“总计”行中


在一个相关的注释中(我不确定这是否是您所要求的),您可以使用HTML
元素在表中显示这些内容

每一行看起来如下所示:

SELECT LogId, EmployeeId, SUM(Hours), SUM(Pay) FROM `log` 
GROUP BY empid, logid WITH ROLLUP
---------------------------------------
| 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  |
---------------------------------------
<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 -->

你的文字在这里
更多的文字可以放在这里

s可以在每个
中无限重复,并且
s可以在
s中重复。

有两种方法可以做到这一点

PHP 保持所有“支付”值的总计,并将其添加到底部的表中。例如:

$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有一个函数,它执行的操作与您尝试执行的操作非常类似,称为
ROLLUP
。您可以在此处阅读更多信息:

基本上,您可以将查询更改为如下方式:

SELECT LogId, EmployeeId, SUM(Hours), SUM(Pay) FROM `log` 
GROUP BY empid, logid WITH ROLLUP
---------------------------------------
| 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  |
---------------------------------------
<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 -->
此查询将返回如下所示的数据集:

SELECT LogId, EmployeeId, SUM(Hours), SUM(Pay) FROM `log` 
GROUP BY empid, logid WITH ROLLUP
---------------------------------------
| 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  |
---------------------------------------
<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 -->
每当
$list['Logid']
为空时,您就知道有一个“总计”行。不过要小心,这会在数据集底部添加“所有员工的总和”行。如果
$list['EmployeeId']
为空,则您知道您在“总计”行中


在一个相关的注释中(我不确定这是否是您所要求的),您可以使用HTML
元素在表中显示这些内容

每一行看起来如下所示:

SELECT LogId, EmployeeId, SUM(Hours), SUM(Pay) FROM `log` 
GROUP BY empid, logid WITH ROLLUP
---------------------------------------
| 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  |
---------------------------------------
<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 -->

你的文字在这里
更多的文字可以放在这里

s可以在每个
中无限重复,并且
s可以在
s中重复。

哦,但是如何在每个empid表的底部显示总数?与Empid:0001一样,pay列下面将显示60,Empid 0003将显示27。当我试图自学php和mysql时,我尝试过mysqli,但不幸的是,它对我不起作用。我认为我的主机的php版本仍然是5.2Oh,但是如何在每个empid表的底部显示总数?与Empid:0001一样,pay列下面将显示60,Empid 0003将显示27。当我试图自学php和mysql时,我尝试过mysqli,但不幸的是,它对我不起作用。我认为我的主机的php版本仍然是5.2。我刚开始编程,还在逻辑上挣扎,我可以澄清这部分代码吗?从if语句开始的代码部分首先检查id是否匹配。第一次迭代它将不匹配,因此它将向i添加1,i将有一个新值1。新值仍然小于1,因此它不会创建页脚并继续创建页眉。下一次迭代由于id匹配,它将继续在标题下创建新的数据行,直到遇到新id为止,同时它还将添加到变量$total\u pay中。在这种情况下,它将在$i中添加1,该$i现在的值为2,因此创建页脚。它会重复,循环完成后,它会创建最后一个页脚。我理解对了吗?对不起,这是一堆文字,只是想从别人的帮助中学习。我还在阅读汇总函数,我在代码中犯了一个错误(我刚刚修复了这个错误)。之前,我们看到的是
LogId=$id
。但是,我们想看看是否
EmployeeId=$id
。下面是程序的流程:我们从第1行开始。我们从第一行开始<代码>$id(空)!=$list['EmployeeId'](1),因此我们输入
if
语句<代码>$i设置为
1
$i
不大于
1
,因此我们跳过footer命令。我们设置
$id=$list['EmployeeId']
并设置
$total\u pay=$list['pay']
。我们为该用户创建标题,并保留
if
语句。我们为该员工添加一个普通行,然后返回循环的开头。现在是第2行
$id(1)=$list['EmployeeId'](1)
,因此我们跳过
if
语句。我们为该员工添加一个普通行,并返回循环的开始。现在我们在第三排<代码>$id(1)!=$list['EmployeeId'](2),因此我们输入
if
语句<代码>$i设置为
2
$i
大于
1
,因此我们为前一名员工添加了一个页脚。我们设置
$id=$list['EmployeeId']
并设置