Welcome Guest, Not a member yet? Register   Sign In
Able to access global variable, but ...
#1

[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 ??
#2

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

[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";

?>
#4

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

[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 ?
#6

[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';
?>
#7

[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
#8

[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;

?>




Theme © iAndrew 2016 - Forum software by © MyBB