Welcome Guest, Not a member yet? Register   Sign In
Passing data to views called from another view
#1

[eluser]bleu[/eluser]
i have my website structure in the following fashion

My controller has
Code:
function page2($page) {
  $data['page_title'] = 'Your title for content 2';

$data['news'] = $this->news_model->get_news($page);
  
  $this->load->view('container',$data);
}


My container view has


Code:
<html>
<head>
<title>hi</title>
</head>
<body>
<?php
$this->load->view('header');
$this->load->view('menu');
$this->load->view($page);
$this->load->view('footer');
?>
</body>
</html>


Now my header and menu will have certain data that will be changing and coming from the database or being calculated.

e.g My menu will display more menu items( which it will get from the database) as per the page selected.

e.g.2 My header will have a counter which will show what work the user has done(and will bring some data from the database)(kind of like no of items selected in a shopping cart and total amount. which it may pick up from database) and even his login status. with a login form and a welcome message when he is logged in

How can I pass that data?


#2

[eluser]achilleusrage[/eluser]
You can pass all your data in the $data var that you set when you load the 'container' view.

For example, I do something similar to what you are doing:

Controller:
Code:
function index()
{
   $data["PAGE_TITLE"] = "This is the HTML title of my page";
   $data["PAGE_CONTENT"] = "This is the content of my page.";
   $this->load->view("container",$data);
}

Views:

container view:
Code:
$this->load->view("header");

print $PAGE_CONTENT;

$this->load->view("footer");

header view:

<html>
<head>
<title><?php print $PAGE_TITLE; ?></title>
</head>
<body>
[/code]

Hope this helps!

#3

[eluser]bleu[/eluser]
Thanks but how will I get my menu each time the user goes to a page

e.g My menu will display more menu items( which it will get from the database) as per the page selected.


I need to put my query and controller to get my menu in one place, but run it each time my user views a page and try to fetch that pages related menu
#4

[eluser]Matalina[/eluser]
[quote author="achilleusrage" date="1331044882"]You can pass all your data in the $data var that you set when you load the
container view:
Code:
$this->load->view("header");

print $PAGE_CONTENT;

$this->load->view("footer");

header view:

<html>
<head>
<title><?php print $PAGE_TITLE; ?></title>
</head>
<body>
[/code]

[/quote]

How did the header view get $PAGE_TITLE you didn't pass it to the view?

This is how I would do it.
Code:
$data['PAGE_TITLE'] = $PAGE_TITLE;
$this->load->view("header",$data);

print $PAGE_CONTENT;

$this->load->view("footer");
#5

[eluser]achilleusrage[/eluser]
[quote author="Matalina" date="1331046608"]
How did the header view get $PAGE_TITLE you didn’t pass it to the view?
[/quote]

Admittedly, I don't know the specifics, but for long as I've used CI (back to 2006) child (sub) views have always had access to the parent view's variables in the fashion I've described.
#6

[eluser]CroNiX[/eluser]
Code:
$this->load_vars($data);
Will make it globally available to all views. If you do that, you don't need to pass $data to the view.

Instead of:
Code:
$this->load->view('file', $data);
You would
Code:
$this->load_vars($data);
$this->load->view('file');  //variables in $data will be available to any view file
#7

[eluser]Matalina[/eluser]
nice to know. Thanks.
#8

[eluser]bleu[/eluser]
[quote author="CroNiX" date="1331057876"]
Code:
$this->load_vars($data);
Will make it globally available to all views. If you do that, you don't need to pass $data to the view.

Instead of:
Code:
$this->load->view('file', $data);
You would
Code:
$this->load_vars($data);
$this->load->view('file');  //variables in $data will be available to any view file
[/quote]


Thanks but how will I get my menu each time the user goes to a page

e.g My menu will display more menu items( which it will get from the database) as per the page selected.


I need to put my query and controller to get my menu in one place, but run it each time my user views a page and try to fetch that pages related menu
#9

[eluser]achilleusrage[/eluser]
Sorry if I'm misunderstanding but I think you will want to select the data for your menu in your controller. Then pass it to your view in the $data var. In looking at your original sample code, you would do something like this:

Code:
function page2($page) {
  $data['page_title'] = 'Your title for content 2';

$data['news'] = $this->news_model->get_news($page);

$data['menu'] = $this->some_function_that_returns_menu_data($page);
  
  $this->load->view('container',$data);
}




Theme © iAndrew 2016 - Forum software by © MyBB