Hello,

This I am trying to create an effective navigation code in CI. Let's say I have 10 shared pages that have the same navigation. Do I have to add the "## navigation link" codes over and over again (10 times ?)

controllers/page.php

<?php

class Page extends CI_Controller {

    public function __construct(){

    parent::__construct();

    ## load the url helper object
    $this->load->helper("url");
    }

    public function index()
    {

    ## define the location of your css directory in reference to the application directory

    $data = array(
            'style' => base_url().'assets/css/style.css',
            'connection' => base_url().'assets/images/connection.jpg',
            'collaboration' => base_url().'assets/images/collaboration.jpg',
            'solution' => base_url().'assets/images/solution.jpg',
            'connectionUrl' => base_url().'index.php/connection/',
            'collaborationUrl' => base_url().'index.php/collaboration/',
            'solutionUrl' => base_url().'index.php/solution/'

            );              

    ## navigation link

    $datanav = array(
               'nav' => base_url().'assets/css/nav.css',
               'logo2' => base_url().'assets/images/logo2.png', 
               'index' => base_url(),
               'connectionUrl' => base_url().'index.php/connection/',
               'collaborationUrl' => base_url().'index.php/collaboration/',
               'solutionUrl' => base_url().'index.php/solution/',
               'foUrl' => base_url().'index.php/connection/fo',
               'wirelessUrl' => base_url().'index.php/connection/wireless',
               'cloudUrl' => base_url().'index.php/collaboration/cloud',
               'emailUrl' => base_url().'index.php/collaboration/email',
               'vconUrl' => base_url().'index.php/collaboration/vcon',
               'sharepointUrl' => base_url().'index.php/collaboration/sharepoint',
               'crmUrl' => base_url().'index.php/collaboration/crm',
               'voiceUrl' => base_url().'index.php/collaboration/voice',
               'networkUrl' => base_url().'index.php/solution/network',
               'systemUrl' => base_url().'index.php/solution/system',
               'surveillanceUrl' => base_url().'index.php/solution/surveillance',
               'searchbutton' => base_url().'assets/images/search button.jpg'
            );

    $this->load->view('templates/navigation', $datanav);
    $this->load->view('homepage', $data); 
    }


}

?>

views/template/navigation.php

<link href= "<?php echo $nav; ?>" rel="stylesheet" type="text/css" media="screen">

<div id="navigation">

<div id="logo"><img src="<?php echo $logo2; ?>" width="160" height="60"></div>

<div id='cssmenu'>
  <ul>
     <li class='active'><a href='<?php echo $index; ?>'>|&nbsp&nbsp&nbsp&nbspHome&nbsp&nbsp&nbsp&nbsp|</a></li>
     <li class='has-sub'><a href='<?php echo $index; ?>'>&nbsp&nbsp&nbsp&nbspProduct & Services&nbsp&nbsp&nbsp&nbsp|</a>
        <ul>
           <li class='onelayer'><a href='<?php echo $connectionUrl; ?>'>&nbsp&nbsp&nbsp&nbspConnection &nbsp&nbsp&nbsp&nbsp&nbsp ></a>
              <ul>
                 <li class='onelayer'><a href='<?php echo $foUrl; ?>'>&nbsp&nbsp&nbsp&nbspFiber Optic</a></li>
                 <li class='onelayer'><a href='<?php echo $wirelessUrl; ?>'>&nbsp&nbsp&nbsp&nbspWireless</a></li>
              </ul>
           </li>
           <li class='onelayer'><a href='<?php echo $collaborationUrl; ?>'>&nbsp&nbsp&nbsp&nbspCollaboration&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp ></a>
              <ul>
                 <li class='onelayer'><a href='<?php echo $cloudUrl; ?>'>&nbsp&nbsp&nbsp&nbspCloud</a></li>
                 <li class='onelayer'><a href='<?php echo $emailUrl; ?>'>&nbsp&nbsp&nbsp&nbspEmail</a></li>
                 <li class='onelayer'><a href='<?php echo $vconUrl; ?>'>&nbsp&nbsp&nbsp&nbspVCON</a></li>
                 <li class='onelayer'><a href='<?php echo $sharepointUrl; ?>'>&nbsp&nbsp&nbsp&nbspSharepoint</a></li>
                 <li class='onelayer'><a href='<?php echo $crmUrl; ?>'>&nbsp&nbsp&nbsp&nbspCRM</a></li>
                 <li class='onelayer'><a href='<?php echo $voiceUrl; ?>'>&nbsp&nbsp&nbsp&nbspVoice</a></li> 
              </ul>
           </li>
           <li class='onelayer'><a href='<?php echo $solutionUrl; ?>'>&nbsp&nbsp&nbsp&nbspSolution&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp ></a>
              <ul>
                 <li class='onelayer'><a href='<?php echo $networkUrl; ?>'>&nbsp&nbsp&nbsp&nbspNetwork Solution</a></li>
                 <li class='onelayer'><a href='<?php echo $systemUrl; ?>'>&nbsp&nbsp&nbsp&nbspSystem Solution</a></li>
                 <li class='onelayer'><a href='<?php echo $surveillanceUrl; ?>'>&nbsp&nbsp&nbsp&nbspSurveillance</a></li> 
              </ul>
           </li>
        </ul>
     </li>
     <li><a href='#'>&nbsp&nbsp&nbsp&nbspAgent&nbsp&nbsp&nbsp&nbsp|</a></li>
     <li><a href='aboutus.php'>&nbsp&nbsp&nbsp&nbspAbout Us&nbsp&nbsp&nbsp&nbsp|</a></li>
     <li><a href='contactus.php'>&nbsp&nbsp&nbsp&nbspContact Us&nbsp&nbsp&nbsp&nbsp|</a></li>
  </ul>
