[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><?=$logged_in?></p>
<?=anchor('blog','Return blog page');?> | <?=anchor('blog/logout','or logout');?>
</div>
</div>
<?php
$this->load->view('footer');
?>
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!