Cyclist Cyclist - 4 months ago 35
MySQL Question

Trouble making foreach loop for multi-dimensional array with StdClass Objects

I'm learning PHP and code igniter, and am new to modern programming in general, so please be kind!

I wrote a function to generate weekly totals of kilometers ridden on a bicycle from a mysql table. My function is generating a multi-dimensional array with Std Class objects. When I try to echo the result in my view file, I get the error : Error: Message: Object of class stdClass could not be converted to string.

I have tried instructions on how to make foreach loops for multi-dimensional arrays in about ten threads, including this one. It appears I need a nested foreach structure, however, I am not understanding how to apply these examples to my code.

//in my controller index function

$weeknumber = array(0, 1, 2, 3, 4, 5, 6, 7);
$bikedata['sums_for_table'] = array_map(array($this->bike_model,
'get_weekly_sum'), $weeknumber);


//in my model

public function get_weekly_sum($when)
{
$this->db->select_sum('distance');
$this->db->where("WEEK (date) = WEEK( current_date ) - $when AND YEAR( date) = YEAR( current_date)");
$query = $this->db->get('bike_stats');

return $query->result();


//in my view (trimmed for simplicity. It's actually a table.)

foreach ($sums_for_table as $sum_array):
foreach ($sum_array as $key => $object):
echo $object;
endforeach;
endforeach;


The function is working correctly in that the array returned by the functions (below, from var_dump) has the info I want in it (the numbers 63.2 etc.) but I'm having trouble understanding how to use the info in this form.

array(8) {
[0]=> array(1) { [0]=> object(stdClass)#23 (1) { ["distance"]=> string(4) "63.2" } }
[1]=> array(1) { [0]=> object(stdClass)#24 (1) { ["distance"]=> string(5) "111.9" } }
[2]=> array(1) { [0]=> object(stdClass)#25 (1) { ["distance"]=> string(4) "48.2" } }
[3]=> array(1) { [0]=> object(stdClass)#26 (1) { ["distance"]=> NULL } }
[4]=> array(1) { [0]=> object(stdClass)#27 (1) { ["distance"]=> string(4) "26.7" } }
[5]=> array(1) { [0]=> object(stdClass)#28 (1) { ["distance"]=> string(4) "42.2" } }
[6]=> array(1) { [0]=> object(stdClass)#29 (1) { ["distance"]=> string(4) "32.3" } }
[7]=> array(1) { [0]=> object(stdClass)#30 (1) { ["distance"]=> string(4) "10.9" } } }


My main question is how I can fix my view file code to properly output the numbers in this array. However, if I could have written my function in a different way to generate a regular associative array I would love to know that too!

Any help would be much appreciated!

Answer

This should do the trick:

   foreach ($sums_for_table as $entry) {
      $distance = $entry[0]->distance;
      echo $distance . ' <br />';
    }

You can access properties of stdClass Objects by using the $object->property syntax.

I don't know how all of your code works because I don't see it all but for a cleaner way you could try:

// controller
$bikedata['sums_for_table'] = [];
$weeknumbers = array(0, 1, 2, 3, 4, 5, 6, 7);
foreach ($weeknumbers as $weeknumber) {
  $weekResult = $this->bike_model->get_weekly_sum($weeknumber);
  $bikedata['sums_for_table'][$weeknumber] = $weekResult[0]->distance;
}

// view
foreach ($sums_for_tables as $weekNumber => $distance) {
  // draw your table here
}
Comments