Echo语句以某种方式修复while循环问题,需要将echo取出

时间:2015-02-17 21:07:57

标签: php arrays while-loop

我正在尝试制作一个线图,显示每天累积的总数。我写了一个循环来查找每天的总数,我认为逻辑是有道理的。为了验证所比较的日期,我添加了两个echo语句以进行直观比较并尝试逐步完成所发生的事情。当我输入两个echo语句时,数组开始按预期工作,但我不知道为什么。我尝试var转储以查看它是否是变量的类型,但它在任何一种情况下都将$ 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);

2 个答案:

答案 0 :(得分:1)

调用prev()end()实际上会移动数组的内部记录指针,这会在主动读取和写入时产生奇怪的行为。在if()内调用这些函数进行!=比较会将数组的指针移动到一个令人困惑的地方。

而不是prev()/end()的解决方案是将从ODBC中获取的行单独存储在变量中,并且在每次循环迭代期间将其存储为$previous以与当前迭代的数组进行比较。 / p>

此外,我建议将odbc_fetch_array()切换为{{3}}而不是odbc_fetch_row()odbc_result(),这会返回一个关联数组,从而取消对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);

答案 1 :(得分:1)

将代码分解为逻辑单元。首先,拉数据。其次,执行任何计算。

$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);