CodeIgniter Forums
load header with database information - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: load header with database information (/showthread.php?tid=17913)



load header with database information - El Forum - 04-19-2009

[eluser]sw[/eluser]
I have header/footer views which I include in all my content views.

ie.
header
content (changes depending on the controller)
footer

The problem is that header view needs to be passed information from the database.
This forces me to repeat the same steps in every controller to load the header view with that information and then the content.
Is there a better way to do it ?


Code:
...
$this->load->model('model');
$query = Model::DBQuery();
$info = array( 'query' => $query );
$this->load->view('header_view', $info);
$this->load->view('content_view', $data);
$this->load->view('footer_view');



load header with database information - El Forum - 04-20-2009

[eluser]Dam1an[/eluser]
If he header info is constant, you could use one of the partial cacheing libraries which seem to be floating round the forum lately

Alternativly, you could have the header queries done in the constructor (I'd recommend making you're own controller and extending that) and have all the header queries in there
You can then just pass the info array (which must be a class variable, protected if you're using PHP5+)

MY_Controller
Code:
MY_Controller extends Controller {
  protected $info;

  function __construct() {
    parent::Controller();
    $this->info['something'] = ... db logic ...
  }
}

Someclass
Code:
class Someclass extends MY_Controller {
  function index() {
    $this->load->view('header', $this->info);
  }
}



load header with database information - El Forum - 04-20-2009

[eluser]sw[/eluser]
Not constant.

Thanks, I'll try that.