Keeping array contents static during repeated page loads

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jgoluch
Forum Newbie
Posts: 5
Joined: Tue Oct 21, 2003 9:38 am

Keeping array contents static during repeated page loads

Post 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
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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 ;)
jgoluch
Forum Newbie
Posts: 5
Joined: Tue Oct 21, 2003 9:38 am

Post 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
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post 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
jgoluch
Forum Newbie
Posts: 5
Joined: Tue Oct 21, 2003 9:38 am

Post 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 !
Post Reply