CodeIgniter Forums
Able to access global variable, but ... - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Able to access global variable, but ... (/showthread.php?tid=3805)



Able to access global variable, but ... - El Forum - 10-22-2007

[eluser]cinewbie81[/eluser]
Hi all,

I have a helper file named : "setting_helper.php" as following:

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

define("__VARIABLE1__","variable1");
$var2 = "variable2";
?>

The following is my controller class:

Code:
<?php
class Main extends Controller {

    function Main()
    {    
        parent::Controller();
          $this->load->helper(array('url', 'setting'));
    }

    function Runsetup()
    {
             gloabl $var2;
             echo __VARIABLE1__;
             echo $var2;
        }
}
?>

How come when i call Runsetup function, echo __VARIABLE1__ will print me "variable1", but echo $var2 will give me nothing ?? By right it should return me the value "variable2" as i already decraed it as global variable .. anyone ??


Able to access global variable, but ... - El Forum - 10-22-2007

[eluser]crikey[/eluser]
Look at your global variable declaration in Runsetup - gloabl should be spelt 'global'.


Able to access global variable, but ... - El Forum - 10-22-2007

[eluser]Jatinder Thind[/eluser]
Even after fixing the "gloabl" typo, it won't work. To know why, you have to first understand how a helper is loaded. When you call

Code:
$this->load->helper(array('url', 'setting'));

The helper() function simply includes the setting_helper.php file. Therefore $var2 will be available only within the local context of helper() function and won't be visible outside this function.

Change your helper as below at it will work:

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

define("__VARIABLE1__","variable1");
$GLOBALS['var2'] = "variable2";

?>



Able to access global variable, but ... - El Forum - 10-22-2007

[eluser]cinewbie81[/eluser]
thx a lot Jatinder Thind .. it works now Smile


Able to access global variable, but ... - El Forum - 10-23-2007

[eluser]cinewbie81[/eluser]
Problem solved .. but one more question, how am i goin to do it if the variable is an array ? I try:

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

$GLOBALS['myArray'] = array();
$GLOBALS['myArray[dell]'] = 'Create Database Dell';
$GLOBALS['myArray[microsoft]'] = 'Create Database Microsoft';
$GLOBALS['myArray[Apple]'] = 'Create Database Apple';
?>

and my controller class:
Code:
<?php
class Main extends Controller {

    function Main()
    {    
        parent::Controller();
          $this->load->helper(array('url', 'setting'));
    }

    function Runsetup()
    {
       global $myArray;
    echo $myArray['apple'];
    }
}
?>

When i call Runsetup function, it generate me the error : "Message: Undefined index: apple" .. any idea ?


Able to access global variable, but ... - El Forum - 10-23-2007

[eluser]xwero[/eluser]
[quote author="cinewbie81" date="1193137997"]
Code:
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

$GLOBALS['myArray'] = array();
$GLOBALS['myArray[dell]'] = 'Create Database Dell';
$GLOBALS['myArray[microsoft]'] = 'Create Database Microsoft';
$GLOBALS['myArray[Apple]'] = 'Create Database Apple';
?>

[/quote]
You have set up strings instead of an array
Code:
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

$GLOBALS['myArray'] = array();
$GLOBALS['myArray']['dell'] = 'Create Database Dell';
$GLOBALS['myArray']['microsoft'] = 'Create Database Microsoft';
$GLOBALS['myArray']['Apple'] = 'Create Database Apple';
?>



Able to access global variable, but ... - El Forum - 10-23-2007

[eluser]cinewbie81[/eluser]
[quote author="xwero" date="1193138753"][quote author="cinewbie81" date="1193137997"]
Code:
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

$GLOBALS['myArray'] = array();
$GLOBALS['myArray[dell]'] = 'Create Database Dell';
$GLOBALS['myArray[microsoft]'] = 'Create Database Microsoft';
$GLOBALS['myArray[Apple]'] = 'Create Database Apple';
?>

[/quote]
You have set up strings instead of an array
Code:
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

$GLOBALS['myArray'] = array();
$GLOBALS['myArray']['dell'] = 'Create Database Dell';
$GLOBALS['myArray']['microsoft'] = 'Create Database Microsoft';
$GLOBALS['myArray']['Apple'] = 'Create Database Apple';
?>
[/quote]

not working still even though i use your code Sad


Able to access global variable, but ... - El Forum - 10-23-2007

[eluser]Jatinder Thind[/eluser]
This will work:

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

$myArray = array();
$myArray['dell'] = 'Create Database Dell';
$myArray['microsoft'] = 'Create Database Microsoft';
$myArray['apple'] = 'Create Database Apple';
$GLOBALS['myArray'] = $myArray;

?>