CodeIgniter Forums
Cant I declare global varirable in controller? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22)
+--- Thread: Cant I declare global varirable in controller? (/showthread.php?tid=7479)



Cant I declare global varirable in controller? - El Forum - 04-10-2008

[eluser]geshan[/eluser]
The following code shows error:

Code:
class Testlibc extends Controller{

    $sql_q="SELECT * from del_user"; //it gives error here
    
    function testlibc(){
        parent::Controller();
        $this->output->enable_profiler(TRUE);
        $this->load->library('testlib',$sql_q);
    }
    
    function index(){
        $this->grid_construct($sql_q);
                    
    }//function index close
}//class close

How can I declare global variable in a contoller?


Cant I declare global varirable in controller? - El Forum - 04-10-2008

[eluser]xwero[/eluser]
in php4 you need to put var before the variable, in php5 you can control the scope of the variable with public, protected or private. Or var for php4 compatibility.


Cant I declare global varirable in controller? - El Forum - 06-19-2008

[eluser]srajibtechno[/eluser]
hello CI experts..

I am using global variable in my menu_model.php script...
I already created two function and one constructor in my model..
But i can not fetch value from my global variable in my 2nd function
named-sub_menu_getall().i need the query variable's value...in my 2nd function...

how can i do this??


i am looking forward to hear from u...

my code is given below....

Code:
<?php

/** Model: Left Menu Model  
* @author Syed Rajib Rahman , Date : 17-06-2008
* @copyright 2008
*/

class  Menu_model extends Model
    {
     function Menu_model()
       {
        parent :: Model();
        
       }
      
       function menu_getall()
       {
          $this->load->database();
          $this->db->select('main_menu');
          $this->db->from('inbook_links');
          $this->db->group_by("main_menu");
          $GLOBALS['query'] = $this->db->get();
            
                  return $GLOBALS['query']->result();
        
       }  
      
      
      function sub_menu_getall()
       {    
    
          $this->load->database();
          
         $sub_menu_array = array();
        
         for ($i=0; $i<count($GLOBALS['query']); $i++)
            {
                for ($i=0; $i<count($GLOBALS['query']); $i++)
                {
                 foreach ($GLOBALS['query'][$i] as $value)
                 {
                     array_push($sub_menu_array,$value);
                    
                 }
                }
            }    
          
           for ($i=0;$i<count($sub_menu_array); $i++)
               {
              $this->db->select('sub_menu');
              $this->db->from('inbook_links');
              $this->db->where('main_menu',$sub_menu_array[$i]);
              
              $query2 = $this->db->get();
              return $query2->result();
            }
          
        
        
         }       //End sub_menu_getall()


    } // End class


?&gt;



Cant I declare global varirable in controller? - El Forum - 06-19-2008

[eluser]mglinski[/eluser]
Drop Global Variables for CI. COmpletly.
If you want a way to permiate variables throughout CI make a library that autoloads variables to a class variable. Then you access it like this:
Code:
$this->LIBRARY_NAME->config['VARIABLE_NAME'];
-Matt


Cant I declare global varirable in controller? - El Forum - 06-19-2008

[eluser]mglinski[/eluser]
Oh and to access class functions and variables in-class you need a $this-> in front of what you are trying to access. EG
Code:
class Testlibc extends Controller
{

    $sql_q="SELECT * from del_user"; //it gives error here
    
    function index()
    {
       echo $this->sql_q;
                    
    }
}