Part of the EllisLab Network
   
 
Making data array available for all functions in the same controller
Posted: 16 August 2007 04:08 PM   [ Ignore ]  
Summer Student
Total Posts:  16
Joined  05-29-2007

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:

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?

Profile
 
 
Posted: 16 August 2007 04:11 PM   [ Ignore ]   [ # 1 ]  
Moderator
Avatar
RankRankRankRank
Total Posts:  1827
Joined  07-30-2007

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.

 Signature 

MichaelWales.com

Profile
 
 
Posted: 16 August 2007 04:25 PM   [ Ignore ]   [ # 2 ]  
Summer Student
Total Posts:  16
Joined  05-29-2007

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 ...

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

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

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

... 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 ...

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

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

This is the error I get:

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: all_sections_query

Filename: includes/header.php

hmmm

Profile
 
 
Posted: 16 August 2007 04:32 PM   [ Ignore ]   [ # 3 ]  
Moderator
Avatar
RankRankRankRank
Total Posts:  1827
Joined  07-30-2007

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.

 Signature 

MichaelWales.com

Profile
 
 
Posted: 16 August 2007 04:35 PM   [ Ignore ]   [ # 4 ]  
Summer Student
Total Posts:  16
Joined  05-29-2007

OK, thanks for the guidance anyway grin

Profile
 
 
Posted: 17 August 2007 11:26 AM   [ Ignore ]   [ # 5 ]  
Lab Assistant
RankRank
Total Posts:  133
Joined  07-17-2007

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
 
  <?php $this->load->view(‘main/includes/header’,array(‘all_sections_query’ => $all_sections_query));?>

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.

 Signature 

Computers are incredibly fast, accurate and stupid. Human beings are incredibly slow, inaccurate and brilliant. Together they are powerful beyond imagination.

                                    A. Einstein

Profile
 
 
Posted: 17 August 2007 11:47 AM   [ Ignore ]   [ # 6 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  102
Joined  07-23-2007

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:

$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.

 Signature 

IamSeanMurphy.com | Statiksoft.com - Custom software development, expert advice

Profile
 
 
Posted: 17 August 2007 12:07 PM   [ Ignore ]   [ # 7 ]  
Grad Student
Avatar
Rank
Total Posts:  71
Joined  10-03-2006

<?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

<ul class="l1">
<?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) { ?>
   
<li class="l1"><strong><?php echo $section_row->title ?></strong></li>
      <
ul class="l2">
      
<?php foreach($page_list_query->result() as $page_row) { ?>
      <?php
if ($section_row->section_id == $page_row->section_id) { ?>
         
<li class="l2"><a href="/main/page/<?php echo $page_row->page_id; ?>"><?php echo $page_row->menu_title; ?></a></li>
      
<?php } ?>
      <?php } ?>
      
</ul>
<?php } ?>
</ul>
?>

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

 Signature 

Stupid people do stupid things, smart people outsmart each otherSOAD

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 719, on June 06, 2008 10:16 AM
Total Registered Members: 64453 Total Logged-in Users: 15
Total Topics: 80961 Total Anonymous Users: 0
Total Replies: 435692 Total Guests: 194
Total Posts: 516653    
Members ( View Memberlist )