Trying to understand how to echo from array |
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,
$data = get_social();
$this->load->view('some_view', $data); Inside of some_view : <?php print $social_name; ?> The keys in $data are your variables.
Simpler is always better
02-19-2016, 10:54 AM
(This post was last modified: 02-19-2016, 12:27 PM by doomie22. Edit Reason: Found solution so posted for others. )
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.
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 (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. |
Welcome Guest, Not a member yet? Register Sign In |