Welcome Guest, Not a member yet? Register   Sign In
Global variables in controllers
#6

[eluser]nguyenbatbinh[/eluser]
I don't understand used global variable in CI (used in all function in controller).
I take a exemple like Sumon:
Library: common_tasks.php
Code:
<?php
class common_tasks{
    function common_tasks()
    {
        global $data;
        $data['logged_in']=false;
    }
}    

?>
Controller: blog.php
Code:
<?php

class Blog extends Controller
{
    function Blog()
    {
        parent::Controller();
        //  load
        $this->load->helper(array('url','form','security'));
        $this->load->model(array('mblog'));
        $this->load->database();
        $this->load->library(array('auth','session','common_tasks'));
        $this->load->scaffolding('entries');

        // global variable
        global $data; // automaticaly load default values
        $data['logged_in']=true;
    }
        function index()
    {
        $data['title']='MY BLOG';
        $data['heading']='WELLCOME TO DISCONTENED BLOG';
        // lay du lieu trong bang entries
        $this->db->order_by("entry_id","desc");
        $data['query']=$this->db->get('entries');
        // Hien thi noi dung bang cach load bblog_view.php voi cac bien $data
        $this->load->view('blog_view',$data);
        
    }
        function secure()
    {
        $data['title']='Secure';
        $data['heading']='SECURE';
        if(!$this->auth->logged_in())
        {
            redirect('blog/login');
        }else
        {
            $this->load->view('secure_view',$data);    
        }
        
    }
  }
?>
View: secure_view.php
Code:
<?php
$this->load->view('header');
?>
<div id="box">
    <div class="title_box">
    <h2>The secure area!</h2>
    </div>
    <div class="body_box">

    <p>&lt;?=$logged_in?&gt;</p>

    &lt;?=anchor('blog','Return blog page');?&gt; | &lt;?=anchor('blog/logout','or logout');?&gt;
    </div>
    
</div>

&lt;?php
$this->load->view('footer');
?&gt;

Big Grin It not work.. And a PHP Error was encountered
Quote:Severity: Notice

Message: Undefined variable: logged_in

Filename: views/secure_view.php

Line Number: 9

My code true or false, and can you tell me the way to fix and use global varialle.

____________________
Sorry! I speak english not good!


Messages In This Thread
Global variables in controllers - by El Forum - 09-11-2008, 08:00 AM
Global variables in controllers - by El Forum - 09-11-2008, 09:39 AM
Global variables in controllers - by El Forum - 09-11-2008, 09:40 AM
Global variables in controllers - by El Forum - 09-11-2008, 10:06 AM
Global variables in controllers - by El Forum - 09-11-2008, 10:44 AM
Global variables in controllers - by El Forum - 10-04-2008, 03:47 AM
Global variables in controllers - by El Forum - 10-04-2008, 07:59 AM
Global variables in controllers - by El Forum - 10-04-2008, 08:35 AM
Global variables in controllers - by El Forum - 10-04-2008, 09:32 AM



Theme © iAndrew 2016 - Forum software by © MyBB