codeigniter global array for a model - php

In my controller I want to declare a global variable which will always fetch all areas from my db
currently I'm passing $data['dropdowns']
in all methods in my class
{
$data['dropdowns']=loading some other model method
$this->load->view('commons/header',$data);
}
{
$data['dropdowns']=loading some other model metod
$this->load->view('commons/header',$data);
}
{
$data['dropdowns']=loading some other model metod
$this->load->view('commons/header',$data);
}
{
$data['dropdowns']=loading some other model metod
$this->load->view('commons/header',$data);
}
the thing is I want to now send $data['area'] to all the views without having to declare it again and again in each method
$data['area']= $this->area_model->get_all_locations();

You want to add global variable , but as per my suggest to use global function to use any where to using to send parameter, so please check below my code.
Note : please load your model in application/config/autoload.php file
This is simple demo :
controller
{
$data['dropdowns']=$this->your_model_name->get_records('table_name','select field like id, name');
$this->load->view('commons/header',$data);
}
{
$data['dropdowns']=$this->your_model_name->get_records('table_name','select field like id, name,user_name');
$this->load->view('commons/header',$data);
}
Your model
function get_records($table_name,$field_name)
{
$this->db->select("$field_name");
$this->db->from("$table_name");
$query=$this->db->get();
return $query->result_array();
}

create a base_controller and placed in application/core
class base_controller extends CI_Controller
{
public $area = array();
function __construct()
{
// Call the Controller constructor
parent::__construct();
$this->get_area();
}
public function get_area() {
$this->load->model('area_model');
$this->area= $this->area_model->get_all_locations();
}
}
now $this->area is available in all controller which extends base_controller and all common functionality you can put here
class homepage extends base_controller{
function __construct()
{
// Call the Controller constructor
parent::__construct();
}
public function index()
{
$data = $this->area; // call this wherever u need
$this->load->view('commons/header',$data);
}
}
importantly $this->area; one can use directly inside view

Create a helper for your custom functions
Eg: custom_helper.php then load your custom helper in autoload.php
In custom_helper.php, create a function for getting area.
if (!function_exists('get_area')) {
function get_area() {
$CI = & get_instance();
$area= $CI->area_model->get_all_locations();
return $area;
}
}
You can call get_area() in your views without declaring in controllers..

Related

Unable to locate the specified class: Session.php, When call a function from another controller in Codeigniter

contrller:News.php
This is my controller News
<?php class News extends CI_Controller {
public function __construct()
{
}
public function getShowIN_News()
{
return $result;
} } ?>
contrller:Category.php
This is my controller Category
<?php class Category extends CI_Controller {
public function __construct()
{
}
public function category()
{
require('news.php');
$test = new News();
$data["headlines"] = $test->getShowIN_News();
} }?>
By using an empty constructor, you're making it so that CI_Controller::__construct() isn't called, and that's where everything in the framework is initialized.
I know you've put it there to hack it so you can call one controller from another, but it is very intentionally made that way, exactly so you don't do this.

How to load a model in a master controller on codeigniter framework?

I am creating a master controller so that every other controllers on my app extend the master (MY_Controller).
My problem is how to make loading a model more abstract, let me show you the code to better explain.
class MY_Controller extends CI_Controller
{
protected $model;
function __construct()
{
parent::__construct();
}
function get($order_by)
{
$this->load->model($this->model);
$query = $this->$this->model->get($order_by);
return $query;
}
}
I declare a protected variable $model in the master controller so that on the extending controller i can asign it a value:
class Home extends MY_Controller {
public function __construct() {
parent::__construct();
$this->model = "home_model";
}
public function index()
{
$test = $this->get('id');
}
}
The problem is that on MY_Controller i cant load the model
$this->$this->model->get();
i get the following error: Message: Object of class Home could not be converted to string
any help will be appreciated , thank you !
Why do not save the value you assign to protected global $model variable in a variable inside the function as such:
class MY_Controller extends CI_Controller{
protected $model;
function __construct(){
parent::__construct();
}
function get($order_by){
$_model = $this->model // I added this line.
$this->load->model($_model); // I modified this line.
$query = $this->$_model->get($order_by); // I modified this line.
return $query;
}
}
The $this->$this->model->get() is your problem. If you want to use an object property ($this->model) inside a chain like this, you need to wrap it in braces: $this->{$this->model}->get().

how to pass same fetched data on my everypage on codeigniter

