Welcome Guest, Not a member yet? Register   Sign In
How to Add Dynamic Header and Footer
#11

(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/g...asses.html



I believe that it is necessary to explain something before showing the method I use. I started with CI2 and had close to zero knowledge of PHP. Then version 3.0 emerged, and I found myself at a crossroads: either I dived deep into learning CI or PHP, but it soon became clear that only the last option, PHP would be adequate. CodeIgniter is a powerful productivity tool, without a doubt, the better I understand it, the higher my productivity will be, but there is a limit. With each new version, some things stop being used and new ones appear. Most importantly, it is a tool designed with PHP in mind, not the other way around.

My knowledge of PHP remains close to zero, but it is my priority and where should I find solutions to problems, regardless of the framework used or even without any. I say this because I see that when faced with a problem whose solution is up to PHP, the solution is sought in the framework, CI.

New version .... New framework ... Wildlife, without framework ...
The solution I use (on the home page):

Controller:
PHP Code:
$header = array ( 
 
'icon' => 'favicon'
 
'css' => 'home',
 
'title' => 'wdeda | Music, videos, blog',
 
'action' => '/lab/search/allmedia/',
 
'placeholder' => 'Search'
 
); 
 
 
$this->load->view('templates/header'$header);
 
$this->load->view('/home/home'$data); 
 
$this->load->view('templates/footer'); 

sometimes it´s very, very complex:

PHP Code:
foreach ($this->album_model->get_name() as $row); 
 
$playerid $row->id;
 
$name $row->name;
 
 foreach(
$this->album_model->get_album() as $row);
 
$rectitle $row->title;
 
$name1 $row->name1;
 
$name2 $row->name2;
 
$name3 $row->name3;
 
$name4 $row->name4
 
 
$names[0] = $name1;
 
$names[1] = $name2;
 
$names[2] = $name3;
 
$names[2] = $name4;
 
$print_name "";
 if( empty(
$name1) )
 {
    $print_name $name;
 }
 else
 {
    foreach( $names as $n )
    {
        if( !empty($n) )
        {
            if( $print_name != "" )
            {
                $print_name .= " / ";
            }
            $print_name .= $n;
        }
    }
 }
 
 
 
$header = array ( 
 
'title' => $rectitle.' - '.$print_name' | Music | Credits | wdeda',
 
'icon' => 'favicon',
 
'css' => 'record',
 
'action' => '/lab/search/allmedia/',
 
'placeholder' => 'search'
 
); 
 
 
$this->load->view('templates/header'$header); 
 
$this->load->view('content/albums/album'$data);
 
$this->load->view('templates/footer'); 

And, of course, a single header meets every need.

PHP Code:
<head>
<
meta charset="UTF-8">
<
meta name="viewport" content="width=device-width, initial-scale=1.0">
<
meta name="description" content="Catálogo privado de filmes e músicas">
<
meta http-equiv="X-UA-Compatible" content="ie=edge">
<
link rel="shortcut icon" href="/lab/public/img/<?php echo $icon; ?>.ico" type="image/x-icon" />
<
link rel="stylesheet" href="/lab/public/css/global.css">
<
link href="/lab/public/css/header.css" rel="stylesheet">
<
link href="/lab/public/css/<?php echo $css; ?>.css" rel="stylesheet">
<
title><?php echo $title?></title>
</head>
...

<div class="search-container">
 <form action="<?php echo $action?>" class="site-search" method="post" name="site-search">
 <input class="site-search-button" name="submit1" type="submit" />
 <input class="site-search-input" name="search" placeholder="<?php echo $placeholder?>" tabindex="1" type="search" />
 </form>
 </div> 

I have a blog, the placeholder will be ... Search the blog;
A specific area to search for backups, the placeholder will be .. Search backups

No matter which framework or its version, it is a solution, I can say, definitive.
It is important to consider that anyone who knows PHP will be able to analyze and, if applicable, make changes. Meets documentation requirements for systems and procedures.
OK, I'm not a professional, but nothing stops me from trying to think like one.
Reply
#12

Why do not you use ajax to load the content? Insert this script inside the head tags and the file should load.

<script>
$(function(){
$.ajax({
type: "GET",
url: "header/footer/header.html",
dataType: "html",
success: function(answer) {
$("body").append(answer);
},
error: function(){
alert("failed call!!!");
}
});
return false;
});
</script>

Omegle Bazoocam Chatrandom
Reply
#13

(This post was last modified: 12-13-2022, 11:07 AM by jamesedger.)

(03-02-2018, 10:56 AM)superbrands Wrote: Here is my code: mini militia

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.

I think you should need to change some basic Home.php codes to solve this issue.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB