Page 1 of 1

Keeping array contents static during repeated page loads

Posted: Tue Oct 21, 2003 9:38 am
by jgoluch
Hi,

I'm looking to find out if I can keep array contents "static" (i.e. retained) during successive loads of a page. The array would be created once and contents set depending on results of an SQL query. Various links on the page could be clicked and would cause the page to reload/refresh with some slightly different content (images). I can retain the few scalar variables I need by passing them with (appending to) the URL of the links mentioned above. Is it possible to pass an array this way also or is there another way to accomplish what I've described ?

Thanks,

John

Posted: Tue Oct 21, 2003 9:42 am
by markl999
You could serlialize() the array and pass it in the url (or via a post) but that can be quite ugly. Storing the array in a session might be better and there's never a bad time to start learning sessions ;)

Posted: Wed Oct 22, 2003 2:37 pm
by jgoluch
I'm looking into sessions. The examples I've seen so far all refer to using sessions for passing/maintaining data between different pages. Will they work to retain data within one page ? What are necessary and sufficient functions to use session_start(), session_register(), ??

John

Posted: Wed Oct 22, 2003 6:00 pm
by DuFF
If you havent already check out the PHP.net site on the functions session_start() and session_register().

Heres a discussion on another forum about adding an array to a session variable:
http://forums.devshed.com/archive/5/2001/11/1/25129

Posted: Thu Oct 23, 2003 12:48 pm
by jgoluch
Alrighty then ! Either I'm missing something, I'm a complete idiot or both. I tried various session examples from various posts all to no avail. Here is a simple example to get started & build on using both scalar and array var's in a session...

Code: Select all

<?php
session_start();
header("Cache-control: private");
?>

<html>
<head>
<title></title>
</head>
<body>

<?php
if($sessVar == 0)
&#123;
	session_register('sessVar');
	$sessVar = 1;
	$sArr = array(0,0,0,0);
	session_register('sArr');
&#125;
else 
&#123;
	$sessVar++;
	for($i=0; $i<sizeof($sArr); $i++) &#123;
		$sArr&#1111;$i] += $i+1;
	&#125;
&#125;

for($i=0; $i<sizeof($sArr); $i++) &#123;
	echo "sArr&#1111;".$i."]= ".$sArr&#1111;$i].", ";
&#125;
echo ("<p><br>");
echo "sessVar = ".$sessVar."<p><br>";
?>
<a href="sesstest.php">Test</a>
</body>
</html>
Hope this helps someone. I feel much better now that I can actually see session var's maintained thru page refreshes !