Problems getting foreach loop to display data - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Problems getting foreach loop to display data (/showthread.php?tid=56631) Pages:
1
2
|
Problems getting foreach loop to display data - El Forum - 01-08-2013 [eluser]douglasbrownca01[/eluser] I'm getting a Trying to get property of non-object error in my for each statement My Model: Code: public function get_accessories($limit) { My controller Code: $results = $this->accessories_model->get_accessories($limit); My view Code: <?php foreach($fingerguards as $fingerguard):?> The two data sets from the $results variable are working fine and I'm able to display the rows. But not with the fingerguards variable. What I'm I missing? Problems getting foreach loop to display data - El Forum - 01-08-2013 [eluser]Rowan Wilson[/eluser] Does this work? Code: $results = $this->accessories_model->get_accessories($limit); Probably need to rethink the $fingerguards_result variable name but it'll do for testing. P.s. you're missing an A from capcitors. Unless that's deliberate ;-) Problems getting foreach loop to display data - El Forum - 01-08-2013 [eluser]douglasbrownca01[/eluser] Thanks for your reply. No, unfortunately, it doesn't. I get an empty response and a blank page. I tested the query in the database for fingerguards and it works fine. The "A" in capacitors was a mistake but carried all the way through so it wasn't an issue. If I understand this right, I'm creating an array and sending the results to the view, but it is like it's not bringing back the rows. Problems getting foreach loop to display data - El Forum - 01-08-2013 [eluser]Rowan Wilson[/eluser] Out of interest, if you var_dump your $fingerguards variable in your view are your results there? Problems getting foreach loop to display data - El Forum - 01-08-2013 [eluser]douglasbrownca01[/eluser] Yes, I'm getting this result in my view from my controller Code: ["fingerguards"]=> Repeats ll times... Problems getting foreach loop to display data - El Forum - 01-08-2013 [eluser]Rowan Wilson[/eluser] ok cool, your result is there so that's good. I just wanted to make sure everything came through the same as your $results query as you said that worked. Revert to how you previously were passing your fingerguard variable straight into data. Code: $data['fingerguards'] = $this->accessories_model->get_fingerguards($limit); The non-object error is probably because, in your view you are trying to echo out item code as an array rather than an object like all the others. Here: Code: <?php echo $fingerguard['item_code'] ?> Code: <?php echo $fingerguard->item_code ?> Problems getting foreach loop to display data - El Forum - 01-08-2013 [eluser]douglasbrownca01[/eluser] Right. This is the way I had it originally. I that snippit was an me taking another tact. I've got Code: <?php foreach($fingerguards as $fingerguard):?> each variable I'm trying to bring back looks like this: Code: <?php echo $fingerguard->item_code ?> I'm still getting the property of non-object error in each instance. I'm new to code ignitor, but it seems like it's how the data is being stored in the results array and that the for each statement can't interpet it correctly. From looking at other code examples, this should work. I have to other statements in my model that are bring back data perfectly. The whole model is listed above. Thanks for the replys, would appreciate any insight you might have. Problems getting foreach loop to display data - El Forum - 01-08-2013 [eluser]Rowan Wilson[/eluser] Without seeing the var_dump from your last change I would guess you need to access the results as: Code: $fingerguard['0']->item_code etc. Problems getting foreach loop to display data - El Forum - 01-08-2013 [eluser]douglasbrownca01[/eluser] Progress --- that pulls out on instance of the array and displays correctly. How do I loop through and get them all? The array dump is exactly as above...I'm getting the same dump. Problems getting foreach loop to display data - El Forum - 01-08-2013 [eluser]Rowan Wilson[/eluser] You were on the right track: Code: <?php foreach($fingerguards as $fingerguard):?> I've spaced it out a little so that I could read the conditionals. |