CodeIgniter Forums
Add to object in loop - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Add to object in loop (/thread-33929.html)



Add to object in loop - El Forum - 09-13-2010

[eluser]frist44[/eluser]
This maybe be a stupid question, but I'm going through a look and doing some calculations and I'm having trouble adding the final result to the object.

Code:
foreach ($parts as $part) {
            $open = new Mo();
            $open->select_sum('quantity')->where('ordertype', 'FG')->where('part_id', $part->id)->group_by('part_id')->get();
            $part->open = $open->quantity;
            //echo $part->open;
        }

If I echo $open->quantity;, I get the result I'm after, but not from $part->open;. I'd like it to be part of the object, so when it's passed to the view, it's available for display.

What am I doing wrong?


Add to object in loop - El Forum - 09-13-2010

[eluser]danmontgomery[/eluser]
Foreach creates a copy, use & to assign reference rather than copying the value.

Code:
foreach($parts as &$part) {



Add to object in loop - El Forum - 09-13-2010

[eluser]frist44[/eluser]
I should've add that $parts is an object, so that doesn't seem to work.


Add to object in loop - El Forum - 09-14-2010

[eluser]frist44[/eluser]
nothing?