我有一个示例脚本
<?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 数量
在 application/admin/config/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