Echo 语句以某种方式修复了 while 循环问题,需要取出 echo
Echo statements fix while loop issue somehow, need to take echo out
我正在尝试制作一个折线图,显示每天的应计总额。我写了一个循环来查找每一天的总数,我认为这个逻辑是有道理的。为了验证比较的日期,我添加了两个 echo 语句来直观比较并尝试逐步了解正在发生的事情。当我输入这两个 echo 语句时,数组开始按预期工作,但我不知道为什么。我试图通过 var-dump 来查看它是否是变量的类型,但在任何一种情况下它都将 $date 称为字符串。但是,如果没有对这两个日期数组位置的 echo 语句,我得到的总数是我应该拥有的总数的两倍,因为这个月到目前为止只有 17 天,我得到了 33 或 34 个条目。有没有办法格式化它以便它继续工作,但去掉 echo 语句,因为这最终将是一个导入的 png 文件?我确定答案很简单,但我是新手。我很感激任何答案。
while ($rows=odbc_fetch_row($result)){
$unit=odbc_result($result,"unit_cost"); /*Rounding for Unit Cost*/
$unit_cost=round($unit,2);
$ext=odbc_result($result,"ext_cost"); /*Rounding for Extended Cost*/
$ext_cost=round($ext,2);
$date[]=odbc_result($result,"date_created");
echo (prev($date));
echo (end($date));
if (prev($date) != end($date)){ //if previous element's date does not match current array's date:
$total[]=$sum; //report total summation to entry in total array, rezero sum variable.
$sum = 0;
$sum = $sum + $ext;
}
else{ //if both element's have same date:
$sum=$sum + $ext; //add extended cost of this line to total $sum, to be reported as an element in $total once this date is finished.
}
}//end while
print_r($total);
调用prev()
and end()
实际上移动了数组的内部记录指针,当它被主动读写时会产生奇怪的行为。在 if()
中调用这些函数来进行 !=
比较会将数组的指针移动到一个令人困惑的地方。
代替prev()/end()
的解决方案是将从ODBC获取的行单独存储在一个变量中,并在每次循环迭代期间将其存储为$previous
以与当前迭代的数组进行比较.
此外,我建议将其切换为 odbc_fetch_array()
而不是 odbc_fetch_row()
和 odbc_result()
,这将 return 一个关联数组,从而消除对 odbc_result()
.
// Initialize an empty var for the comparison date
$prev_date = null;
// Init $sum for later...
// It looks like you need this...
$sum = 0;
// Get $row as an assoc array
while ($row = odbc_fetch_array($result)){
$unit=$row['unit_cost']; /*Rounding for Unit Cost*/
$unit_cost=round($unit,2);
$ext=$row['ext_cost']; /*Rounding for Extended Cost*/
$ext_cost=round($ext,2);
// Store the new date in $current_date
$current_date = $row['date_created'];
// Store it onto $date
$date[] = $current_date;
// And you can safely echo these or not...
echo "Current date: " . $current_date;
echo "Previous date: " . $prev_date;
// Now compare with the previous date value
if ($prev_date) != $current_date)){ //if previous element's date does not match current array's date:
// Where does $sum come from at first? Is it initialized outside the loop?
$total[]=$sum; //report total summation to entry in total array, rezero sum variable.
$sum = 0;
$sum = $sum + $ext;
}
else{ //if both element's have same date:
$sum=$sum + $ext; //add extended cost of this line to total $sum, to be reported as an element in $total once this date is finished.
}
// After the comparison, store the current into previous
// for the next loop comparison
$prev_date = $current_date;
}//end while
print_r($total);
将您的代码分解为逻辑单元。首先,拉取数据。其次,执行任何计算。
$extCosts = array();
while ( odbc_fetch_row($result) ) { // http://php.net/manual/en/function.odbc-result.php
$ext = odbc_result($result,"ext_cost");
$ext_cost = round($ext,2);
$date = odbc_result($result,"date_created");
$extCosts[$data][] = $ext_cost;
}
print_r($extCosts);
数组 $extCosts 应该类似于:
2015-01-01
2
1
5
2015-01-02
2
2015-01-03
4
7
然后简单地使用PHP的数组函数来求和
$sums = array();
foreach ( $extCosts as $k => $v ) {
$sums[$k] = array_sum($v);
}
print_r($sums);
我正在尝试制作一个折线图,显示每天的应计总额。我写了一个循环来查找每一天的总数,我认为这个逻辑是有道理的。为了验证比较的日期,我添加了两个 echo 语句来直观比较并尝试逐步了解正在发生的事情。当我输入这两个 echo 语句时,数组开始按预期工作,但我不知道为什么。我试图通过 var-dump 来查看它是否是变量的类型,但在任何一种情况下它都将 $date 称为字符串。但是,如果没有对这两个日期数组位置的 echo 语句,我得到的总数是我应该拥有的总数的两倍,因为这个月到目前为止只有 17 天,我得到了 33 或 34 个条目。有没有办法格式化它以便它继续工作,但去掉 echo 语句,因为这最终将是一个导入的 png 文件?我确定答案很简单,但我是新手。我很感激任何答案。
while ($rows=odbc_fetch_row($result)){
$unit=odbc_result($result,"unit_cost"); /*Rounding for Unit Cost*/
$unit_cost=round($unit,2);
$ext=odbc_result($result,"ext_cost"); /*Rounding for Extended Cost*/
$ext_cost=round($ext,2);
$date[]=odbc_result($result,"date_created");
echo (prev($date));
echo (end($date));
if (prev($date) != end($date)){ //if previous element's date does not match current array's date:
$total[]=$sum; //report total summation to entry in total array, rezero sum variable.
$sum = 0;
$sum = $sum + $ext;
}
else{ //if both element's have same date:
$sum=$sum + $ext; //add extended cost of this line to total $sum, to be reported as an element in $total once this date is finished.
}
}//end while
print_r($total);
调用prev()
and end()
实际上移动了数组的内部记录指针,当它被主动读写时会产生奇怪的行为。在 if()
中调用这些函数来进行 !=
比较会将数组的指针移动到一个令人困惑的地方。
代替prev()/end()
的解决方案是将从ODBC获取的行单独存储在一个变量中,并在每次循环迭代期间将其存储为$previous
以与当前迭代的数组进行比较.
此外,我建议将其切换为 odbc_fetch_array()
而不是 odbc_fetch_row()
和 odbc_result()
,这将 return 一个关联数组,从而消除对 odbc_result()
.
// Initialize an empty var for the comparison date
$prev_date = null;
// Init $sum for later...
// It looks like you need this...
$sum = 0;
// Get $row as an assoc array
while ($row = odbc_fetch_array($result)){
$unit=$row['unit_cost']; /*Rounding for Unit Cost*/
$unit_cost=round($unit,2);
$ext=$row['ext_cost']; /*Rounding for Extended Cost*/
$ext_cost=round($ext,2);
// Store the new date in $current_date
$current_date = $row['date_created'];
// Store it onto $date
$date[] = $current_date;
// And you can safely echo these or not...
echo "Current date: " . $current_date;
echo "Previous date: " . $prev_date;
// Now compare with the previous date value
if ($prev_date) != $current_date)){ //if previous element's date does not match current array's date:
// Where does $sum come from at first? Is it initialized outside the loop?
$total[]=$sum; //report total summation to entry in total array, rezero sum variable.
$sum = 0;
$sum = $sum + $ext;
}
else{ //if both element's have same date:
$sum=$sum + $ext; //add extended cost of this line to total $sum, to be reported as an element in $total once this date is finished.
}
// After the comparison, store the current into previous
// for the next loop comparison
$prev_date = $current_date;
}//end while
print_r($total);
将您的代码分解为逻辑单元。首先,拉取数据。其次,执行任何计算。
$extCosts = array();
while ( odbc_fetch_row($result) ) { // http://php.net/manual/en/function.odbc-result.php
$ext = odbc_result($result,"ext_cost");
$ext_cost = round($ext,2);
$date = odbc_result($result,"date_created");
$extCosts[$data][] = $ext_cost;
}
print_r($extCosts);
数组 $extCosts 应该类似于:
2015-01-01
2
1
5
2015-01-02
2
2015-01-03
4
7
然后简单地使用PHP的数组函数来求和
$sums = array();
foreach ( $extCosts as $k => $v ) {
$sums[$k] = array_sum($v);
}
print_r($sums);