Welcome Guest, Not a member yet? Register   Sign In
How to get "header" variable
#1

[eluser]Andychan[/eluser]
Hi, I'm come from Hong Kong. Is a Codeigniter novice.

As usual, I make a site with php

will have three part - header.php , main.php , footer.php

And then use rquire_once / include_once combined them

header.php for the detect login or not, and title variable etc.

I'm not sure the CI concept, if I make a simple site, only three page

I will.........

Quote:index.php, index_view.php

about.php, about_view.php

contact.php, contact_view.php

header.php (At View)

footer.php (At View)

every page include header.php and footer.php

if About.php , I want to get some $data , I need to write

Code:
$data['title'] = xxx;

Code:
<?php

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class About extends CI_Controller {

    public function index(){        
        
        // layout part
        $this->load->view('header');
        $this->load->view('about_view');
        $this->load->view('footer');
    }
}

/* End of file about.php */


for the repeat use, I will put it into header.php,

Code:
$a = "hello";

but about_view.php cannot get the $a

How can I do?

please help, thanks !!!
#2

[eluser]InsiteFX[/eluser]
application/views/template.php
Code:
<?php $this->load->view('header_view');
<?php $this->load->view('content_view');
<?php $this->load->view('footer_view');

In your controller:
Code:
$data = array();

// you use $data to pass info to your views!
$data['a'] = 'hello';

$this->load->vars($data);
$this->load->view('template');

InsiteFX
#3

[eluser]Andychan[/eluser]
Thanks for your help !!

I am not clearly understand one more thing, how about I change the content_view?

because content is not a fix page, maybe is about us , maybe is contact us...
#4

[eluser]marcogmonteiro[/eluser]
in that case do this:

Code:
$this->load->view('header_view');
<?php $this->load->view($content);
<?php $this->load->view('footer_view');

in your controller

Code:
$data = array();

// you use $data to pass info to your views!
$data['a'] = 'hello';
$data['content'] = 'about_view';
$this->load->vars($data);
$this->load->view('template');
#5

[eluser]Andychan[/eluser]
Thanks !

So you mean detect the URL?

For example: domain.com/index.php/about/

And then controller detect the url is about, show the about content?

right?
#6

[eluser]marcogmonteiro[/eluser]
That's one way. But in my example I'm actually defining the content variable inside the controler.

Code:
$data['content'] = 'about_view';

to accomplish that should be something like this:
(if your url is something like this: index.php/about)
Code:
$data['content'] = $this->uri->segment(1).'_view';
#7

[eluser]Andychan[/eluser]
Oh, great !!!
Thanks ! I know what you mean, let me try it.
#8

[eluser]Andychan[/eluser]
but the url only index?
because the url define by controller, right?


if controller page is index

how can I define
domain.com/index.php/about/
domain.com/index.php/contact/

(that will have two controller , right?)


I try to use function class, only can:

domain.com/index.php/welcome/about/

domain.com/index.php/welcome/contact/
#9

[eluser]marcogmonteiro[/eluser]
No, your controller is about and your method inside that controller is index...


something like this:


Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class About extends CI_Controller {

    public function index(){
          
          //your segment 1 is about index.php does not count for segments
          $data['content'] = $this->uri->segment(1).'_view';

    }

}

I hope this helps =)
#10

[eluser]Andychan[/eluser]
Thank you very much !!! That's very useful for me.

I try it this week first Smile


this part is index (welcome.php)?

Code:
$data = array();

// you use $data to pass info to your views!
$data['a'] = 'hello';
$data['content'] = 'about_view';
$this->load->vars($data);
$this->load->view('template');

Thanks again !




Theme © iAndrew 2016 - Forum software by © MyBB