By promes on
I created a custom module. I store an array in global variable (global $myvar) in function 1. When I go to function 2 this global variable is empty.
In both functions "global $myvar;" is defined just under the function definition.
After putting data the variable in function 1 this variable is empty when entering function 2.
I also tried to (mis-)use the $user global with adding data in $user->myvar.
Also here the data exists only in function 1 as long as I am still in this function. After going to function 2 the variable is empty.
It may be a PHP setting, but in other additional modules (published on this site) I am using, the construct is working.
Any help will be appreciated.
Comments
Posting the code in
Posting the code in question would be useful. How is function 2 called relative to function 1?
The code in function1 existed
The code in function1 existed allready (including the while-loop), I only added the globals to have no database access in function2.
The code itself is to large to post here. But essentially it is like:
function function1() {
global $user;
global $myvar;
print_r($myvar);
$var2 = array();
$content = db_query('SELECT * FROM {tablename}');
while ($zgl = db_fetch_object($content)) {
$var2[$zgl->zid]['number'] = $zgl->number;
$var2[$zgl->zid]['price'] = $zgl->price;
}
$myvar = $var2;
print_r($myvar);
}
function function2() {
global $user;
global $myvar;
print_r($myvar);
drupal_set_message('quantity='.count($myvar));
}
Function1 is called by a menu-item and displays and optionally alters the database content. Function2 is called from within a block on the left side of some pages and displays part of the content of the databasetable. Since the function2 code is called from within a block it is called many times.
After selecting the menuoption that calls function1 the array is printed by the second print_r(). After going to a page where the block is displayed I get the drupal message: "quantity=0".
After selecting the menuoption that calls function1 again I expect to see the output from the first print_r(), being Array= (), but this doesn't appear. The second print_r() really prints.
I hope somebody will find my error.
I presume the problem is in
I presume the problem is in the use of the forms api.
In my function I create a form and try to store the requesting URI from $_SERVER['REQUEST_URI'] if it has meaningfull data into $myvar.
The forms handling starts my function twice, where the second time the request URI doesn't have meaningfull data and is set to empty.