Welcome Guest, Not a member yet? Register   Sign In
Dynamic content in the template file
#1

[eluser]Kamy[/eluser]
Hi:

I'm running a site on CI 2.0. All my data are inserted in a template.php file.

I can extract and display data from my controllers into views within the template file without any issues.

My template has a content and sidemenu section. The content area gets filled with Data from the controller.

What I'd like to do now, is to display some data from the DB on the sidemenu section so it gets displayed on all pages.

Not sure how to handle this, obviously I need a controller to extract data, but how could it be on all pages?

Thanks in advance for your help
#2

[eluser]haileCI[/eluser]
in all view :
<div id='sidemenu'>
echo $sidemenu=site_url('youcontroller/method');
</div>

class Youcontroller extentds CI_Controller{
function method()
{
//get what you want
$this->load->view('sidemenu',$data)
}
}

i am not sure it can work, but you can try
#3

[eluser]Kamy[/eluser]
Thanks but that doesn't work, site_url will output the URL of the specified page....

I have a controller Sidestuff, which outputs data into Sidestuff view page... I tried including that file in the sidemenu, but it didn't work either
#4

[eluser]InsiteFX[/eluser]
Code:
$data['sidemenu'] = 'Whatever';
$this->load->vars($data);  // no need to pass in your load->view, makes $data variables global to all views!

//get what you want
$this->load->view('load_your_template_view');

InsiteFX
#5

[eluser]Kamy[/eluser]
thanks, now I'm getting a Undefined variable: sidemenu error.

Here's my setup:

controller:
Code:
public function index()
    {
        $data['page_title'] = 'Your title';
        $this->load->vars($data);
        $this->load->view('test_view');  
    }

View:
Code:
&lt;?= $sidemenu;?&gt;
<p>Hi there ! </p>

Template:
Code:
&lt;? $this->load->view('test_view');?&gt;

The error:
Code:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: sidemenu
Filename: views/test_view.php
Line Number: 2

It does display the "Hi there !" line below it....

any ideas why?
#6

[eluser]InsiteFX[/eluser]
Use full PHP tags &lt;?php not short tags.

Code:
&lt;?php echo $sidemenu;?&gt;
and
&lt;?php $this->load->view('test_view');?&gt;

InsiteFX
#7

[eluser]Kamy[/eluser]
that made no difference and it shouldn't !

Short tags are enabled on my site and work within the rest of the pages !
#8

[eluser]InsiteFX[/eluser]
In your Controller you need to set:
Code:
$data['sidemenu'] = 'Something';

If this is not set and passed you will get that error!

InsiteFX
#9

[eluser]InsiteFX[/eluser]
Also show your Controller for Sidestuff

InsiteFX
#10

[eluser]Kamy[/eluser]
It is set ! I noticed here in the forum I made the mistake of not setting it.

Code:
public function index()
    {
    $data['sidemenu'] = 'Whatever';
    $this->load->vars($data);
    $this->load->view('test_view');  
    }


When I browse to that page directly, like : http://localhost/index.php/test
The page and the data display properly.

But when I try to display $data within the template file it fails. It doesn't get recognized!




Theme © iAndrew 2016 - Forum software by © MyBB