1

I have this question: how to load function in codeIgniter in every page and get data or variables from it to show in view?
I have many controllers and each one has many function. Each function load master view as a template and load its own view. Master view has dynamic data I push it from database.
So I need in master view to show some data from database automatically. How to do it?

Tareq Nassry
  • 101
  • 1
  • 1
  • 7
  • Just refer this post http://stackoverflow.com/questions/15564329/cannot-fetch-results-from-mysql/15564395#15564395. You may get some idea – Edwin Alex Mar 22 '13 at 07:02
  • Use [Hooks](http://ellislab.com/codeigniter/user-guide/general/hooks.html) – Vaibhav Mar 22 '13 at 09:10

3 Answers3

0

You can use this. change it according to your

function index()
{
    $data['list']       =   $this->some_model->show_data();
    $data1['content']   =   $this->load->view('some_view',$data,true);
    $this->load->view('template',$data1);
}

content is in your main template where u will pass your data to show in template

Azam Alvi
  • 6,918
  • 8
  • 62
  • 89
  • I have a lot of functions and controllers so I think it's wrong way to load it like you said **Azam Alvi**. I need to autoload function to get data from database. – Tareq Nassry Mar 22 '13 at 07:11
  • if u want to auto load then make a library and load into your autoload file – Azam Alvi Mar 22 '13 at 07:13
  • u want to say that u dont know how to do this? – Azam Alvi Mar 22 '13 at 07:15
  • I need to get data_0 (for example) from database to show in every page, and sure other data_x shown in each page. How to exec function automatically and get data without call thim? – Tareq Nassry Mar 22 '13 at 07:18
0

I am not sure what exactly you ask but if you want a function to be loaded automatically to any controller

then create a Mycontroller.php in application/core

class Mycontroller extends CI_Controller
{
    function __construct()
    {
        parent::__construct();
        // here goes your function
    }
}

and then extend every controller you have to Mycontroller instead of CI_Controller

Nikitas
  • 1,013
  • 13
  • 27
  • Ok lets give it one more try: If you just need to show data from database to a VIEW you can use $CI =& get_instance(); see here http://stackoverflow.com/questions/2075369/how-to-query-a-database-from-view-codeigniter – Nikitas Mar 22 '13 at 07:38
0

if you want to use your function inside views. use CI Helpers

Codeigniter Helpers

create your helper function

function get_data() {

 // and use CI instance
 $CI = & get_instance();

 // now you can call your models, libraries. etc. in the helper
 $data = $CI->your_model->your_model_funcion();

 return $data;
}

and you can call your helper function in controllers, views, models. etc...

get_data();
Dino Babu
  • 5,814
  • 3
  • 24
  • 33