Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

global stack not working

Status
Not open for further replies.

rbauerle

Programmer
Oct 16, 2001
48
BR
I'm trying to use a global array and i have to manipulate it inside a function, but every time i start the function again it is empty. I'm using array_push to add elements (strings) to the array.
The array is declared outside the function and inside the function I have 'global $array;' in the begining.

Any suggestions?

Thanks a lot.
 
Actualy, that would be hard. Its a lot of stuff! But I can try to explain it better.
I have 2 include files: config.php and lib.php and they're included in every file I have in my system.
My global array is declared in config.php and my function is running in lib.php. When ever the function is called, the array returns as if it was empty when the function was called.
The function I'm using should do some tests and in certain condition push an element to my array (with array_push()).

Thanks again.
rbauerle
 
Without code it is very hard to debug.
This works for me.
config.php
Code:
<?php
$gArray = array(&quot;test&quot;, &quot;test2&quot;);
?>
lib.php
Code:
<?php
function manipArray($elementno, $elementval)
{
    global $gArray;
    $gArray[$elementno] = $elementval;
}
?>
page.php
Code:
<?php
include('./config.php'); // Assuming the included files are in the same directory
include('./lib.php');
manipArray(2, &quot;Test3&quot;);
echo $gArray[1];
?>
//Daniel
 
Typo in my previous post.
Code:
    $gArray[$elementno] = $elementval;
should read
Code:
    $gArray[$elementno - 1] = $elementval;
//Daniel
 
Well, since nothing's working I decided to copy the relevant parts of the aplication and see if you can try to help me again.

Thanks, rbauerle

config.php
<?
$url_stack = array();
// there are some defines here that are irrelevant to the problem
?>

lib.php
<?
function msSetBackLink($url)
{
global $url_stack;

$stack_size = count($url_stack);

if ($stack_size == 0){
array_push($url_stack,$url);
$stack_size++;
}

if ($url_stack[$stack_size-1] != $url){
array_push($url_stack,$url);
$stack_size++;
}

$link_pos = $stack_size-2;

if ($link_pos >= 0){
return &quot;<br><a href='&quot;.$url_stack[$link_pos].&quot;'><< Voltar</a><br>&quot;;
}else
return &quot;&quot;;
}
?>

page.php
<?
include_once &quot;config.php&quot;;
include_once &quot;lib.php&quot;;

if ($HTTP_SERVER_VARS[&quot;argv&quot;])
$url = msGetCurrentPage($HTTP_SERVER_VARS[&quot;SCRIPT_NAME&quot;],$HTTP_SERVER_VARS[&quot;argv&quot;]);
else
$url = msGetCurrentPage($HTTP_SERVER_VARS[&quot;SCRIPT_NAME&quot;],&quot;&quot;);

echo &quot;esta é a url 'pegada' da página atual: &quot;.$url;

echo msSetBackLink($url);

?>
 
my opinion about this problem is simple ... avoid by maximum using global vars, cause the value deppends on the order of processing.

Another thing, what's the order of the files includes?

you have one thing. you are resetting the array using the array() function. So, when the value after tis will be an empty array.

Do you expect to keep the array between pages? if so you need to keep it in a session var. Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top