【发布时间】:2016-07-04 01:11:33
【问题描述】:
我的控制器是这样的:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Dashboard extends EX_Controller
{
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->data['news'] = $this->dashboard_model->get_news();
$this->load->view('dashboard/index_view', $this->data);
}
我的 EX_Controller 是这样的:
<?php
class EX_Controller extends CI_Controller
{
public $data;
public function __construct()
{
parent::__construct();
$this->load->model('notification_model');
$this->get_notification();
}
public function get_notification()
{
$session = $this->session->userdata('login');
$this->data['notification'] = $this->notification_model->get($session);
$this->data['count_notification'] = $this->notification_model->get_count_notification($session['customer_id']);
}
}
?>
我的索引视图是这样的:
<?php
foreach ($notification as $value) {
?>
<li>
<?php
echo '<a href='.base_url().'notification/notif/'.$value['notification_id'].'>';
?>
<span class="time">
<?php
echo time_elapsed($value['notification_created_date']);
?>
</span>
<span class="details">
<span class="label label-sm label-icon label-info">
<i class="fa fa-bookmark"></i>
</span> <?php echo $value['notification_message']; ?>
</span>
</a>
</li>
<?php
}
?>
执行时出现错误:
Message: Undefined variable: count_notification
Message: Undefined variable: notification
EX控制器中好像不能调用get_notification函数
我输入了函数get_notification(EX Controller),以便在所有控制器中读取
有什么办法可以解决我的问题吗?
【问题讨论】:
-
$data定义在哪里? -
@meda,不是
$data,而是$this->data。我想在function get_notification(EX_Controller) 中获得$this->data['notification']和$this->data['count_notification']。然后将其发送到 index_view。 -
是的,但我看不出您在代码中的哪个位置声明了该属性,它也应该是公开的
public $data; -
@meda,我在
EX_Controller中添加public $data;,如下所示:<?php class EX_Controller extends CI_Controller { public $data; public function __construct() { ...。但是,它不起作用 -
@moses_toh 在你的代码中哪里调用 get_notification
标签: php codeigniter