CodeIgniter Forums

Full Version: How to set and access global variable through controller functions?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I might have a wrong approach on this one BUT, is it possible to set a global variable in one controller function and access it through another one?

Here's what i mean:

PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

class 
Groups extends CI_Controller {
 private 
$page_id ''//

 
public function view($slug false){
  
$this->load->model('some_random_model'); //Loading model
  
$info $this->some_random_model->get_info($slug); //Getting more info
  
  
$this->page_id $info['page_id']; //Setting a value in page_id variable
 
}

 public function 
anotherFunction(){
  
$page_id $this->page_id;

  
//Some code here

  
$this->load->view('group/anotherFunction');
 }




Is this possible, or what's the best practice on that?

To give you a hint on what i'm trying to achieve.

Using an ajax request with get parameters from "view" function, i'm calling "anotherFunction()" and trying to get some info. What i want is to reassure that the "page_id" is valid and the user is registered in that page.

In other words i want to handle those data in controller and not passing them with get parameters in the client side.

I hope i made my self clear.

If you have any questions or suggestions feel free to ask! 

Thanks in advance!!
Ajax is used to run a server sided script from a view (not from a function in a controller), and most times return information to the view and update the DOM without refreshing the page. You'll have to rely on Javascript to do that (that's the j in Ajax).

What you can do, is pass the page_id to the view (from your "view" function). Put the Ajax function in that view to run the "groups/anotherFunction" function. The view knows the page_id, so it can pass it (by get or post) to "anotherFunction".
Don't make "anotherFunction" load another view, but let it echo the information that you want to get in the original view.

There's lot of Ajax examples combined with CodeIgniter out there on the internet, e.g. on StackOverflow.
(09-14-2019, 12:43 PM)Wouter60 Wrote: [ -> ]Ajax is used to run a server sided script from a view (not from a function in a controller), and most times return information to the view and update the DOM without refreshing the page. You'll have to rely on Javascript to do that (that's the j in Ajax).

What you can do, is pass the page_id to the view (from your "view" function). Put the Ajax function in that view to run the "groups/anotherFunction" function. The view knows the page_id, so it can pass it (by get or post) to "anotherFunction".
Don't make "anotherFunction" load another view, but let it echo the information that you want to get in the original view.

There's lot of Ajax examples combined with CodeIgniter out there on the internet, e.g. on StackOverflow.

Yeap i'm familiar with Ajax.

As for loading a second view with anotherFunction, that's because i've created a calendar with dynamic data in each cell. So it's kinda complicated to return them as json encoded data. Moreover i have tabs which (after clicking the button) the ajax pass data with get to anotherFunction (page_id and some other stuff) and returns the filtered data in the view.
I hope you understood what i've tried to do here.

As for the last one, i've searched a big amount of pages and the best i found was to place a hidden input somewhere in the "view" page and then pass the parameters in controller with ajax.
You should create a Page_Controller in /application/core/Page_Controller.php

And extend it from every controller you wan't to access that data with.

PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

class 
Page_Controller extends CI_Controller
{
    protected 
$page_id;


PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

class 
Groups extends Page_Controller {
...


I just re-read your question. And yes that's how you are supposed to use them.

You should however execute anotherFunction() inside the controller instead of Ajax, as people can just omit executing them.

PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

class 
Groups extends CI_Controller {

 public function 
view($slug false){
  
$this->load->model('some_random_model'); //Loading model
  
$info $this->some_random_model->get_info($slug); //Getting more info
  
  
$this->anotherFunction($info['page_id'])
 }

 private function 
anotherFunction($page_id){
  
//Some code here

  
$this->load->view('group/anotherFunction');
 }




Or like this.

PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

class 
Groups extends CI_Controller {
 private 
$page_id ''//

 
public function view($slug false){
  
$this->load->model('some_random_model'); //Loading model
  
$info $this->some_random_model->get_info($slug); //Getting more info
  
  
$this->page_id $info['page_id']; //Setting a value in page_id variable
  
$this->anotherFunction();
 }

 private function 
anotherFunction(){
  
$page_id $this->page_id;

  
//Some code here

  
$this->load->view('group/anotherFunction');
 }



You don't need a hidden field. You can "inject" a php variable into Javascript code.

Example:
Code:
$('#some_button').click(function(){
  var url = "<?= site_url('groups/anotherfunction/' . $page_id);?>";
  $.post( url )
  .done(function(data) {
     $('#some_element').html(data);
  });
});

In the controller, load the view and put it in a variable:
PHP Code:
$dynamic_html $this->load->view('group/anotherFunction',NULL,TRUE);
echo 
$dynamic_html
The 3rd argument of $this->load->view() will return the value.
The echo in the controller function will return the result to the Ajax call.
Maybe loading the view as usual will work as well, but I didn't test that.