1

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!

1 Answer 1

1

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
}
Sign up to request clarification or add additional context in comments.

2 Comments

Awesome, Gries. Thank you! This works perfectly now. I had tried something along the lines of $object->property but I did not know to specify the key of zero. My table is now displaying perfectly, so now, on to the next step of making this into a bar graph! And thank you also for the cleaner code. I'm going to play with this and see how it works; looks like a good way to make another use of $object->property to help learn it. Those of us who are learning on our own really appreciate thoughtful answers such as yours!
The alternate code initially generated an error message of "trying to get property of nonobject." Then I added the key of zero and it worked great! So the last line of the controller code becomes: $bikedata['sums_for_table'][$weeknumber] = $weekResult[0]->distance;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.