using static variable in different controller - php

i'm making an application where i'm saving user information in user controller of my code igniter application inside a static variable, now i want to access static variable in other controller, how can i approach that? my code here
<?php
class User extends CI_Controller{
public static $user_data = array();
public __construct()
{
parent::__construct();
self::$user_data = array('value'); // values from the model
}
}
// now i can user that static variable from the view in this controller
class Friends extends CI_Controller{
public __construct()
{
parent::__construct();
if(User::$user_data->isFriends)
{
redirect('person/'.User::$user_data->id);
}
}
}
// how can i access this functionality in codeigniter? it gives error undefined class User not found

static::$user_data = array('value')
shouldn't it be self::... in your User class?

There are a number of issues with your code.
As you have defined that $user_data is an array with public static $user_data = array(); at the top of the User class, it is unnecessary to redefine it on line 7, i.e. self::$user_data = array('value');
Second issue is that the $user_data array will only contain one element, 'value'. I assume by lines 15 and 17 (User::$user_data->isFriends and User::$user_data->id), you are looking for two elements, 'isFriends' and 'id', these will not exist.
This brings me to another issue, the syntax you are using to get an element of the $user_data array will not work. The syntax '->' followed by a property name is used for variables of type Object, and cannot be used for an associative array.
Instead, you should use - for example - $user_data['isFriends'].
I have updated your code below...
<?php
class User extends CI_Controller{
public static $user_data = array();
public __construct()
{
parent::__construct();
// this does the same thing as self::$user_data = array('value');
// but without unnecessarily declaring the array again.
self::$user_data[] = 'value';
}
}
// now i can user that static variable from the view in this controller
class Friends extends CI_Controller{
public __construct()
{
parent::__construct();
if(User::$user_data['isFriends'])
{
redirect('person/'.User::$user_data['id']);
}
}
}
// how can i access this functionality in codeigniter? it gives error undefined class User not found
Try running the code and let me know what errors are being displayed and I will try and resolve them by updating my answer

Related

Setting a variable for storing the user email without repeating the code in every function

I am using Ion_Auth library with Codeigniter 3.0.*. I managed to display the user email I am logged in with with this line of code in the __construct() method of my Admin_Controller:
$this->user_email = $this->ion_auth->user()->row();
But I need to repeat this code:
$data['user_email'] = $this->user_email->email;
In every view method within every controller. I display the variable $user_email in my header.php, which is the same for every page. How do I make it accessible for everyone without repeating this line of code?
Add a class property $data to the Admin_controller class definition.
The $data property will be available to each controller that extends Admin_controller. Because it is a class property it is accessed with the syntax $this->data.
class Admin_controller extends CI_Controller
{
//our new class property
protected $data = array();
public function __construct(){
parent :: __construct();
// do what is needed to get $ion_auth working
}
}
In any class extending Admin_controller set $data['$user_email'] in the constructor. It is then available to every view that is given $this->data
class Some_controller extends Admin_controller
{
public function __construct(){
parent :: __construct();
//I am assuming that by this time $this->ion_auth->user() exists
//so we add a key and value to the class' $data property
//Note the use of the "$this->" syntax)
$this->data['user_email'] = $this->ion_auth->user()->row();
}
public function sets_up_a_view(){
//do stuff until you're ready for the header
//note that we are sending the class property "$this->data" to the view
$this-load->view('header', $this->data);
//load other views as needed using $this-data or other array - your choice
}
public function some_other_view(){
//send class property to view
$this-load->view('header', $this->data);
$data['foo'] = 42;
//send local var to view
$this-load->view('other_parts', $data);
}
}
Notice that both sets_up_a_view() and some_other_view() send the class property '$this->data' to header.php. But in some_other_view() we setup a local variable called $data to send to the other_parts.php view.
The solution was adding these two lines of code in my Admin_controller:
$data['user_email'] = $this->ion_auth->user()->row()->email;
$this->load->vars($data);
With this, every view method in the controllers extending Admin_controller can access this variable.

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 Undefined property when loading a model [duplicate]

This question already has answers here:
Codeigniter - I am looking to use/connect to a different database for one of my controllers and one model
(3 answers)
Closed 10 years ago.
I'm receiving the following errors whilst trying to implement a very simple model controller in codeigniter. I'm new to the framework but as far as I can see this should work.
I've also tried auto loading the model. I am autoloading the database library.
Message: Undefined property: User::$user_model
Fatal error: Call to a member function get_user() on a non-object
The model
class User_model extends CI_Model
{
function __construct()
{
parent::__construct();
}
function get_user()
{
return "test";
}
}
The controller
class User extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
function index()
{
$this->load->model('User_model');
$data['value'] = $this->User_model->get_user();
$this->load->view('user_edit', $data);
}
}
Thanks
just use some thing like this, $this->load->model('user_model'); don't use $this->load->model('User_model'); and make sure you have same name, when the model name like user_mode.php and the class like class User_model extends CI_Model(){} so when you call it from controller use the lower case look like your model name user $this->load->model('user_model')
I think i've found the problem. I had some additional setter methods in my user controller which i've used all the time, didnt think they would be a problem.
public function __set($key, $value)
{
// Check to see that the requested attribute exists and then assign the value
if (property_exists($this, $key))
{
$this->$key = $value;
}
}
turns out I needed to take away one of the underscores as codeigniter doesn't like it.
public function _set($key, $value)
I should have really included the full class but I was trying to keep it as simple as possible!

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