Welcome Guest, Not a member yet? Register   Sign In
Making data array available for all functions in the same controller
#1

[eluser]millo[/eluser]
Hi,

I hope I'm not asking a silly question here.

I've got a controller and within it I have many functions, each of which have to load in the same data arrays that are used for building the navigation menu.

So for example, in my simple page function in the main.php controller you'll see how I load in the lines 4 and 5 which are used for the menu construction:

Code:
function page() {
    $this->load->helper('markdown');
    // all_sections_query and page_list_query used for constructing menu
    $data['all_sections_query'] = $this->Section_model->get_all_sections();
    $data['page_list_query'] = $this->Page_model->get_page_list();
    // load main content
    $page_id = $this->uri->segment(3);
    $data['page_contents_query'] = $this->Page_model->get_page_contents($page_id);
    $this->load->view('main/page', $data);
}

Rather than have to instantiate them in every function, is there a way to instantiate them globally for the whole controller since they are used in every function anyway?
#2

[eluser]Michael Wales[/eluser]
You can do it in the constructor. It's only going to save you code retyping though - the database will still be queried upon each page load.
#3

[eluser]millo[/eluser]
Thanks WalesMD,

That's what I thought, but I've tried that and it doesn't work.

You see, in the view that I'm loading in for that code I've got a header and footer include ...

Code:
<?php $this->load->view('main/includes/header'); ?>

<?php $page_row = $page_contents_query->row(); ?>
<h1>&lt;?php echo $page_row->page_title; ?&gt;</h1>
&lt;?php echo markdown($page_row->text); ?&gt;

&lt;?php $this->load->view('main/includes/footer'); ?&gt;

... it's in the header where I'm calling on this data and for some reason it doesn't like it unless I instantiate it directly in the function (as opposed to globally).

This is the code in the header ...

Code:
<ul class="l1">
&lt;?php foreach($all_sections_query->result() as $section_row) { ?&gt;
   <li class="l1"><strong>&lt;?php echo $section_row->title ?&gt;</strong></li>
      <ul class="l2">
      &lt;?php foreach($page_list_query->result() as $page_row) { ?&gt;
      &lt;?php if ($section_row->section_id == $page_row->section_id) { ?&gt;
         <li class="l2"><a href="/main/page/&lt;?php echo $page_row->page_id; ?&gt;">&lt;?php echo $page_row->menu_title; ?&gt;</a></li>
      &lt;?php } ?&gt;
      &lt;?php } ?&gt;
      </ul>
&lt;?php } ?&gt;
</ul>

Any idea why my header that's included in the view doesn't like it?

This is the error I get:


Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined variable: all_sections_query

Filename: includes/header.php

Undecided
#4

[eluser]Michael Wales[/eluser]
I think you may have to make the variable a global - since you are trying to transfer data from one function to another. I'm not very sure - I rarely use constructors/destructors.
#5

[eluser]millo[/eluser]
OK, thanks for the guidance anyway :-)
#6

[eluser]alpar[/eluser]
I think that you have to add a parameter to the included views. I did it that way and it worked, so you could have something like

&lt;?php $this->load->view('main/includes/header',array('all_sections_query' => $all_sections_query));?&gt;

not very clear, but you might come up with a cleaner way to write it, the idea is to pass the variable forward, i am not sure if CI does that by itself, i always pass the variable forward.
#7

[eluser]Sean Murphy[/eluser]
You could set an instance variable in your controller (similar to what has been discussed, but slightly different). So in your constructor you'd have:
Code:
$this->all_sections_query = 'foo';

And in your view you'd access the variable using $this->all_sections_query instead of $all_sections_query. This will allow you to access the same variable throughout your controller and view without having to explicitly pass the variable to the view() method. Hope it works.
#8

[eluser]Skulls[/eluser]
Code:
&lt;?php
class Welcome extends Controller {
var $giving_data;
    function Welcome()
    {
        parent::Controller();    
        $this->load->model('Section_model');
        $this->load->model('Page_model');
        $all_sections_query = $this->Section_model->get_all_sections();
        $page_list_query = $this->Page_model->get_page_list();
        $page_contents_query=$this->Page_model->get_page_contents($this->uri->segment(3));
        $this->giving_data=array('all_sections_query'=>$all_sections_query,
                             'page_list_query'=>$page_list_query,
                             'page_contents_query'=>$page_contents_query);
    }
    
    function index()
    {
        $this->load->view('welcome_message');
    }
    function page()
    {
     $this->load->helper('markdown');
  
   $data['something']='bla bla bla';
  //loading the $data here is not needed if u don;t have other data to load then the one in the constructor
   $this->load->view('main/page', $data);
  }

}

then the in main/page view

Code:
<ul class="l1">
&lt;?php
$all_sections_query=$this->giving_data['all_sections_query'];
//etc page_list_query or other stuff defined in the controller
foreach($all_sections_query->result() as $section_row) { ?&gt;
   <li class="l1"><strong>&lt;?php echo $section_row->title ?&gt;</strong></li>
      <ul class="l2">
      &lt;?php foreach($page_list_query->result() as $page_row) { ?&gt;
      &lt;?php if ($section_row->section_id == $page_row->section_id) { ?&gt;
         <li class="l2"><a href="/main/page/&lt;?php echo $page_row->page_id; ?&gt;">&lt;?php echo $page_row->menu_title; ?&gt;</a></li>
      &lt;?php } ?&gt;
      &lt;?php } ?&gt;
      </ul>
&lt;?php } ?&gt;
</ul>
?&gt;

Of course ... before the foreach u will have to put some conditions if the data is !empty ... just for the non-error's sake Smile




Theme © iAndrew 2016 - Forum software by © MyBB