Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/298.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.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 好奇为什么循环不';t显示第二天的数据:_Php_Loops_Nested Loops - Fatal编程技术网

Php 好奇为什么循环不';t显示第二天的数据:

Php 好奇为什么循环不';t显示第二天的数据:,php,loops,nested-loops,Php,Loops,Nested Loops,我试图从存储了值的数据库中读取数据。我试图添加每天的所有值,并编写了以下代码来实现这一点。出于某种原因,只对第一天的行进行了汇总。我确信我的一个循环的条件不起作用是有原因的,但我想我看得太多了。请随便看一眼 $readsum =array(); $totalsum = 0; for ($i=1; $i <= $numdays; $i++){ $readresult=mysql_query("SELECT * FROM `test`.`cost_table` WHERE `Day`='$

我试图从存储了值的数据库中读取数据。我试图添加每天的所有值,并编写了以下代码来实现这一点。出于某种原因,只对第一天的行进行了汇总。我确信我的一个循环的条件不起作用是有原因的,但我想我看得太多了。请随便看一眼

$readsum =array();
$totalsum = 0;


for ($i=1; $i <= $numdays; $i++){
$readresult=mysql_query("SELECT * FROM `test`.`cost_table` WHERE `Day`='$i'",$LinkID);



while($rows=mysql_fetch_array($readresult)){
    $readsum[]=$rows['Yield'];
    $readsum[]=$rows['Rolls'];
    $readsum[]=$rows['Utilities'];
    $readsum[]=$rows['Payroll'];
    $readsum[]=$rows['Direct Materials'];
    $readsum[]=$rows['3rd Party'];
    $readsum[]=$rows['Supplies'];
    $readsum[]=$rows['Packaging'];
    $readsum[]=$rows['Rental'];
    $readsum[]=$rows['Other'];
}   

for($i=0; $i <= 9; $i++){
    $totalsum = $totalsum + $readsum[$i];
    }


echo $totalsum;
$totalsum = 0;

}
$readsum=array();
$totalsum=0;

对于($i=1;$i)来说,在这里展开我的评论,我将如何重写代码以改进它:

$readsum =array();
$totalsum = 0;

$readresult=mysql_query("SELECT * FROM `test`.`cost_table` ORDER BY `Day`",$LinkID);

while($rows=mysql_fetch_array($readresult)){
    $readsum[$rows['Day']][]=$rows['Yield'];
    $readsum[$rows['Day']][]=$rows['Rolls'];
    $readsum[$rows['Day']][]=$rows['Utilities'];
    $readsum[$rows['Day']][]=$rows['Payroll'];
    $readsum[$rows['Day']][]=$rows['Direct Materials'];
    $readsum[$rows['Day']][]=$rows['3rd Party'];
    $readsum[$rows['Day']][]=$rows['Supplies'];
    $readsum[$rows['Day']][]=$rows['Packaging'];
    $readsum[$rows['Day']][]=$rows['Rental'];
    $readsum[$rows['Day']][]=$rows['Other'];
}   

//var_dump $readsum to see what you have to work with

foreach($readsum as $day=>$dataArray){
  foreach($dataArray as $rsum){
    $totalsum = $totalsum + $rsum;
  }
  echo $totalsum;
  $totalsum = 0; 
}
基本上,您可以对整个集合进行查询

浏览结果,构建一个易于使用的关联数组

然后使用该数组对数据执行任何操作


更易于重用和理解。

迭代器使用相同的变量名(
$i
)在两个循环中。这可能会导致问题。此外,您不应该将查询置于循环中。您应该查询整个集合,然后循环查看结果。^您还需要查看缩进。由于缺少缩进,您可能忘记了第二个
for
循环位于另一个
for
循环中。