How to pass the data to view in codeigniter - php

Suppose i have a controller called home like this
class Home extends CI_Controller{
public function __construct()
{
parent::__construct();
//load the settings model
$this->load->Model('settings_model');
//get the all settings and store it into a variable
$siteSettings = $this->settings_model->SiteSettings();
//how to pass the data to view
$data['site'] = $siteSettings;
}
public function index()
{
//some code and pass the value to view
$data["some_item"] = "some value";
//Load the view
$this->load->view("home",$data);
}
public function SomeMethod1()
{
//some code and pass the value to view
$data["some_item"] = "some value";
//Load the view
$this->load->view("home",$data);
}
public function SomeMethod2()
{
//some code and pass the value to view
$data["some_item"] = "some value";
//Load the view
$this->load->view("home",$data);
}
}
But my problem is i want to pass $siteSettings to my each method. I don't want to fetch the data every time from settings_model for my different method of home controller. I just want to get the data from database on __construct() and pass the value to each method when i call the different method.
How can i achieve this? Should I use a public variable and store the fetched data to this variable and call the variable from different method?

Set that in session. and can access all over the project.
public function __construct()
{
parent::__construct();
$this->session->unset_userdata('site'); # unset on each and every time
$this->load->Model('settings_model');
$siteSettings = $this->settings_model->SiteSettings();
$this->session->set_userdata('site', $siteSettings); # Set after end of __construct
}
Or do like this
public function __construct()
{
parent::__construct();
$this->load->Model('settings_model');
$this->data['site'] = $this->settings_model->SiteSettings();
}
In function
$this->load->view("name",$this->data);

Related

passing parameter to view give infinite loop / page refresh in codeigniter 3

I have some problem with CodeIgniter 3.
When I try to access URL with the parameter like these http://localhost/crm/Customer/update/3
The result is infinite loop page refresh.
This does not happen if I access other view or URL.
For example : http://localhost/crm/Customer/list
Below is my controller file :
class Customer extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->helper(array('form', 'url', 'customer'));
$this->load->database();
$this->load->library('session');
$this->load->model(array('Auth', 'Model_customer'));
$this->_init();
}
function _init() {
$this->output->set_template('index');
// $this->load->section('sidebar','template/sidebar');
}
public function index(){
...
$this->load->view('customer/index');
}
function update($id) {
// TEST
$data['id'] = $id;
$this->load->view('customer/update2', $data);
}
}
Any help will be valuable. Thanks

When page loads i need access variable from query string

I have controller in codeigniter that name is Product. When I access product controller, I am passing cateid variable in URL as query string parameter and getting on product controller.
I have created two methods in controller with names index and productajax . When I click on product link then controller index method executes. I need to use cateid variable value from URL query string in productajax method when page is initially loaded or controller index method being called.
class Product
{
public function index()
{
$cateid= $this->input->get('cateid'):
$this->load->view('product'):
}
public function productajax()
{
// I need to get cateid from index function
}
}
Hope this will help you :
You can do it by creating a public or private variable $cate_id in class and when index loads then
assign $cate_id variable in your index method and when you access productajax you can get cat id in productajax by using $this->cat_id
Your controller should be like this :
// Product class
class Product extends CI_Controller
{
public $cate_id;
public function __construct()
{
parent::__construct();
$this->load->helper('url');
}
public function index()
{
$cateid = $this->input->get('cateid'):
$this->cate_id = $cateid;
$this->load->view('produxt');
}
public function productajax()
{
echo $this->cate_id;
}
}
For more : http://php.net/manual/en/language.oop5.php

codeigniter global array for a model

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..

Persistent properties in Codeigniter

With Codeigniter 2.xx, how can I keep the values of properties set between method calls that require new HTTP request? I'm am trying to avoid getting and setting the same data multiple times. Example of current:
Model:
class Plan_model extends Ci_model{
public $plan_id;
public function __construct()
{
parent::construct();
}
public function set_plan_id($plan_id)
{
$this->plan_id = $plan_id; //Becomes NULL with new http request, want to keep value
}
}
Controller:
class Plan extends Ci_controller{
public function __construct()
{
parent::__construct();
$this->load->model('plan_model');
}
public function set_plan_id()
{
$this->plan_model->set_plan_id(3)
redirect('/plan/get_plan_id');
}
public function get_plan_id()
{
$plan_id = $this->plan_model->plan_id; //produces NULL, Expecting 3
}
You probably are looking at using sessions to define the info.
$this->session->set_userdata($plan_id);
Here's the link for session data: https://ellislab.com/codeigniter/user-guide/libraries/sessions.html

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.

Categories