我正在尝试制作一个线图,显示每天的应计总数。我写了一个循环来查找每天的总数,我认为这个逻辑是有道理的。为了验证正在比较的日期,我添加了两个 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()
和 end()
实际上会移动数组的内部记录指针,这在主动读写时会产生奇怪的行为。在 if()
内调用这些函数来进行 !=
比较会将数组的指针移动到一个令人困惑的地方。
替代
prev()/end()
的解决方案是将从 ODBC 获取的行单独存储在变量中,并在每次循环迭代期间将其存储为 $previous
以与当前迭代的数组进行比较。
此外,我建议将其切换为
odbc_fetch_row()
,而不是 odbc_result()
和
odbc_fetch_array()
,它将返回关联数组,从而消除对 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);