Welcome Guest, Not a member yet? Register   Sign In
How to set and access global variable through controller functions?
#4

(This post was last modified: 09-15-2019, 01:09 AM by jreklund.)

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');
 }



Reply


Messages In This Thread
RE: How to set and access global variable through controller functions? - by jreklund - 09-15-2019, 01:00 AM



Theme © iAndrew 2016 - Forum software by © MyBB