Passing data to views called from another view - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Passing data to views called from another view (/showthread.php?tid=49892) |
Passing data to views called from another view - El Forum - 03-06-2012 [eluser]bleu[/eluser] i have my website structure in the following fashion My controller has Code: function page2($page) { My container view has Code: <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? Passing data to views called from another view - El Forum - 03-06-2012 [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() Views: container view: Code: $this->load->view("header"); header view: <html> <head> <title><?php print $PAGE_TITLE; ?></title> </head> <body> [/code] Hope this helps! Passing data to views called from another view - El Forum - 03-06-2012 [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 Passing data to views called from another view - El Forum - 03-06-2012 [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"); 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; Passing data to views called from another view - El Forum - 03-06-2012 [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. Passing data to views called from another view - El Forum - 03-06-2012 [eluser]CroNiX[/eluser] Code: $this->load_vars($data); Instead of: Code: $this->load->view('file', $data); Code: $this->load_vars($data); Passing data to views called from another view - El Forum - 03-06-2012 [eluser]Matalina[/eluser] nice to know. Thanks. Passing data to views called from another view - El Forum - 03-07-2012 [eluser]bleu[/eluser] [quote author="CroNiX" date="1331057876"] Code: $this->load_vars($data); Instead of: Code: $this->load->view('file', $data); Code: $this->load_vars($data); 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 Passing data to views called from another view - El Forum - 03-07-2012 [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) { |