Welcome Guest, Not a member yet? Register   Sign In
Dinamic page
#1

[eluser]stalwart[/eluser]
Hi all. I am new in framework and i want to ask how can i maka a dinamic page.
my idea is to have a information that can be seen in all sub pages without declareing it in all sub pages.
this is my code
Code:
class Base extends Controller {
    
    function Base()
    {
        parent::Controller();
        $this->load->helper('url');
        $this->load->helper('form');
        $this->load->library('session');
    }
    
    function index()
    {        
        if(!$this->session->userdata('username')){redirect('');}
        $data['username'] = $this->session->userdata('username');
        $data['user_id']  = $this->session->userdata('user_id');
        $data['query']=$this->db->query("SELECT * FROM city where user_id='$data[user_id]'");
    
    $this->load->view('base', $data);    
    }
    function buildings()
    {    if(!$this->session->userdata('username')){redirect('');}
        $data['username'] = $this->session->userdata('username');
        $data['user_id']  = $this->session->userdata('user_id');
        $data['query']=$this->db->query("SELECT * FROM city where user_id='$data[user_id]'");
        $data['buildings'] = 'Сгради';
        $this->load->view('buildings', $data);

    }

}
you can see that username, user_id and so on are declared in all sub pages
#2

[eluser]TheFuzzy0ne[/eluser]
You can probably create a private function (prefixed with an underscore) in your class, which will be called upon, and that can setup your variables for you.
Code:
private function _getVars()
{
    static $data = '';
    if (is_array($data))
    {
        return $data;
    }
    /* initialisation code here */
    $data['username'] = $this->session->userdata('username');
    $data['user_id']  = $this->session->userdata('user_id');
    $data['query']    = $this->db->query("SELECT * FROM city where user_id='$data[user_id]'");
    return $data;
}
Now when you call $this->_getVars() the user data is returned. There may be another solution, which would be to export the function to a helper file, but you should only really need to do that if it's required by more than one controller.

Hope this helps.
#3

[eluser]stalwart[/eluser]
I try it but it's not working
#4

[eluser]Tom Glover[/eluser]
DB calls should be in a model, if you are sticking with MVC principle and have you called the the database class.

Models: http://ellislab.com/codeigniter/user-gui...odels.html
Database: http://ellislab.com/codeigniter/user-gui...index.html
MVC: http://ellislab.com/codeigniter/user-gui...w/mvc.html

Hope this helps you.
#5

[eluser]TheFuzzy0ne[/eluser]
Oops. You're right. :bug: I apologise.




Theme © iAndrew 2016 - Forum software by © MyBB