1

I am making a website in which i have to save a global variable.

I am using this person code globals_helper.php custom global variable class

But i always get static variable value null.

globals_helper.php:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

 // Application specific global variables
class Globals
{
 private static $authenticatedMemberId = null;
 private static $initialized = false;

 private static function initialize()
 {
     if (self::$initialized)
         return;

     self::$authenticatedMemberId = null;
     self::$initialized = true;
 }

 public static function setAuthenticatedMemeberId($memberId)
 {
     self::initialize();
     self::$authenticatedMemberId = $memberId;
 }


 public static function authenticatedMemeberId()
 {
    self::initialize();
     return self::$authenticatedMemberId;
 }
}

I have done all the steps like add globals_helper.php in helper folders and updated autoload file. Now I am trying to access those static variable from a custom library "Ctrl_utility" function "get_search_term" and my controllers calling get_search_term function

Ctrl_utility.php

class Ctrl_utility {
 protected $CI;
public static $static_search = "";


public function __construct()
{
    // Assign the CodeIgniter super-object
    $this->CI =& get_instance();

}

public function get_search_term($searchTerm){

    $searchTerm = $this->CI->security->xss_clean(htmlspecialchars($searchTerm));

    if (isset($searchTerm) && strlen($searchTerm)>0) {
        Globals::setAuthenticatedMemeberId($searchTerm);  
    } else {
       $searchTerm = Globals::authenticatedMemeberId();
    }
    return $searchTerm;
}

One of my controller and they all have class ctrl_utility, get_search_term function:

class Blog_controller extends CI_Controller{

public function __construct() {

    parent::__construct();

    $this->load->model('blogs_model');
}

public function index(){


    //Get SearchTerm Values
    $searchTerm = $this->ctrl_utility->get_search_term($this->input->post('searchTerm'));

    //Get Url First Parameter
    $start = $this->ctrl_utility->get_url_first_parameter();

    // Get Data from solr 
    $rows = 10;
    $data = $this->blogs_model->solrData($start, $rows, $searchTerm); //give start of documents

    //Pagination
    $this->pagination->initialize($this->ctrl_utility->pagination_config($this->uri->segment(1), $rows, $data['found']));

    //Views
    $this->load->view('tabs/blogs', $data);


}
}

Am i doing something wrong?

Community
  • 1
  • 1
MTA
  • 1,033
  • 5
  • 16
  • 37

1 Answers1

0

Now when it comes to define them in CodeIgniter, there are several ways do that. I’ve listed some of them below:

  1. Create your own file in application/libraries in which class constructor contains an array as an argument. Now create a new file in /application/config with same name as given in application/libraries and declare your global variables in it. Now to use these variables, autoload the newly created library.

  2. Create your own file in application/core and declare the global variables in it. Than in controller you need to extend your file name instead of CI_Controller.

  3. If the Global Variables are true constants, just add them in application/config/constants.php file and name them in all uppercase like the others are defined.

  4. If you want to set application constants create new config file and add the variables. Now load it as $this->config->load(‘filename’); And access those variables as

    $this->config->item(‘variable_name’);

Instead of creating helper create a library

Step 1: First of all, open application/libraries and create a custom class name globals.php. It contains a constructor function which contains an array as an argument.

<?php

class Globals {

//  Pass array as an argument to constructor function
public function __construct($config = array()) {

//  Create associative array from the passed array
foreach ($config as $key => $value) {
$data[$key] = $value;
}

// Make instance of CodeIgniter to use its resources
$CI = & get_instance();

// Load data into CodeIgniter
$CI->load->vars($data);
}

}

?>

Step 2: Now to make config file, open application/config and create file as globals.php and write the code given below. This file contains the config variable which will be passed as an array to constructor of Globals class where its keys and values are stored in $data

<?php

// Create customized config variables
$config['web_Address']= 'https://www.example.com/blog';
$config['title']= 'CodeIgniter Global Variable';

?>

When constructor function loads, it will take the config variables from the config file in order to use these variables anywhere.

Note: Name of the above file must be same as the class created in libraries folder otherwise the code will not work.

Step 3: But before using these variables we have to autoload the newly created library globals as given below.

And load library in autoload

$autoload['libraries'] = array('globals');

Now, you can use global variables in your controller

<?php
class CI_Global_Variable_Tutorial extends CI_Controller{
public function __construct() {
parent::__construct();
}
// Load view page
public function index() {
$this->load->view('show_global_variables');
}
}
?>

Views : show_global_variables.php

In view page, we can use global variables according to our need.

<?php
echo "Title of the blog post : ".$title;
echo "<a href='$web_Address'>"."Click here to go to blog page"."</a>";
?>
Siddhartha esunuri
  • 1,104
  • 1
  • 17
  • 29
  • load->vars($globalv) is used for storing global variable? is it like a session but not storing in server side? Can i override $globalv value like in sessions we do on the basis of key/value pair? – MTA Jul 20 '16 at 07:41
  • Follow all steps as I explained above it's work @MTA – Siddhartha esunuri Jul 20 '16 at 07:56
  • Sorry to ask you this many questions, i am new in this and eager to learn. How can i override these variables again and again from code like in controller or model or these variables are constant? – MTA Jul 20 '16 at 08:11
  • go through the user guide http://www.codeigniter.com/user_guide/ it will help you all corners – Siddhartha esunuri Jul 20 '16 at 09:02