• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trying to understand how to echo from array

#1
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() {
 
      $this->db->select('*');
 
      $this->db->from('social');
 
      $query $this->db->get();
 
      return $query->result();
 
  

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,
Reply

#2
$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
Reply

#3
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) : ?>
<?php echo $soc->social_name; ?>  
<?php endforeach; ?>

Thank you for your time.
Reply

#4
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
foreach ($social as $soc) {
  echo 
$soc['social_name'];
}
?>
Reply

#5
(02-19-2016, 02:52 PM)Wouter60 Wrote: 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
foreach ($social as $soc) {
 
 echo $soc['social_name'];
}
?>

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.
Reply

#6
@Skunkbad, you're right, your answer is more acurate.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.