如何在COdeigniter的控制器的每个功能中自动加载功能 [英] How to get a functiona automatically load in every function of controllers in COdeigniter

查看:53
本文介绍了如何在COdeigniter的控制器的每个功能中自动加载功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个示例脚本

<?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');

      }
}

?>

您可以看到我在每个函数中总是有这两行

as you can see i have these two lines always in every function

$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();

现在我想要一个替代项,以便自动将这两个函数称为

now i want a substitute such that automatically to have these two functions called

说些什么,我定义了一个返回数组的函数

say something i define a function which returns the array

而当我只是编写此脚本时

and when i just write this script

echo pro_user(),然后在应用程序/管理员中显示pro_user数量

echo pro_user(), then it displays the pro_user quantity

/config/autoload.php

say in the application/admin/config/autoload.php

我已经添加了此脚本

$autoload['model'] = array('special_model');

special_model中的函数是这样的

the function in the special_model is like this

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视图中,我想要这样的东西,想要写这样的东西

in the view php like i want something like this want to write something like this

<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


推荐答案

您可以在ci_controller中定义它们,每个控制器都进行扩展,以便在每个控制器功能中也具有它们

you can define these in the ci_controller , every controller extends that so you will have them in every controller function as well

转到

system/core/codeigniter.php 

cahnge

class CI_Controller {

    private static $instance;

class CI_Controller {

    private static $instance;
        public $pro_users;
        public $any_thing ;

,然后在 __ construct()在同一页面中的功能

and then at the end of __construct() function in the same page

添加

$this->pro_users = $this->db->query("SELECT count(*)AS pro_user FROM user_table WHERE membership_type <>0")->result_array();

现在在每个控制器中,您可以通过调用

now in every controller you can have that by calling

$this->pro_users

这篇关于如何在COdeigniter的控制器的每个功能中自动加载功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