Here is my controller code
public function index()
{
$this->load->model("mod_home");
$data['avoinics'] = $this->mod_home->getAvoinics();
$data['dir']="home";
$data['page']="index";
$this->load->view('main',$data);
}
for another page
public function about()
{
$this->load->model("mod_home");
$data['avoinics'] = $this->mod_home->getAvoinics();
$data['dir']="home";
$data['page']="about";
$this->load->view('main',$data);
}
But i don't want to send $data['avoinics'] again again. Is there any way to access a data from anypage.
How to use same data in a single view more than time.
foreach($avoinics as $avoinics):
$name=$avoinics->sc_name;
echo '<li>'.$name.'</li>';
endforeach;
if i use it again on same view page it's sowing error...
Yes, you can:
Create a global array
private $data = array();
In constructor
$this->load->model("mod_home");
$this->data['avoinics'] = $this->mod_home->getAvoinics();
Now your function will look like this
public function index() {
$this->data['dir']="home";
$this->data['page']="index";
$this->load->view('main',$this->data);
}
For second part, do not change the variable value
foreach($avoinics as $record){
echo '<li>'.$record['name'].'</li>';
}
$avoinics is intact now. You can use it again until you do not modify it.
A good example you might easily understand is when you need to call certain scripts or css files for a specific controller. You won't call it in every single page but yes in the constructor.
class yourController extends CI_Controller
{
private $data;
public function __construct()
{
$this->data['css'] = array('file1.css', 'file2.css');
$this->data['js'] = array('jquery.min.js', 'jquery-ui.min.js');
}
public function index()
{
$this->load->view('yourView', $this->data);
}
public function about()
{
$this->load->view('yourView', $this->data);
}
}
I recommend you to extend core ci_controller with my_controller and declare there your variable in constructor. Then in your new controllers extend your my_controller where you will have variable declaration.

Access an array in all views in codeigniter

I made an array in MY_Controller class placed on core folder. In its constructor i fetched records from db so as to make navigation menu in my views. Since i have different page layouts so i cannot call the same header view every where. for this reason i made a core class as per my understanding which i am not sure is right or not. below is the code for my controller
class MY_controller extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('Category_model');
$data['parent'] = $this->Category_model->getParentCategories();
$data['child'] = $this->Category_model->getChildCategories();
}
}
my default controller is main
class Main extends MY_controller {
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->view('home/header',$data);
$this->load->view('home/footer');
}
Now in my header view i am receiving undefined variable parent and child error. I want this two variables available in all the views so that i do not have to define those two variables in every controller.
Thanks
You may try something like this:
class MY_controller extends CI_Controller
{
$commonData = array();
function __construct()
{
parent::__construct();
$this->load->model('Category_model');
$this->commonData['parent'] = $this->Category_model->getParentCategories();
$this->commonData['child'] = $this->Category_model->getChildCategories();
}
}
Then use $this->comonData in your index method instead of $data to pass to the view:
public function index()
{
$this->load->view('home/header', $this->comonData);
$this->load->view('home/footer');
}
Now it'll be available in the header view and since it's at the top of other views then you may use it further, unless you override it with other value in any class.

codeigniter declaring a class in to one variable

i am trying to use $admin->function() instead $this->admin_model->function()
when i tried to declare a variable $admin=new Admin_model; in constructor and use it in other functions it gives error..
my code is given below, i don't know much about OOP concept, somebody please help.
class Admin extends CI_Controller {
var $admin;
function __construct() {
parent::__construct();
$this->load->model('admin_model');
$admin=new Admin_model;
}
public function index($value='')
{
if(!$admin->is_admin_logged_in()){
redirect('admin/login?r='.urlencode(current_url()));
}
$data['loggedin']=TRUE;
$data['account']=$this->session->all_userdata();
$this->load->view('pages/admin-home',isset($data)?$data:NULL);
}
}
presently i am using this method
public function login()// this function belongs to the same controller mentioned above
{
$r=isset($_GET['r'])?urldecode($_GET['r']):'admin';
$admin=new Admin_model;
if($admin->is_admin_logged_in()) redirect($r);
}
i don't want declare $admin=new Admin_model; in every single function and want to make the code look good and clean, so don't like to use $this->admin or $this->admin_model either.
You are trying to give an alias to a model (as what I can see)
All you have to do is:
class Admin extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('admin_model', 'admin');
}
public function index($value='')
{
if(!$this->admin->is_admin_logged_in()){
redirect('admin/login?r='.urlencode(current_url()));
}
$data['loggedin']=TRUE;
$data['account']=$this->session->all_userdata();
$this->load->view('pages/admin-home',isset($data)?$data:NULL);
}
}
Notice where the alias of the model is given
$this->load->model('admin_model', 'admin');
And how it is used
if(!$this->admin->is_admin_logged_in()){

Categories