Instead of repetitive data pasting into my controllers, I am looking to create a helper called “navigation” this will be used for every controller and load in the navigation subviews.
Currently, in EVERY controller I am adding the following code.
// Get Subcategories
$subcat1 = $this->Categories_model->get_artstyles();
$subcat2 = $this->Categories_model->get_artsubjects();
$subcat3 = $this->Categories_model->get_media();
$subcat4 = $this->Categories_model->get_photography();
$this->view_data['sub_cat1'] = $subcat1->result();
$this->view_data['sub_cat2'] = $subcat2->result();
$this->view_data['sub_cat3'] = $subcat3->result();
$this->view_data['sub_cat4'] = $subcat4->result();
So what I would like to do is trim this down and load that data as a helper. So ultimately all I do in each controller is load the helper.
If you feel this is the wrong way to do it, please advise as I’m still a little unsure on CI and its ways.
Those 4 subcat variables are all wanting to be global variables.
If this data is relevant to all your controllers, you should extend the CI_Controller like this:
Place this file inside the
coredirectory and extend all your controllers like this:Read more here.
In the helper function:
}
EDIT:
In order to load the variable into the view:
In the view you can access it like this:
$view_data.