开发者

Cannot push values into an array from withing do ... while loop

开发者 https://www.devze.com 2023-01-25 11:30 出处:网络
I\'ve searched and searched and can\'t find anyone with a similar problem. I have a do ... while loop with code to push values from each record into four different arrays. All seems fine except that

I've searched and searched and can't find anyone with a similar problem.

I have a do ... while loop with code to push values from each record into four different arrays. All seems fine except that the arrays are empty (giving me the Warning: division by zero).

I am able to echo the four values for each record as well as the unix timestamp for each order date within the loop, so the values are correct for each record but the arrays are empty. I tried commenting out the conditional statement around the block of array[] code in the loop, but no difference.

Also tried array_push instead just to see; again, no difference.

I am a newbie, and for the life of me can't see why the arrays aren't getting populated.

This is the code outside the loop:

// set timezone and $one_year and $year_limit variables (used in the do-while loop)
ini_set('date.timezone', 'America/Los_Angeles');
       $one_year = 60 * 60 * 24 * 365;
       $year_limit = strtotime("-1 year");

// create the empty arrays for each rating
$total_vals = array();
$food_vals = array();
$service_vals = array();
$atmo_vals = array();

// get the average for each rating
$total_avg = round((array_sum($total_vals)) / (count($total_vals)));
$food_avg = round((array_sum($food_vals)) / (count($food_vals)));
$service_avg = round((array_sum($service_开发者_Go百科vals)) / (count($service_vals)));
$atmo_avg = round((array_sum($atmo_vals)) / (count($atmo_vals)));

This is the loop:

<?php do { ?>
  <?php 
  // create unix timestamp from order_date
  $pub_date = strtotime($row_getReviews['order_date']);

  // add rate values from each record to the arrays if less than 1 year old
  if($pub_date - $one_year < $year_limit) { // tried commenting the condition out
 $total_vals[] = $row_getReviews['total_rate'];
 $food_vals[] = $row_getReviews['food_rate'];
 $service_vals[] = $row_getReviews['service_rate'];
 $atmo_vals[] = $row_getReviews['atmo_rate'];
 }
  ?>    

<!-- html, css, etc here to display text and more -->

<?php } while ($row_getReviews = mysql_fetch_assoc($getReviews)); ?>

Any help is greatly appreciated!


Try

Create empty arrays

Loop

Get averages

In that order. Be sure that the loop occurs before you get averages.

And use any sort of loop other than do ... while, so that you have data on the first iteration for the reasons Mayhem stated.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号