public function showcart() {
$showcartdata = array();
$showcartdata['showcart'] = $this->ShowcartModel->showcart_mod();
$this->load->view('templates/header');
$this->load->view('showcart_view.php',$showcartdata);
$this->load->view('templates/footer');
}
public function place_order() {
$this->load->view('templates/header');
$this->load->view('placeorder_onsubmit.php',$showcartdata);
$this->load->view('templates/footer');
}
I have looked all over to find a way to share the variable in the first method "$showcartdata['showcart']" with the methods below to be used in a view file. I tried to use Global variable but in vain. Is there any smart way to accomplish the this task. Thanks a lot for the help and your time.
Define your variable in controller's constructor as below.Then it is accessible from all controller's functions.
public function __construct()
{
parent::__construct();
$showcartdata = array();
$showcartdata['showcart'] = $this->ShowcartModel->showcart_mod();
}
public function showcart() {
$this->load->view('templates/header');
$this->load->view('showcart_view.php',$showcartdata);
$this->load->view('templates/footer');
}
public function place_order() {
$this->load->view('templates/header');
$this->load->view('placeorder_onsubmit.php',$showcartdata);
$this->load->view('templates/footer');
}
One way to do that is using GLOBALS
$_GLOBALS['global_var'] = 'Some Shareable Data';
function func1()
{
$x = $_GLOBALS['global_var'];
//function codes
}
Another way is using ENVIRONMENT VARIABLES
To know more visit this and this
Also if the value is static you can define CONSTANTS
define('MY_CONST', 'static data');
function func1()
{
$x = MY_CONST;
function codes
}
Related
I don't know if the title above is an correct title about my question.
Is there a way to declare a variable which we can access it from anywhere in view without need to redefine it again in each function in controller?
for example in controller file Students.php contains many function that handle the views, take a look below :
public function __construct() {
parent::__construct();
$data['count_student'] = $this->m_data->allStudent(); // count all students
}
public function index() {
$data['content'] = 'view-students';
$this->load->view('frontend/header' , $data);
}
public function showDetails() {
$data['content'] = 'view-detail-students';
$this->load->view('frontend/header' , $data);
}
I expected we can access $count_student in both view-students.php and view-detail-student.php without to define $data['count_student'] = $this->m_data->allStudent(); on each function that handle the view.
Is there possible ways to do that?
In the application/core/ create MY_Controller.php
<?php
class MY_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('m_data');
$this->data['count_student'] = $this->m_data->allStudent();
}
}
Controller use $this->data
class Students extends MY_Controller {
public function __construct() {
parent::__construct();
}
public function index() {
$this->data['content'] = 'view-students';
$this->load->view('frontend/header', $this->data);
}
public function showDetails() {
$this->data['content'] = 'view-detail-students';
$this->load->view('frontend/header', $this->data);
}
}
Now that you have extended the controller you shoudld be able to just go like
<?php echo $count_student;?>
On the view
I noticed that you can access variables in views without passing them if you declare them in the controller with $this->. Probably because they default to public visibility.
public function __construct() {
parent::__construct();
// $data['count_student'] = $this->m_data->allStudent(); // count all students
//
$this->count_all_the_students = $this->m_data->allStudents();
}
public function index() {
$data['content'] = 'view-students';
$this->load->view('frontend/header' , $data);
}
And then in the view you can use $this->count_all_the_students without putting it in the $data array.
<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>
<?php
echo 'I have misled ' . $this->count_all_the_students . ' students with my MVC breaking suggestions.';
If you are using the same variable in every controller function then you have to declare it as follows
class Example extends CI_Controller{
protected $data = array();
public function __construct() {
parent::__construct();
$this->data['count_student'] = $this->m_data->allStudent();
}
public function index() {
$this->data['content'] = 'view-students'; //variables are same
$this->load->view('frontend/header' , $this->data);
}
public function showDetails() {
$this->data['content'] = 'view-detail-students'; //variables are same
$this->load->view('frontend/header' , $this->data);
}
}
This applies only if you have common variables for all views in this controller.
If you are using different variables then use the following code.
class Example extends CI_Controller{
protected $count_student= "";
public function __construct() {
parent::__construct();
$this->count_student = $this->m_data->allStudent();
}
public function index() {
$data['content'] = 'view-students'; //Variable is different
$data['count_student'] = $this->count_students;
$this->load->view('frontend/header' , $data);
}
public function showDetails() {
$data['details'] = 'view-detail-students'; // Variable is different
$data['count_student'] = $this->count_students;
$this->load->view('frontend/header' , $data);
}
}
I'm not CI programmer, just trying to learn it. Maybe this is wrong approach, please advice.
my controller(not in sub directory) :
class Users extends CI_Controller {
function __construct() {
parent::__construct();
}
public function index($msg = NULL) {
$this->load->helper(array('form'));
$data['msg'] = $msg;
$this->load->view('user/login' , $data);
}
public function process_logout() {
$this->session->sess_destroy();
redirect(base_url());
}
}
And a route for login :
$route['user/login'] = 'users/index';
Problem is when I wanna logout, it shows me 404 because I do not have it in my route :
$route['user/process_logout'] = 'users/process_logout';
and in my view I put logout
When I add that, it works, and that is stuppid to add a route for everything. What I'm I doing wrong, please advice.
Thank you
Don't know why you are trying to implement login feature in index() function. However since you said you are learning CI I'm telling something about _remap() function.
Before that. You can try the following routing:
$route['user/:any'] = 'users/$1';
$route['user/login'] = 'users/index';
If you want to take value immediately after controller segment you need to use _remap() function and this function may be solve your routing problem, i mean you don't need to set routing. Lets implement your code controller 'users' using _remap() function.
class Users extends CI_Controller {
private $sections = array('login', 'logout');
function __construct() {
parent::__construct();
}
public function _remap($method)
{
$section = $this->uri->segment(2);
if(in_array($section, $this->sections))
call_user_func_array(array($this, '_'.$section), array());
else show_404(); // Showing 404 error
}
private function _login()
{
$msg = $this->uri->segment(3);
$this->load->helper(array('form'));
$data['msg'] = $msg;
$this->load->view('user/login' , $data);
}
public function _logout() {
$this->session->sess_destroy();
redirect(base_url());
}
}
I am using Tank_auth for user authentication in codeigniter. To check if any user is logged in or not, if he is then to get the username and id I need to use the following code in every functions of my controllers, which is very irritating.
// If any user is logged in get his/her userid or name
if ($this->tank_auth->is_logged_in()) {
$data['user_id'] = $this->tank_auth->get_user_id();
$data['username'] = $this->tank_auth->get_username();
}
So , I was wondering if I could make life easier by putting the following code inside the function __construct() like following,
but its not working.
Could you please tell me how to get it work?
Thanks in Advance :)
function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->library('tank_auth');
if ($this->tank_auth->is_logged_in()) {
$data['user_id'] = $this->tank_auth->get_user_id();
$data['username'] = $this->tank_auth->get_username();
}
}
if you plan to use this array only inside this controller, you can use it like this:
//outside the functions define the $data as controller class property:
private $data = array();
function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->library('tank_auth');
if ($this->tank_auth->is_logged_in()) {
$this->data['user_id'] = $this->tank_auth->get_user_id();
$this->data['username'] = $this->tank_auth->get_username();
}
}
//and then use it like
function index(){
$this->load->view("something.php",$this->data);
}
This is what I have:
class Calendar extends CI_Controller {
public $extension;
function __construct()
{
parent::__construct();
$this->extension = "";
}
public function index($page_id, $extension=null)
{
if(!is_null($extension))
{
$this->extension = $extension;
}
$this->firephp->log($this->extension);
$this->load->view('/modules/calendar_view', array("page_id" => $page_id, "extension" => $this->extension));
}
public function update_calendar($width, $page_id, $new_month=null, $new_year=null)
{
$this->firephp->log($this->extension);
}
}
The correct value echos out in the index method fine, obviously. But the view it loads then calls the second method and the global variable is not set.
Is there a way of setting it until it's explicitly changed?
Thanks.
public $extension is not a global variable here. I believe flashdata is what you are looking for
$this->session->set_flashdata('item', 'value');
In your next call you can get the item as
$this->session->flashdata('item');
These are flushed in next execution.
I know this is probably simple, but I'm not getting. I've created a library, and I want to load the parameters from a config file. So here's an example of what I have:
// libraries/Mylib.php
class Mylib {
var $ci;
var $key;
public function _construct {
$this->ci =& get_instance();
$this->ci->config->load('mylib');
$this->key = $this->ci->config->item('key');
}
public function myKey() {
return "Key=" . $this->key;
}
}
// config/mylib.php
$config['key'] = 'randomcharacters';
I load the library, and try to print out the myKey function, but it just returns "Key=", without the actual key. What am I missing?
It seems like you missed an underscore for your constructor:
instead of
public function _construct () {
you should use
public function __construct () {