CodeIgniter Forums
How to Add Dynamic Header and Footer - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: Model-View-Controller (https://forum.codeigniter.com/forum-10.html)
+--- Thread: How to Add Dynamic Header and Footer (/thread-70162.html)

Pages: 1 2


How to Add Dynamic Header and Footer - superbrands - 03-02-2018

Here is my code:

controllers/Home.php


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

class 
Home extends CI_Controller {
    
    
public function __construct()
    {
        parent::__construct();
        $this->load->helper('url_helper');
    }
    
    
public function index()
    {
        if ( ! file_exists(APPPATH.'views/themes/default/templates/common/home.php'))
        {
            // Whoops, we don't have a page for that!
            show_404();
        }
        
        $data
['title'] = "Home";
        
        $this
->load->view('themes/default/templates/common/header'$data);
        $this->load->view('themes/default/templates/common/home'$data);
        $this->load->view('themes/default/templates/common/footer'$data);
    }



views/themes/default/templates/common/header.php

PHP Code:
<!DOCTYPE html>
<
html>
<
head>
<
title>Title</title>
</
head>
<
body>
<
header>
    <div class="container"><?php echo $title?></div>
</header> 


views/themes/default/templates/common/home.php

PHP Code:
<div class="container">
    <h1><?php echo $title?></h1>
</div> 


views/themes/default/templates/common/footer.php


PHP Code:
<footer>
    <div class="container"><?php echo $title?></div>
</footer>
</body></html> 


Right now, it shows the text Home in each section (in header, in home, and in footer).

I want each page shows different titles (header shows text Header, home shows text Home, and footer shows text Footer).

How to achieve it? Thanks.


RE: How to Add Dynamic Header and Footer - wallacesilva09 - 03-02-2018

I solved this problem with MY_Controller.php in folder app/core/ I created a method/action with this:

PHP Code:
//...
public function theme($file){
 
   $this->load->view('themes/default/templates/common/header'$data);
 
   $this->load->view('themes/default/templates/common/'.$file$data);
 
   $this->load->view('themes/default/templates/common/footer'$data);
}
//... 

in controller extends the MY_Controller and call view with
PHP Code:
$this->theme('home'); 


see more about MY_Controller here: https://www.codeigniter.com/user_guide/general/core_classes.html


RE: How to Add Dynamic Header and Footer - superbrands - 03-02-2018

(03-02-2018, 03:35 PM)wallacesilva09 Wrote: I solved this problem with MY_Controller.php in folder app/core/ I created a method/action with this:

PHP Code:
//...
public function theme($file){
 
   $this->load->view('themes/default/templates/common/header'$data);
 
   $this->load->view('themes/default/templates/common/'.$file$data);
 
   $this->load->view('themes/default/templates/common/footer'$data);
}
//... 

in controller extends the MY_Controller and call view with
PHP Code:
$this->theme('home'); 


see more about MY_Controller here: https://www.codeigniter.com/user_guide/general/core_classes.html

The $data, where it comes from?

If all views have the same $data inputs, then all views will display the same data, right? Just like in my case.


RE: How to Add Dynamic Header and Footer - jreklund - 03-03-2018

You need to add different keys to your $data array.
PHP Code:
$data['header'] = 'Header';
$data['home'] = 'Home';
$data['footer'] = 'Footer';

<?
php echo $header?>
<?php 
echo $home?>
<?php 
echo $footer?>

PHP Code:
$data['title']['header'] = 'Header';
$data['title']['home'] = 'Home';
$data['title']['footer'] = 'Footer';

<?
php echo $title['header']; ?>
<?php 
echo $title['home']; ?>
<?php 
echo $title['footer']; ?>



RE: How to Add Dynamic Header and Footer - Daniel_30 - 05-04-2018

Hi,

I will show given below one simple code

//...
public function theme($file){
$this->load->view('themes/default/templates/common/header', $data);
$this->load->view('themes/default/templates/common/'.$file, $data);
$this->load->view('themes/default/templates/common/footer', $data);
}
//...

Hope that Helps!
Regards,
Daniel


RE: How to Add Dynamic Header and Footer - qury - 05-09-2018

The solution is fairly simple:

PHP Code:
$header['title'] = "Header";
$data['title'] = "Home";
$footer['title'] = "Footer";

 
       $this->load->view('themes/default/templates/common/header'$header);
 
       $this->load->view('themes/default/templates/common/home'$data);
 
       $this->load->view('themes/default/templates/common/footer'$footer); 



RE: How to Add Dynamic Header and Footer - Gurutechnolabs - 09-28-2018

Hello,
When you want to set particular(dynamic) header footer in your home, about, contact us that type of pages then you have to set this code in about controller.

class Userlist extends CI_Controller {
public function __construct(){
}
public function index()
{
        $this->load->view('themes/default/templates/common/header', $data);
        $this->load->view('themes/default/templates/common/home', $data);
        $this->load->view('themes/default/templates/common/footer', $data);

}
This code you have to put in about controller and you get results as per your desire.

Hope This will Help you.


RE: How to Add Dynamic Header and Footer - InsiteFX - 11-26-2018

PHP Code:
$data['header'] = 'Header';
$data['home'  'Home';
$data['footer'] = 'Footer';

$this->load->vars($data);        // makes the $data avaiable to all views Global
$this->load->view('yourView');   // no need to pass $data here 



RE: How to Add Dynamic Header and Footer - garimapatil - 01-22-2019

Code:
Header Code

<div id="header" align="center">
<img src=\"/images/header.jpg border='0' \">
<p>
<a href="index.php">Home</a> -
<a href="profile.php">Profile</a> -
<a href="links.php">Links
</a>- <a href="contact.php">Contact</a>
</p>
Code:
Footer code

<div id="footer" align="center">
<p>
<a href="index.php">Home</a> -
<a href="profile.php">Profile</a> -
<a href="links.php">Links
</a>- <a href="contact.php">Contact</a>
© Copyright Your name here <?php include 'date.php'; ?>
</p>
</div>



RE: How to Add Dynamic Header and Footer - MAILITY - 05-24-2019

(01-22-2019, 12:30 AM)garimapatil Wrote:
Code:
Header Code

<div id="header" align="center">
<img src=\"/images/header.jpg border='0' \">
<p>
<a href="index.php">Home</a> -
<a href="profile.php">Profile</a> -
<a href="links.php">Links
</a>- <a href="contact.php">Contact</a>
</p>
Code:
Footer code

<div id="footer" align="center">
<p>
<a href="index.php">Home</a> -
<a href="profile.php">Profile</a> -
<a href="links.php">Links
</a>- <a href="contact.php">Contact</a>
© Copyright Your name here <?php include 'date.php'; ?>
</p>
</div>

I solved this problem with MY ->Xender Discord Omegle _Controller.php in folder app/core/ I created a method/action with this: