Welcome Guest, Not a member yet? Register   Sign In
How to include header controller to other pages?
#1

[eluser]Lazos[/eluser]
I am trying for hours to pass/include the header controller into my other controllers so I wont need to write header and footer all the time. How can I do that?

My header is not just a view that I could just write load->view(header) and then below that load->view(main)

Header Controller
Code:
class Header extends Controller {
    
    function index()
    {    
        $user_ip = $this->input->ip_address();
        $userdata = $this->my_session->sess_read($user_ip, ADMIN_HOME);
        $this->load->model('admin/home_model', 'home_model');
        $this->load->library('admin_theme');
        if ($userdata['session_logged_in'] != 1) {
            _redirect('index.php/admin/login');
        }
        $siteprefs = $this->home_model->site_preferences();
        $this->admin_theme->_constructor($userdata['user_id'], $userdata['group_id']);
        $headers = $this->admin_theme->SendHeaders();
        $topmenu = $this->admin_theme->DoTopMenu();
        $templatevars = array (
            'V_MENU'                    => $topmenu,
            'L_WELCOME_USER'             => $this->lang->line('welcome'),        
            'L_ADMIN_PANEL_TITLE'        => $this->lang->line('admin_panel_title'),
            'V_ADMIN_PANEL_TITLE'         => $siteprefs['sitename'],
            'V_FIRST_LAST_NAME'             => $userdata['first_name']." ".$userdata['last_name']
            );

        $this->parser->parse('admin/header.tpl', $templatevars);
    }
}


Code:
include('header.php'); //???????????
class Main extends Controller {
    
    function index()
    {    
            // Main Content
    }
}
#2

[eluser]Lazos[/eluser]
Hey guys no one knows the answer for this? Sad
#3

[eluser]lmv4321[/eluser]
I don't have a header controller or a footer controller. I have a header view and a footer view and a content view that get called by a container view:
Code:
<?php
$this->load->view('header');
$this->load->view('content');
$this->load->view('footer');
?>
The content view also calls the menu view.

The data that gets passed to these views is either built in the individual controllers functions or are built by library functions that I wrote or installed and which get called in the controller.
#4

[eluser]Lazos[/eluser]
Thanks for you reply.

Can you show an example of this?
#5

[eluser]lmv4321[/eluser]
A browse function in my categories controller which displays all rows in the table:
Code:
// -------------------------------------------------------------------------
    // Function: browse()
    // Description: Extracts a list of all categories and
    //              displays it.
    function browse()
    {
        $this->load->model('categories_model');    // Instantiate the model

        // fetch query object from categories with fields id, name
        $template['categories_list'] = $this->categories_model->fetch('categories','id, name');

        // create urls for use in content view
        $template['base_url']   = 'concentric/categories/browse/';
        $template['modify_url'] = 'concentric/categories/modify/';
        $template['delete_url'] = 'concentric/categories/delete/';
        $template['add_url']    = 'concentric/categories/add/';
        $template['cancel_url'] = 'concentric/home';

        // set values for use in content view
        $template['form_attributes'] = array('name' => 'frmcc_categories', 'id' => 'frmcc_categories');
        $template['header'] = $this->form_name;

        // build content html using grid view and template data
        $data['content'] = $this->load->view('/concentric/categories/categories_grid', $template, TRUE);

        // set values for use in header view
        $data['header']  = $this->form_name;

        // pass all data to container view and display to user
        $this->load->view('/concentric/container',$data);

    }
In the contructor of this controller, other libraries get called which set values that are then output in the header view.
Code:
$this->page->set_metatag('robots','nofollow, noindex');
        $this->page->set_metatag('pragma','nocache',TRUE);
(BackendPro users may recognize these functions.)

The categories_grid view contains this code:
Code:
<h2>&lt;?=$header?&gt;</h2>

<table width="100%" border="0" cellspacing="1" cellpadding="0">
<tr>
    <th align="right">
    <div class="buttons">
        <a href="&lt;?=site_url($add_url)?&gt;" class="blue">
        &lt;?= $this->page->icon('add');?&gt;
        Add
        </a>
    </div>
    </th>
</tr>
</table>

<br />

<table class="tbl" border="0" cellpadding="2" cellspacing="1" width="100%">
<thead>
<tr>
    <th align="right" width="70"> &nbsp; sort by:&nbsp; </th>
    <th VALIGN='MIDDLE' ALIGN='CENTER'  class='tbl_headercell'>
        Id
    </th>
    <th VALIGN='MIDDLE' ALIGN='CENTER'  class='tbl_headercell'>
        Name
    </th>

</tr>
</thead>
<tbody  class="scrollingContent">

    &lt;?
        $i = 0;
        foreach ($categories_list->result_array() as $categories) {
            $i++;
            if (($i%2)==0) { $bgColor = "#FFFFFF"; } else { $bgColor = "#C0C0C0"; }
    ?&gt;
    <tr bgcolor="&lt;?= $bgColor; ?&gt;">
        <td align="center" nowrap="nowrap">
            <a href = "&lt;?= site_url($modify_url).">Edit</a>
            &nbsp;&nbsp;&nbsp;
            <a href = "&lt;?= site_url($delete_url).">Delete</a>
        </td>
        <td align="left" nowrap="nowrap">&lt;?= $categories['id']; ?&gt;</td>
        <td align="left" nowrap="nowrap">&lt;?= $categories['name']; ?&gt;</td>
    </tr>
        &lt;? } ?&gt;
</tbody>
</table>

<br />

<div class="buttons">
    <a href="&lt;?=site_url($cancel_url)?&gt;" class="negative">
    &lt;?= $this->page->icon('cross');?&gt;
    Cancel
    </a>
</div>

and then the content view has this code:
Code:
<div id="navigation">
    &lt;?=$this->load->view('concentric/menu');?&gt;
</div>

<div id="content">            
    <a name="top"></a>
    &lt;?=$this->status->display();?&gt;
    &lt;?=(isset($content)) ? $content : NULL; ?&gt;
</div>


This may not be the best code or the most efficient, but after only 2-3 weeks of using CodeIgniter (and with very little prior experience in php), I think it is pretty good.

Notes: CodeIgniter version 1.6.3, BackendPro 0.3.1




Theme © iAndrew 2016 - Forum software by © MyBB