Codeigniter: preserving objects when linking from view - php

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.

Related

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

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.

Can't access property in abstract class via sub class

I am trying to build an abstract base controller that will extend all other controllers. So far I have something like:
abstract class BaseController {
protected $view;
protected $user;
public function __construct() {
$this->view = new View; //So a view is accessible to subclasses via $this->view->set();
$this->user = new User; //So I can check $this->user->hasPermission('is_admin');
}
abstract function index();
}
class UserController extends BaseController {
public function index() {}
public function login() {
if($this->user->isLoggedin()) {
redirect to my account
}
else {
$this->view->set('page_title', "User Login");
$this->view->set('sidebar', $sidebar); //contains sidebar HTML
$this->view->set('content', $content); //build main page HTML
$this->view->render();
}
}
}
The problem i get is I get errors like this:
Call to a member function set() on a non-object in C:\xampp\htdocs\program\core\controllers\admin.controller.php on line 44
If I put the $user and $views properties in the main controller (ie UserController), everything works fine. But I only want to set up these objects once (in the base controller) and not have to add $this->view = new View; in all my controllers.
FIXED: I overrode my constructors and I thought you couldn't call parent::__construct() on abstract classes.
What you are trying to do should work. Make sure you aren't covering up your constructor in UserController. (i.e., if it has a constructor, it needs to call its parent constructor.)
Otherwise, do some debugging to see where $this->view is being reset.
Your code works for me. You are either overriding your __construct() method in UserController, or you are overridding the view field with something other than a View object.
What you have in this form would work.

Same data variable passing to view

In my project, I have one search section with 3 select box. I passed the value to it using
$data['restaurant_all']=$this->restaurant_model->get_all('','','','','','yes')->result();
$data['state_all']=$this->state_model->get_all();
$data['menu_all']=$this->menu_model->get_all('all','','','','','','yes')->result();
$data['restaurant']=$this->input->post('restaurant');
$data['state']=$this->input->post('area');
$data['food_type']=$this->input->post('menu');
I need this statement in all my pages. In there any way to accomplish this without writing these statements in all the pages
a. extend the default controller by creating a file MY_Contoller.php at a suitable location.
b. create a custom class that will extend the default controller.
c. add a protected or public variable $data to custom class.
e. do something with data using __construct()
d. make every controller extend the custom controller.
e. you can access this variable like any other class variable.
example code:
MY_Controller.php
class APP extends CI_controller {
protected $data;
function __construct() {
parent::__construct();
$this->_init();
}
function _init() {
$this->data['state'] = $this->input->post('area');
}
}
normal controllers:
class Welcome extends APP {
function __construct() {
parent::__construct();
}
function view() {
/* pass this data value like normal data param */
$this->load->view('some_view', $this->data);
}
}
hope it helps.
Use constants, in /config/constants.php

Categories