Reputation: 35
I am trying to produce a line plot that shows accrued totals as day by day. I wrote a loop to find the totals of each day, and I thought the logic made sense. To verify the dates being compared, I added two echo statements to visually compare and attempt to step through what is happening. When I entered the two echo statements, the array started working as I expected, but I don't know why. I tried to var-dump to see if it was the type of variable, but it calls $date a string in either case. However, without the echo statements on those two date array positions, I get double the number of totals I should have, because there are only 17 days thus far this month and I get 33 or 34 entries. Is there a way to format this so it continues to work, but take the echo statements out, as this will ultimately be an imported png file? I'm sure the answer is simple, but I am new at this. I appreciate any answers.
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);
Upvotes: 1
Views: 148
Reputation: 270607
Calling prev()
and end()
actually move the array's internal record pointer, which can produce strange behavior when it is being actively read and written. Calling those functions inside the if()
to do the !=
comparison will move the array's pointer into a confusing place.
The solution instead of prev()/end()
is to store the row fetched from ODBC in a variable on its own, and during each loop iteration store it as the $previous
to compare with the current iteration's array.
Additionally, instead of odbc_fetch_row()
, and odbc_result()
, I would recommend switching this to odbc_fetch_array()
which will return an associative array, eliminating the calls to 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);
Upvotes: 1
Reputation: 3658
Break your code up into logical units. First, pull the data. Second, perform any calculations.
$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);
Array $extCosts should look something like:
2015-01-01
2
1
5
2015-01-02
2
2015-01-03
4
7
Then simply use PHP's array functions to get sums
$sums = array();
foreach ( $extCosts as $k => $v ) {
$sums[$k] = array_sum($v);
}
print_r($sums);
Upvotes: 1