When page loads i need access variable from query string - php

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

Related

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

How to pass the data to view in codeigniter

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

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: preserving objects when linking from view

I am new to both PHP and Codeigniter.
I am loading a view from my controller. This view then has a form_open in it that directs to a function within that controller. Can I use variables previously set in my controller within that function?
For example, the constructor of the controller loads a model. Then, another function in this controller calls $this->model->someFunction($id) and sets the $id variable of my model to $id. Later, after a link is clicked in my view, it goes to a different function in the controller which then calls $this-model->printID(). This fails because in my model, $id is not set.
How can I achieve that a link goes to a function that accesses the same object (or model) that I modified earlier? That is, the final echo of $ID returns a blank string - the $ID is not set as I would have expected it to be.
Thanks in advance for the help.
My controller:
class Studio extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('studio_model','',TRUE);
}
function view($id)
{
$this->studio_model->setID($id);
$this->load->view('studio_view');
}
function signup(){
$ID = $this->input->post('ID');
$this->studio_model->signup($ID);
}
}
My model:
Class studio_model extends CI_Model{
public $ID;
function setID($id) {
$this->ID = $id;
}
function signup($ID){
echo $this->ID;
echo $classID;
}
I think that the answer to my question is related to the following: ellislab.com/forums/viewthread/185409/#876547 A PHP instance is not maintained and so each new call instantiates new classes, controllers, etc. As a result, global variables are no longer set if you have left a controller and then re-enter it later.

Codeigniter: Using url segments in a query

I just started working with CodeIgniter and I am having some trouble with the segment-based urls. I understand how to call them doing $variable = $this->uri->segment(2); but whenever I go to the url, I am getting a 404. Is there something I need to do for URI routing?
For example, I am trying to go to localhost/ci/index.php/games/1000 (where 1000 would be a game ID), but I am getting a 404. localhost/ci/index.php/games/ works fine.
In order for that to work you would need to have a controller called games.php with this content
class Games extends CI_Controller
{
public function index($id)
{
echo $id;
}
}
Unless you do something like this
class Games extends CI_Controller
{
public function index()
{
echo 'this is index';
}
public function game($id)
{
echo $id;
}
}
and add this to your routes.php
$route['game/(:any)'] = "games/game/$1";
By default the 2nd segment of the URI is a method (function) within the controller which CI automatically calls.
So in your case you are actually attempting to call a function named 1000() within the games controller, which doesn't exist and therefore results in a 404.
Instead what I think you want to do is call the index() function, and pass the variable 1000 to it.
So if you were to go to localhost/ci/index.php/games/index/1000 you shouldn't get a 404 anymore, however your URI segment will now be wrong to get the variable 1000.
Here is a working example of the controller with the corrected URI segment:
class Games extends CI_Controller
{
// good habit to call __construct in order to load
// any models, libraries, or helpers used throughout this controller
public function __construct()
{
parent::__construct();
}
// default controller
public function index()
{
// this should display 1000
echo $this->uri->segment(3);
}
}

Categories