Trying to understand how to echo from array - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24) +--- Thread: Trying to understand how to echo from array (/showthread.php?tid=64436) |
Trying to understand how to echo from array - doomie22 - 02-19-2016 Hi there, I am having a lot of trouble getting my head around echoing out from an array. I have this in one of my models PHP Code: public function get_social() { and if I print_r it in my view its pulling everything nicely. The problem I am having is to try to understand how to echo out the information, I will be making a while and if thing for it but for not I just want to echo one part of it to know that it works. So say I want to echo out the 'social_name' from it, how do I do it as I cannot seem to find any documentation that tells me. Thanks, RE: Trying to understand how to echo from array - donpwinston - 02-19-2016 $data = get_social(); $this->load->view('some_view', $data); Inside of some_view : <?php print $social_name; ?> The keys in $data are your variables. RE: Trying to understand how to echo from array - doomie22 - 02-19-2016 Thank you for that. I have managed to get to do the basics that I wanted as I didn't realize that I had to wrap it in a foreach, like: Code: <?php foreach ($social as $soc) : ?> Thank you for your time. RE: Trying to understand how to echo from array - Wouter60 - 02-19-2016 Actually, $this->db->query("...")->result() does not return an array, but an object. If you do want an array, use $this->db->query("...")->result_array(); In order to output the results in your view, do this: PHP Code: <?php RE: Trying to understand how to echo from array - skunkbad - 02-19-2016 (02-19-2016, 02:52 PM)Wouter60 Wrote: Actually, $this->db->query("...")->result() does not return an array, but an object. Actually, result() returns an array of objects. row() returns an object for a single row. result_array() returns an array of arrays. row_array() returns an array for a single row. RE: Trying to understand how to echo from array - Wouter60 - 02-19-2016 @skunkbad, you're right, your answer is more acurate. |