Welcome Guest, Not a member yet? Register   Sign In
How do I reference variables from the controller into a view?
#1

[eluser]Unknown[/eluser]
Needing a little assistance with setting up multiple view references from within on controller function.

Maybe I am setting this up wrong and if so please let me know.

Question is how do I carry stored variable data like the $seo array listed below from the controller into the view if the view is loaded from the controller.

Views:

View1: header
Code:
<!DOCTYPE html>
&lt;html&gt;
&lt;head&gt;
&lt;meta name="keywords" content="&lt;?PHP echo $seo-&gt;keywords ?&gt;" /&gt;
&lt;meta name="description" content="&lt;?PHP echo $seo-&gt;description ?&gt;" /&gt;
&lt;meta http-equiv="content-type" content="text/html; charset=utf-8" /&gt;
&lt;title&gt;&lt;/title>
&lt;link href="style.css" rel="stylesheet" type="text/css" media="screen" /&gt;
&lt;/head&gt;

View2: navi
Code:
<div id="menu">
  <ul>
   &lt;?PHP echo $menu->list ?&gt;
  </ul>
</div>

View3: footer

Code:
<div id="footer">
  <p>Footer</p>
</div>
&lt;/body&gt;
&lt;/html&gt;

Controller:
Code:
class User extends CI_Controller {
    public function addUser(){
        $this->load->view('header');
        $this->load->view('wrapper');
        $this->load->view('navi');
        
        $this->load->view('add_user');
        
        $this->load->view('footer');
    }
#2

[eluser]Aken[/eluser]
http://ellislab.com/codeigniter/user-gui...views.html
#3

[eluser]Unknown[/eluser]
Thank you. Had looked at that document but was having problems understanding it. Looked it over again now knowing it does contain what I wanted and ran tests until I successfully got this to work.
#4

[eluser]Aken[/eluser]
Passing dynamic data to views is an incredibly important and common feature with CI apps. Definitely learn how to do it properly.
#5

[eluser]rulin[/eluser]
Hi Creator

In your controller you create an array of values and pass it to the view as the 2nd parameter
Code:
$data = array();
$data['menu'] = $menu->list;
$data['seo_keywords'] = $seo->keywords;
$this->load->view('header', $data);
$this->load->view('navi', $data);

then in you view you reference the array element as a variable
Code:
&lt;meta name="keywords" content="&lt;?PHP echo $seo_keywords ?&gt;" /&gt;
and
Code:
&lt;?PHP echo $menu ?&gt;




Theme © iAndrew 2016 - Forum software by © MyBB