I'm new to CI and I'm trying to access a private variable through the application, but I set a value to the variable, next time I try to access the function(that I called from a submit form in my view), the private variable that I had set is empty. Can someone help? thx
class Example extends CI_Controller{
private $_variable;
public function __construct()
{
parent::__construct();
}
public function index()
{
//value from database
$this->_variable = 'somevalue';
}
//calling this function from a view
public function some_method()
{
// code...
// $this->_variable returning without any value
}
}
Your views shouldn't directly try to access methods of your controller, instead, you should send those while calling the view in the second arg:
See Codeigniter's docs related to this (I assume you're spanish, because you use "variable").
$args = Array( "var1" => "variable", "var2" => "variable" );
$this->load->view("some_url", $args);
Then $var1 and $var2 will be available in your view.
Your some_method is not actually a view. At best it could be a Example Controller function that will calling from a view. Your index() function is actually assigning the value in your private $_variable, so to pass the value to your view, you must first call the index() function to assign variable and the value will be available to your some_method(). Example of how you'll pass the variable to your view is given bellow.
public function some_method()
{
return $this->_variable;
}
In Your view, to access the variable:
echo $this->some_method();
I believe this will help you to show your private variable in your view.
Related
I have already tried lots of ways. add library, add config file, add a controller, just add in same controller.........etc.
This also have same problem:
(this is add in same controller)
<?php
class Test extends CI_Controller{
public $data = array();
public function __construct(){
parent::__construct();
//if call add_data() here, it is work
}
function add_data(){
$arraya = array('a'=>'aa', 'b'=>'bb');
$this->data = $arraya;
}
function index(){
$this->add_data();
}
function want_print(){
print_r($this->data);
}
}
?>
if I call add_data in index, i cannot get any data in want_print()....
if I call add_data in the construct, i can get data in want_print()..
Please anyone help me solve this problem?
I don't want to call it in construct because i will not call it every time...
You can set the data in your want_print() function like this:
function want_print() {
$this->index();
print_r($this->data);
}
I am developing a CRM for our agency in Codeigniter and I have a question that I can't seem to find a solid answer on. If I have a task that I do on the majority of methods in a controller, is there any way to define that action only once? For instance...
Every view call gets passed the $data variable, like so...
$this->load->view('templates/template.php', $data);
So if I am doing something like getting the admins information in every function of the controller, how can i tell it to do that action ONE time and pass it to all my functions.
Like this...
$data['admin'] = $this->Crm_model->get_admin();
I've tried putting that ^ in the constructor and it doesn't work. Any ideas?
If you do:
$data['admin'] = $this->Crm_model->get_admin();
in the constructor, $data's scope is limited to the constructor. You need to create it as a class property so it is scoped to the entire class. Do this instead
$this->data['admin'] = $this->Crm_model->get_admin();
in the constructor, and then in other methods, you can access the array by doing $this->data
Here's an example:
class Foobar extends CI_Controller {
public function __construct() {
$this->data['foo'] = "bar";
}
public function index() {
// use the class property data here to add more info to it
$this->data['hello'] = "world";
// now pass this to the view
$this->load->view('myView', $this->data);
// myView will receive both $foo and $hello
}
}
I am trying to initialize data in index function of controller, so that initialized data can be used in subsequent functions of controller. But the problem is data is not being displayed when I am trying to access it from other function. All of this is just to follow a sort of object oriented pattern.
Here is my code.
class Dashboard extends CI_Controller
{
private $account_data; /*Declaration*/
private $profile_data;
function __construct() {
// code...
}
function index() /*Here I am initializing data*/
{
$this->load->model('db_model');
$this->account_data = $this->db_model->get_row();
$this->profile_data = $this->db_model->get_row();
$this->load->view('user/dashboard');
}
function function account_details()
{
print_r($this->account_data); // This displays nothing
}
/*other function...*/
}
Idea is to get data once and use it for other functions and if data is updated again calls a function to initialize it.
But it is not working out. Please help me. Also suggest if I am following right approach.
Thanks for your time.
index method is not initializer, its default page/sub_method,
if you call the "*account_details*" in url as index.php/dashboard/account_details the index wont be called.
try put the code on constructor,
class Dashboard extends CI_Controller
{
private $account_data; /*Declaration*/
private $profile_data;
function __construct() { /*Here I am initializing data*/
parent::CI_Controller(); // Thank you Sven
$this->load->model('db_model');
$this->account_data = $this->db_model->get_row();
$this->profile_data = $this->db_model->get_row();
}
function index()
{
$this->load->view('user/dashboard');
}
function function account_details()
{
print_r($this->account_data); // This displays nothing
}
/*other function...*/
}
Note : don't the models or other computations on __construct() if you don't need on all methods of this controller.
create a private method like "model_initializer()" put this codes on this scope, and the call it in your other methos as $this->model_initialize(); if you need.
Thanks yo Sesama Sesame for note,
I am developing a stats site in Codeigniter locally. I have a url like localhost/sitename/player/show_profile/PlayerName
I currently have the following:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Player extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('player_model');
$player_name = $this->uri->segment(3);
}
public function index()
{
echo "index";
}
public function show_profile($player_name)
{
$data['player_stats'] = $this->player_model->get_stats( $player_name );
$this->load->view('player/player_stats', $data);
}
}
?>
This works, but my question is regarding the $player_name variable. I have $player_name = $this->uri->segment(3); in the __construct so it's available to all of the class methods. Is this the way I should be doing it?
Is this safe?
Fist of all, there is no point in assigning the variable in the constructor because it's going to get overwritten. When you pass CI a url like localhost/sitename/player/show_profile/PlayerName, anything passed the method (i.e. PlayerName) get's set as the parameters. Therefore, your variable in
public function show_profile($player_name){
is already set when you get to your method code.
Secondly, I agree with Peter's:
protected $player_name;
for making it globally accessible in the controller. BUT, I don't agree with setting it in the constructor. If you have another method in this controller that passes a variable in that spot, you're going to get the wrong data in there. Set it in the method you called:
public function show_profile($player_name){
$this->player_name = $player_name;
$data['player_stats'] = $this->player_model->get_stats( $player_name );
$this->load->view('player/player_stats', $data);
}
What you could do is define a class variable called $player_name and in the constructor set this to segment(3).
class Player extends CI_Controller
{
protected $player_name;
public function __construct() {
parent::__construct();
$this->load->model( 'player_model' );
$this->player_name = $this->uri->segment( 3 );
}
public function index() {
echo "index";
}
public function ( show_profile ) {
$data['player_stats'] = $this->player_model->get_stats( $this->player_name );
$this->load->view( 'player/player_stats', $data );
}
}
This way will be able to access the $play_name variable anywhere in the class.
You could also check to see if it's set using the $this->uri->uri_to_assoc(n) method and check to see if the key/value isset() http://codeigniter.com/user_guide/libraries/uri.html.
Peter
Im trying to get the total unread accounts from the database, the value is assigned to $data['head']
I want to make the $data['head'] available globally so it will be automatically loaded into the template and displayed on the header.
What is the best way to do this?
Below is my controller
function __construct()
{
parent::__construct();
$this->load->model('process_model');
$data['headbody']='includes/header';
$data['head'] = $this->process_model->loadnew($this->session->userdata('id'));
}
function invform()
{
$this->load->model('slave');
$data['body']='slave-account';
$data['questions'] = $this->slave->loadq($this->uri->segment(3));
$this->load->view('includes/template',$data);
}
View
$this->load->view($head);
$this->load->view($body);
$this->load->view('includes/footer');
You first need to make $data into a variable outside the function, using variable scope. Can be private or public. I made it private in this case.
Here's a quick revision:
private $data = array();
function __construct()
{
parent::__construct();
$this->load->model('process_model');
$this->data['headbody']='includes/header';
$this->data['head'] = $this->process_model->loadnew($this->session->userdata('id'));
}
function invform()
{
$this->load->model('slave');
$this->data['body']='slave-account';
$this->data['questions'] = $this->slave->loadq($this->uri->segment(3));
$this->load->view('includes/template',$this->data);
}
Notice the $this->data, instead of $data. When we're accessing variables within the same class, but outside of the function, we use $this.