我有一个示例脚本
<?php
class level_price extends CI_Controller {
function __construct() {
parent::__construct();
session_start();
$this->load->library('encrypt');
}
function index()
{
if(!isset($_SESSION['uname'])){
redirect(base_url().'admin.php/login');
}
$data['users']=$this->db->query("SELECT count(*)AS user FROM user_table")->result_array();
$data['pro_users']=$this->db->query("SELECT count(*)AS pro_user FROM user_table WHERE membership_type <>0")->result_array();
$this->load->model('level_model');
$data['level_list']=$this->level_model->get_all_level();
$this->load->view('header',$data);
$this->load->view('level_price');
$this->load->view('footer');
}
function add_level()
{
if(!isset($_SESSION['uname']))
{
redirect(base_url().'admin.php/login');
}
$this->load->model('level_model');
$data['users']=$this->db->query("SELECT count(*)AS user FROM user_table")->result_array();
$data['pro_users']=$this->db->query("SELECT count(*)AS pro_user FROM user_table WHERE membership_type <>0")->result_array();
if(count($_POST))
{
$data['msg']=$this->level_model->add_level();
}
$this->load->view('header',$data);
$this->load->view('add_level',$data);
$this->load->view('footer');
}
function edit_level($id){
if(!isset($_SESSION['uname'])){
redirect(base_url().'admin.php/login');
}
$this->load->model('level_model');
$data['level_id'] = $id;
$data['users']=$this->db->query("SELECT count(*)AS user FROM user_table")->result_array();
$data['pro_users']=$this->db->query("SELECT count(*)AS pro_user FROM user_table WHERE membership_type <>0")->result_array();
if(count($_POST))
{
$data['msg']=$this->level_model->edit_level($id);
}
$data['level_info'] = $this->level_model->get_level($id);
$this->load->view('header',$data);
$this->load->view('edit_level',$data);
$this->load->view('footer');
}
}
?>
你可以看到我有这两条线总是在每一个功能
$data['users']=$this->db->query("SELECT count(*)AS user FROM user_table")->result_array();
$data['pro_users']=$this->db->query("SELECT count(*)AS pro_user FROM user_table WHERE membership_type <>0")->result_array();
现在我想的替代品,从而自动地拥有这两个函数调用
说一下我定义它返回数组的函数
当我只写这个剧本
echo pro_user()
然后将其显示pro_user量
说,在应用程序/管理/配置/ autoload.php
我已经加入此脚本
$autoload['model'] = array('special_model');
在special_model的功能是这样的
function pro_user()
{
$data['pro_users']=$this->db->query("SELECT count(*)AS pro_user FROM user_table WHERE membership_type <>0")->result_array();
echo $data['pro_users']['pro_user'];
}
在视图PHP像我想是这样想写点东西像这样
<a data-rel="tooltip" title="" class="well span3 top-block" href="#">
<span class="icon32 icon-color icon-star-on"></span>
<div>Pro Members</div>
<div><?php $this->special_model->pro_user();?></div>
<span class="notification green">4</span>
</a>
但我得到这样的错误
A PHP Error was encountered
Severity: Notice
Message: Undefined index: pro_user
Filename: models/special_model.php
Line Number: 12