Welcome Guest, Not a member yet? Register   Sign In
Redundant data control in Controller, How!!!
#1

[eluser]Unknown[/eluser]
I am new to CI,

This is my code
Code:
<?php
class Home extends Controller {
    
    /**
     *
     * Enter description here ...
     */
    function __construct() {
        parent::Controller();
    }
    
    /**
     *
     * Enter description here ...
     */
    function index() {
        
        $data = "";
        $data['view_pagetitlebar'] = "Welcome | Muhammad Ahmed";
        $data['view_metadata'] = "metadata";
        $data['view_stylesheet'] = "css style sheet";
        $data['view_navlinks'] = "nav links";
        $data['view_pagetitle'] = "asd";
        
        $data['view_contents_left'] = "Left side content";
        $data['view_footer'] = "Footer";
        
        
        $data['view_contents_right'] = "This is the home page";
        
        $this->load->view('view_main', $data);
        
    }
    
    /**
     *
     * Enter description here ...
     */
    function about() {
        
        $data = "";
        $data['view_pagetitlebar'] = "Welcome | Muhammad Ahmed";
        $data['view_metadata'] = "metadata";
        $data['view_stylesheet'] = "css style sheet";
        $data['view_navlinks'] = "nav links";
        $data['view_pagetitle'] = "asd";
        
        $data['view_contents_left'] = "Left side content";
        $data['view_footer'] = "Footer";
        
        
        $data['view_contents_right'] = "This is the about page";
        
        $this->load->view('view_main', $data);
        
    }
}

we can see each time maximum data remains the same, only 'view_contents_right' is different in each function.

If i made a new function or any new controller i need to paste all the data again with update 'view_contents_right' data...
Can we controller this redundant data..

Any link, or any thing that can help me...

Thanks,
Muhammad Ahmed
#2

[eluser]Unknown[/eluser]
Perhaps you can use attributs of your controlleur :

Code:
private $data = array('key1' => 'value1')
#3

[eluser]Georgi Budinov[/eluser]
If this redundant data is static and always pretty much the same it is better to move that to a config file. Then you can load the value and set the different pieces in it.
#4

[eluser]InsiteFX[/eluser]
Look in the CodeIgniter Users Guide MY_Controller

InsiteFX
#5

[eluser]Derdiusz[/eluser]
You can also add an array variable to your controller:

Code:
class Home extends Controller {
var $data = array();

and in your constructor put your static data:
Code:
function __construct() {
parent::Controller();
$this->data['view_pagetitlebar'] = "Welcome | Muhammad Ahmed";
$this->data['view_metadata'] = "metadata";
$this->data['view_stylesheet'] = "css style sheet";
$this->data['view_navlinks'] = "nav links";
$this->data['view_pagetitle'] = "asd";
        
$this->data['view_contents_left'] = "Left side content";
$this->data['view_footer'] = "Footer";
}

From now load view with:
Code:
$this->load->view('view_main', $this->data);

You able to add (or replace) any data to your $this->data in other methods, like:

Code:
function about() {
$this->data['view_contents_right'] = "This is the about page";
$this->load->view('view_main', $this->data);
}

function home() {
$this->data['view_contents_right'] = "This is the HOME page";
$this->load->view('view_main', $this->data);
}

Greetings
D.




Theme © iAndrew 2016 - Forum software by © MyBB