Welcome Guest, Not a member yet? Register   Sign In
Help, How to access the variable did not define in function?
#1

[eluser]benyu[/eluser]
e.g
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
$the_array = array();
$the_array[] = "some value ...";
//more..

// save as test_helper.php in application/helper directory.
function test()
{
global $the_array;

//always getting the " Invalid argument supplied for foreach()" error
foreach($the_array as $key=>$value)
{//....}
}

//because here used the array as well!
function another_function(){}
?>
#2

[eluser]gtech[/eluser]
Hello,

what are you trying to do here?

set up a global variable in a helper and use it in a controller?

or

set up a global variable in a helper and use it within the same helper?
#3

[eluser]benyu[/eluser]
Yes!
I want to set up a global variable in a helper file and use it within the same helper!
e.g.
has two functions needs the variable.
more details as above sample codes!

thanks your response!
#4

[eluser]gtech[/eluser]
This worked for me:

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

$GLOBALS['whatever'] = 'hello';

if (! function_exists('test_test1'))
{
    function test_test1()
    {
           global $whatever;
           return $whatever;
    }
}

if (! function_exists('send_email'))
{
    function test_test2()
    {
           global $whatever;
           return $whatever;
    }
}

?>

Controller:
Code:
<?php
class Test extends Controller {

  function Test()
  {
    parent::Controller();
  }
  function sproc()
  {
    $this->load->helper('testgbl');
    echo test_test1();
    echo test_test2();
  }
}
?>

sproc returns

"hellohello"

hope it helps, looks like you can use it in the controller functions as well so be careful Smile.

adapted from this link [url="http://ellislab.com/forums/viewthread/63343/"]http://ellislab.com/forums/viewthread/63343/[/url]




Theme © iAndrew 2016 - Forum software by © MyBB