</div>


<div id="search">
<input type="text" name="search"></div>
<div id="sbutton"><img src="<?php echo $searchbutton; ?>"></div>
</div>

</div>

You can change $data to a property of the class, so you can do:

class Page extends CI_Controller {

    // declare property
    protected $data;

    public function __construct()
    {
        parent::__construct();

        // define the property
        $this->data['assets'] = array(
                'style' => base_url().'assets/css/style.css',
                'connection' => base_url().'assets/images/connection.jpg',
                'collaboration' => base_url().'assets/images/collaboration.jpg',
                'solution' => base_url().'assets/images/solution.jpg',
                'connectionUrl' => base_url().'index.php/connection/',
                'collaborationUrl' => base_url().'index.php/collaboration/',
                'solutionUrl' => base_url().'index.php/solution/'
        );

        $this->data['navigation'] = array(
               'nav' => base_url().'assets/css/nav.css',
               'logo2' => base_url().'assets/images/logo2.png', 
               'index' => base_url(),
               'connectionUrl' => base_url().'index.php/connection/',
               'collaborationUrl' => base_url().'index.php/collaboration/',
               'solutionUrl' => base_url().'index.php/solution/',
               'foUrl' => base_url().'index.php/connection/fo',
               'wirelessUrl' => base_url().'index.php/connection/wireless',
               'cloudUrl' => base_url().'index.php/collaboration/cloud',
               'emailUrl' => base_url().'index.php/collaboration/email',
               'vconUrl' => base_url().'index.php/collaboration/vcon',
               'sharepointUrl' => base_url().'index.php/collaboration/sharepoint',
               'crmUrl' => base_url().'index.php/collaboration/crm',
               'voiceUrl' => base_url().'index.php/collaboration/voice',
               'networkUrl' => base_url().'index.php/solution/network',
               'systemUrl' => base_url().'index.php/solution/system',
               'surveillanceUrl' => base_url().'index.php/solution/surveillance',
               'searchbutton' => base_url().'assets/images/search button.jpg'
        );

    }

    public function index()
    {
        $this->data['title'] = 'title of this page';
        $this->load->view('templates/navigation', $this->data['navigation']);
        $this->load->view('homepage', $this->data['assets']); 
    }

    public function another_page()
    {
        $this->data['title'] = 'Another title';
        $this->load->view('templates/navigation', $this->data['navigation']);
        $this->load->view('anotherpage', $this->data['assets']); 
    }

}

Reference: http://www.php.net/manual/en/language.oop5.properties.php

Hm I don't know CI, but that sounds pretty unlogical to me. If you define the class Page as you are doing now, and if you include that class on every page that needs the navigation that it now contains, then there is no problem, right? As it would then contain the hardcoded navigation items that it contains right now each time it is included.

(See also the post above that wasn't there yet when I started typing this message ^^).

@cereal, thanks. That's a good idea. It saves more time.

Yet, I still have three different controller name that's going to need to use the same shared navigation:

'connectionUrl' => base_url().'index.php/connection/',
'collaborationUrl' => base_url().'index.php/collaboration/',
'solutionUrl' => base_url().'index.php/solution/'

  1. controllers/connection.php
  2. controllers/collaboration.php
  3. controllers/solution.php

@everyone: Is it possible to include the "## navigation link" in all those controllers ?

Yes if you extend those controllers with Page, something like:

class Connection extends Page
{
    public function __construct()
    {
        parent::__construct();
    }
}

Recalling the construct of Page (parent::__construct()) you can access to the public and protected properties.

In CodeIgniter this can work if you place Page controller inside the System/Core or if you include it when the controller loads:

<?php

include 'page.php';

class Connection extends Page
{
    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        $this->load->view('connection/someindex', $this->data);
    }
}

Read this for more information: http://philsturgeon.co.uk/blog/2010/02/CodeIgniter-Base-Classes-Keeping-it-DRY

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: data

Filename: controllers/page.php

Line Number: 19

Fatal error: Cannot access empty property in C:\xampp\htdocs\IndonusaCI\application\controllers\page.php on line 19

controllers/page.php

class Page extends CI_Controller {

    // declare property
    protected $data;

    public function __construct(){

        parent::__construct();

        ## load the url helper object
        $this->load->helper("url");


        ## define the property

        $this->$data['assets'] = array(
                'style' => base_url().'assets/css/style.css',
                'connection' => base_url().'assets/images/connection.jpg',
                'collaboration' => base_url().'assets/images/collaboration.jpg',
                'solution' => base_url().'assets/images/solution.jpg',
                'connectionUrl' => base_url().'index.php/connection/',
                'collaborationUrl' => base_url().'index.php/collaboration/',
                'solutionUrl' => base_url().'index.php/solution/'

                );              

Line 19:

$this->$data['assets'] = array(

Change $this->$data['assets'] to $this->data['assets']

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.