Welcome Guest, Not a member yet? Register   Sign In
how to have a varibale in the header
#1

[eluser]runrun[/eluser]
Hi,

How could i have a variable in the header.php file ?

This is how use the header.php:

For exmaple: I open file account_view.php i will see

Code:
<?php $this->load->view('doctype_view');?>
<link href="../../../css/main_css.css" rel="stylesheet" type="text/css" />
<link href="../../../css/account.css" rel="stylesheet" type="text/css" />
<?php $this->load->view('header_view'); ?>
    <div class="center">
        <div>account information</div>
        <dvi><span>name</span>&lt;?=$name?&gt;</div>
        <div><span>email</span>&lt;?=4email?&gt;</div>
        ....
    </div>
&lt;?php $this->load->view('footer_view'); ?&gt;

This way, I see its impossible to have a variable in the header_view.php as it has no controller.
#2

[eluser]pistolPete[/eluser]
Code:
$this->load->vars($array)
User guide:
Quote:This function takes an associative array as input and generates variables using the PHP extract function. This function produces the same result as using the second parameter of the $this->load->view() function above. The reason you might want to use this function independently is if you would like to set some global variables in the constructor of your controller and have them become available in any view file loaded from any function. You can have multiple calls to this function. The data get cached and merged into one array for conversion to variables.
#3

[eluser]runrun[/eluser]
Sorry , can't understand what you really mean. What would the $array contain ?

Now give it up. Say I i put this line in the account_view.php

&lt;?php $this->load->view('header_view',$data); ?&gt;

Where would I declare variable $data ? and note that I have no controller file for it
#4

[eluser]TheFuzzy0ne[/eluser]
A controller is called at some point, that's how your views are loaded, so that's when you should load your variables, from a model, library or helper if it makes life any easier.
#5

[eluser]runrun[/eluser]
You know what, that's really great suggestion, I'm gonna write a function in the url helper which was set to be available globally, then call the function in the header.
#6

[eluser]jdfwarrior[/eluser]
I'm trying to figure out why hes going html mixed in with php to load views. Why aren't the views being loaded in the controller? Use a model, library, helper, or the controller to load an array to all the values that need to be passed. Then load the view in the controller and pass that array to it.
#7

[eluser]runrun[/eluser]
I know what mean, after looking other discussion about header and footer, I know we can do that as well. I'm sure everybody else doing that too, but at the moment, i'm more comfortable with what i'm doing.

I think it would be perfect if CI features a layout framework as well, less headache for newbie.
#8

[eluser]jdfwarrior[/eluser]
Less headache if you use it the way its intended as well. Cars are intended to be driven from the driver's seat. If I decide to attempt to drive from outside the car instead, hilarity and issues occur.
#9

[eluser]runrun[/eluser]
[quote author="pistolPete" date="1236956476"]
Code:
$this->load->vars($array)
User guide:
[/quote]

I understand this now, this wouldn't make the the variables available in the entire view files in the view folder unless you put it in every single controllers constructor.
#10

[eluser]jdfwarrior[/eluser]
But your not using a controller, according to you.

1. Enter Controller
2. Request Data
3. Prep Data
4. Store Data in Array
5. Load Views with Data passed into the View
6. Data available in the view.




Theme © iAndrew 2016 - Forum software by © MyBB