Welcome Guest, Not a member yet? Register   Sign In
Selecting data from database in footer
#1

[eluser]ThatsJustMe[/eluser]
Hello,

So basically, I would like to find out what is the best way to select out the data and display them in footer.php file, on each page?

Basically footer.php file on all pages are the same, and it's included with

Code:
$this->load->view('includes/footer');

Is there any easy way to include the information from database (latest 5 users (I know how to get out the data in CI)) into footer? Basically currently I have only one idea - write in each controller new function, which will add $data variable with all information from database to footer view, and then write it inside footer.php view file.

Any suggestions?

Best regards,
ThatsJustMe
#2

[eluser]csotelo[/eluser]
I happen to create a helper that takes charge of the query and returns the array with the desired data. And in the view using the helper as any other function and with a foreach sample data.
#3

[eluser]ThatsJustMe[/eluser]
Hello,

Thanks!
Could you show an example please?

Did it myself Wink! Thank you for hint very much!

Here is the code if someone needs something similar -

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

if (!function_exists('latest_users'))
{
    function latest_users()
    {        
      $ci = get_instance();
      $ci->load->database();                      
      $ci->db->order_by("id", "desc");
      $ci->db->limit(5);
      $query = $ci->db->get('users');
      $data =array();
      foreach($query->result_array() as $orders)
      {
         $data[] = $orders;
      }
      return $data;
    }  
}

?>

It's a helper file.

Best regards,
ThatsJustMe
#4

[eluser]csotelo[/eluser]
application/helpers/example_helper.php :

Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

if ( ! function_exists('get_last_users'))
{
   function get_last_users()
   {
       $CI =& get_instance();
       $query = $CI->db->query("SELECT * FROM users ORDER BY login_date DESC LIMIT 5");
       return $query->result();
   }
}

in your footer_view.php :

Code:
<?php if(get_last_users(): ?>
<?php foreach(get_last_users() as $row): ?>

&lt;?php echo $row->username . "<br />"; ?&gt;

&lt;?php endforeach ?&gt;
&lt;?php endif ?&gt;

load new helper in autoload config.




Theme © iAndrew 2016 - Forum software by © MyBB