Click to See Complete Forum and Search --> : need help with global variables


moondance
09-10-2003, 09:42 AM
Here's what i'm trying to do:

One php file with 4 functions, each with a location of PHP_SELF. The functions that run depend on variables set, so at the top of the page there are if loops ie:
//from form
if ($_POST['search')]{
search();
}

//from clicking on link
if ($_GET['All']){
Find_All();
}

What i can't get my head around is if i run one function, i want i to set a variable, so that when the page reloads for a different function using PHP_SELF, it can see that that variable is set so will perform a slightly different task.

So for instance:

Function search()
{
if (isset($_POST['globalvar'])){
one kind of search
}else{
another kind of search
}
}

I'm sure this would invlove using global variables but its not working. I've set the global variable with all my other variables
$globalvar = "somevalue"

Then in the function i want this value to change i've got:

global $globalvar;
$globalvar = "True"

So when the page is reloaded, $globalvar should ouput true. If it does, well, see the example above. But its not happening.

arrgh i been trying this all day and me heeds throbbing.

pyro
09-10-2003, 09:53 AM
All that setting a variable to global does is makes that variable available from function to function. When the page is reloaded, you will lose all variable information. So, you'll want to set them in something that persists, such as a session, a cookie, or perhaps you will just want to append the value as a query string, and user $_GET['varindex']; to get it later...

moondance
09-10-2003, 10:26 AM
how would that work with sessions? i have already a session set up from the login. i've tried this:

function search()
{
$_SESSION['gvar'] = $gvar;
rest of code
}

then
function find_all()
{
if (isset($_SESSION['gvar'])
{
search type 1
}else{
search type 2
}
}

but thats not working for me.

pyro
09-10-2003, 10:32 AM
You can read up on sessions at http://us4.php.net/session, but basically, you need to be sure you start you session with session_start() (http://us4.php.net/manual/en/function.session-start.php)