![]() |
Amending result_array - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21) +--- Thread: Amending result_array (/showthread.php?tid=56119) Pages:
1
2
|
Amending result_array - El Forum - 11-27-2012 [eluser]CinoGenX[/eluser] Hi @ all In my model I'm trying to amend values in an array generated from a phpbb query before passing back to my controller.... i.e. $sql = "SELECT post_id, post_subject, post_text, bbcode_uid, bbcode_bitfield, bbcode_options FROM phpbb_posts WHERE ......."; $query = $this->db->query($sql); I now want to foreach through the results and change the post_text using the phpbb3 function "generate_text_for_display". Can someone help me out on this please....banging my head on the table on this one. Many thanks. Amending result_array - El Forum - 11-27-2012 [eluser]Aken[/eluser] Code: <?php Amending result_array - El Forum - 11-28-2012 [eluser]bretticus[/eluser] [quote author="Aken" date="1354059587"] Code: <?php Note the ampersand before $row in this example. In case you didn't know, that's called passing a variable by reference instead of by value. It just means that as each element of the original array ($results) is looped through, you are modifying $result's actual array element instead of just assigning $row as a value (or a copy.) Amending result_array - El Forum - 11-28-2012 [eluser]CinoGenX[/eluser] That explains alot. Many thanks Guys, ill give it a try. Im familiar with passing by reference from C++, but thats functions, didnt realise it was used like this in php for a foreach loop. Cheers for the explanation guys. Amending result_array - El Forum - 11-28-2012 [eluser]CinoGenX[/eluser] hmmm having a little problem still. Fatal error: Cannot use object of type stdClass as array... This is what i have so far. Code: // in Model I think i need the $data['recent_posts'] to be result_array as $data has arrays inside array or something? Help?! ![]() Thanks. Amending result_array - El Forum - 11-28-2012 [eluser]Aken[/eluser] Chances are your view is expecting an array when looping through your recent posts. You need to change it to object syntax or use result_array() and change your get_recent_posts() to reflect that as well. Amending result_array - El Forum - 11-29-2012 [eluser]CinoGenX[/eluser] Thanks for reply Aken. Code: Here is the view calls in controller : And this is the view: Code: <?php foreach ($recent_posts as $post_item): ?> So how am i referrring to it as object, or how should i change to results_array()? Should i change $data in the controller (further up) to results_array or.....? Amending result_array - El Forum - 11-29-2012 [eluser]Aken[/eluser] The problem is you aren't referring to it like an object. You're fetching array indexes in your view, when you should be fetching object properties. Each $post_item is an object as your code stands. Amending result_array - El Forum - 11-29-2012 [eluser]CinoGenX[/eluser] Thanks Aken!!! Updated view to call object properties (i.e. $post_item->topic_id etc) and it works. Just out of interest, how would i have got it to work as array instead of object? I tried just changing: $results = $query->result(); to $results = $query->result_array(); ....in the home_Model. But that didn't work. What else would i have had to do? Thanks again. Amending result_array - El Forum - 11-29-2012 [eluser]Aken[/eluser] Using result_array() should've worked fine for your view code, but you would've needed to update your get_recent_posts() model method, also, so that your foreach() loop there used array notation also. |