CodeIgniter Forums

Full Version: Trouble with system vars
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]The Beginner[/eluser]
Hi all,
I am having a great deal of difficulty with my first major CI app. I have posted on a couple of other topics and got a lot of help. The problem i am having nos is $base_url() is not available in my views. It was when i started and then for seamingly no reason I started getting errors because I could no longer see or get $base_url(); also I cannot pass $data from one view to another.
I got around the $base_url thing by defining a constant with the current base_url but I still need to figure out why it is not available and I think when I get that I will get the $data passing again.
I have tried all the obvious stuff like config['base_url'] = in config.php.

Anyone have any ideas.
Thanks,
Del
:-S

El Forum

[eluser]jedd[/eluser]
Is the URL helper definitely loaded?

Does it work from within a controller?

Do other functions from the URL helper work?

El Forum

[eluser]The Beginner[/eluser]
Thanks for the reply. I wnet back and checked to see if other functions from the url helper were available and I called "current_url()"; just like that and it worked. I tried base_url(); again and it worked. I guess I ahve been placing a $ in front of it when I should not. Shows how much i know about PHP.
I still have the problem of data not passing to a view.
I have in my views folder a file called Container.php and in it I have the following..:
Code:
<html>
<head>
<title><?php echo $title; ?></title>
<?php
    $this->load->view('wpMeta');
?>
</head>
<body>
<?php
    $this->load->view('wpHeader');
    $this->load->view('wpMenu');
//         $this->load->view('sidebar', $data);
//         $this->load->view($page, $data);
  //        $this->load->view('footer');
?>
</body>
</html>
The wpHeader loads fine and displays its information exactly as i would expect. wpMenu throws the following error and will not display any information.
A PHP Error was encountered
Severity: Notice

Message: Undefined variable: my_menu

Filename: views/wpMenu.php

Line Number: 7

In the controller they both have $data passed as a paramiter, so I am really confiused as to why it fails to pass the data.
Any Ideas?
Thanks,
Del

El Forum

[eluser]jedd[/eluser]
Yeah, this stuff is fairly well explained in the documentation, and again in the oodles of tutorials.

The way you're doing this is a bit confusing, to my mind. Calling views within views is not something that sits well with me. Check out [url="http://codeigniter.com/wiki/Header_and_Footer_and_Menu_on_every_page_-_jedd/"]this page[/url] in the wiki - it's how I repeat lumps of code throughout my site, but in passing it'll show you another way you can do this - by calling view partials and storing the results into a variable. Knowing both approaches means you can work out what way sits better with the way you work/think.

In any case, you still need to put variables into an array, and call that with your view loads - even if you're loading that view from within another view. You can look at load_vars() (?) - which will get this stuff everywhere for you, as an alternative, and might work better for you if you stick with the hierarchical view-within-a-view method.

El Forum

[eluser]The Beginner[/eluser]
Hi Jedd,
Thanks for the helpful information. I tried the template and it worked great when I created the simple about view shown in the instructions. When i tried a view that needed data passed to it I got the same error I had before, $header_logo not defined.

I think I have some fundimintal setup problem, maybe with my server, although I uploaded it to my remote site and got the same error. It seams like anytime I try to pass data to a view it just does not happen.
Any suggestions would be greatly appreciated.
Thanks,
Del

El Forum

[eluser]steelaz[/eluser]
I assume you have variable $my_menu in views/wpMenu.php line 7.

In your controller, do this:
Code:
// Pass $my_menu var to wpMenu view and load it into $wpMenu_view var
$wpMenu_data['my_menu'] = 'My menu variable';
$wpMenu_view = $this->load->view('wpMenu', $wpMenu_data, true);

// Load more partial view here
$wpMeta_view = $this->load->view('wpMeta', $wpMeta_data, true);
$wpHeader_view = $this->load->view('wpHeader', $wpHeader_data, true);

// Load main view and pass partial views as variables (among other stuff)
$data['title'] = 'My Title';
$data['wpMenu'] = $wpMenu_view;
$data['wpMeta'] = $wpMeta_view;
$data['wpHeader'] = $wpHeader_view;
$this->load->view('template', $data);

In your main template:
Code:
<html>
<head>
    <title><?= $title; ?></title>
    <?= $wpMeta; ?>
</head>
<body>
    <?= $wpHeader; ?>
    <?= $wpMenu; ?>
</body>
</html>

El Forum

[eluser]jedd[/eluser]
[quote author="The Beginner" date="1254085721"]
Thanks for the helpful information. I tried the template and it worked great when I created the simple about view shown in the instructions. When i tried a view that needed data passed to it I got the same error I had before, $header_logo not defined.
[/quote]

Show the code sections for:
o controller, where you set your header_logo variable,
o controller, where you call the view,
o view, where you use the header_logo variable.

El Forum

[eluser]The Beginner[/eluser]
Quote:Show the code sections for:
o controller, where you set your header_logo variable,
o controller, where you call the view,
o view, where you use the header_logo variable.

Ok, first the controller loading data:

Code:
$this->db->where('wp_header_active', '1');
$querydata = $this->db->get('wp_header_data');
if ($querydata->num_rows() > 0):
  {
     foreach($querydata->result() as $row):
      {    
       $data['header_logo'] = snappy_image($row->wp_header_logo, 'My Picture', 'somestyle');
       $data['title'] = $row->wp_header_name;
       $data['site_name'] = $row->wp_header_name;
      }
     endforeach;
   }
endif;
$querydata->free_result();
$this->template->set('title', $data['title']);
$this->template->set('header_logo',$data['header_logo']);
// Load the template
$this->template->load('template', 'wpHeader');

This is my template.php, the plan is to load my header view, menu view contents and then my footer.
Code:
<html>
<head>
    <title><?php echo $title; ?></title>
</head>
<body>
    <div id="header">&lt;?php echo 'header'; ?&gt;</div>
    <div id="menu">&lt;?php echo 'menu'; ?&gt;</div>
    <div id="contents">&lt;?php echo $contents; ?&gt;</div>
    <div id="footer">&lt;?php echo 'footer'; ?&gt;</div>
&lt;/body&gt;
&lt;/html&gt;

Now the view:
Code:
&lt;?php
   echo $header_logo;
?&gt;

And that is it...... the error I get is :

A PHP Error was encountered
Severity: Notice

Message: Undefined variable: header_logo

Filename: views/wpHeader.php

Line Number: 2

El Forum

[eluser]jedd[/eluser]
Quote:
Code:
$this->template->set('title', $data['title']);

Oh, if you're using a template library now, I can't help you - never used them, and don't know how they work. Maybe changing the post subject to something about templates, rather than system variables, might attract the attention of template users.