0

I have a sample script

<?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(), then it displays the pro_user quantity

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

i have added this script

$autoload['model'] = array('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'];
        }

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>

but i am getting the error like this

A PHP Error was encountered

Severity: Notice

Message: Undefined index: pro_user

Filename: models/special_model.php

Line Number: 12

3 Answers 3

1

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

go to

system/core/codeigniter.php 

cahnge

class CI_Controller {

    private static $instance;

to

class CI_Controller {

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

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

add

$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
Sign up to request clarification or add additional context in comments.

Comments

0

I think the best way is to write a codeigniter hook and set the value of a variable with that function, and then pass that variable to the view. Calling model functions in views breaks the MVC pattern.

You can use post_controller or post_controller_constructor, more info:

Comments

0

I think the best solution is to write a helper and load it in autoload.

e.g

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

function alphabetArray() {
    $alph = array();
    for ($i=65; $i<=90; $i++) {
        array_push($alph, chr($i));
    }
    return $alph;
}

now anyehre in code I can call (code from view)

<?php
     echo "|";
     foreach (alphabetArray() as $alphabet) {
     echo '<a href="#"> '.$alphabet.' </a> |';
}
?>

As you can see my alphabetArray() function is "global" function and it can be used in models/views/controllers

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.