Jump to content

Session Array isn't keeping its values


Caislean

Recommended Posts

I've got a calendar database, and my PHP pulls the events for the day. For now, I'm only pulling three values: the events unique id, the events title, and the events start date. I place these in an array, but when I try to retrieve them later they don't show up.

 

The top of the script looks like this:

		//---- Start a unique session. ----
		session_start();

	//When set to 1, debug information is shown.
		(integer) $debugInfo = 1;

	//Set whether this is considered a new session or not.
		if (isset($_SESSION['newSession'])) {
			$_SESSION['newSession'] = 0;
		} else {
			$_SESSION['newSession'] = 1;
		}
		if ($debugInfo == 1) echo "<p>[DEBUG] newSession: " .$_SESSION['newSession'];

 

Then I grab the events from the calendar database, and then I store those elements in an array. Here is the code for storing the events:

 

		//---- Store the event information in an array that lasts the entire session, if this is considered a new session.
		if ($_SESSION['newSession'] == 1) {
			(integer)$arrayElement = 0;
			while($row = mysql_fetch_array($result)) {
				$_SESSION['eventID[ . $arrayElement]'] = $row['extid'];
				$_SESSION['eventTitle[ . $arrayElement]'] = $row['title'];
				$_SESSION['eventDate[ . $arrayElement]'] = $row['start_date'];

				if ($debugInfo == 1) {
					echo "<p><b>[DEBUG] Array Element:</b> " . $arrayElement;
					echo "<p>[DEBUG] ID: " . $_SESSION['eventID[ . $arrayElement]'];
					echo "<p>[DEBUG] ID: " . $_SESSION['eventID[ . $arrayElement]'];
					echo "<p>[DEBUG] Title: " . $_SESSION['eventTitle[ . $arrayElement]'];
					echo "<p>[DEBUG] Date: " . $_SESSION['eventDate[ . $arrayElement]'];
				}
				$arrayElement = $arrayElement + 1;
			}
		}
	//---- End of array assignments. ----

 

Here is the code for retrieving the events (and this is where the problem is). For some reason, the array doesn't keep its values.

		//---- Declare $_SESSION variables only if they have not yet been set. ----
		if (!isset($_SESSION['numberOfRows'])) (integer)$_SESSION['numberOfRows'] = mysql_num_rows($result);
		if (!isset($_SESSION['actualEvents'])) (integer)$_SESSION['actualEvents'] = $_SESSION['numberOfRows'] - 1;
		if (!isset($_SESSION['eventsPerPage'])) (integer)$_SESSION['eventsPerPage'] = 6;
		if (!isset($_SESSION['numberOfPages'])) (integer)$_SESSION['numberOfPages'] = round(($_SESSION['numberOfRows'] - 1) / $_SESSION['eventsPerPage']);
		if (!isset($_SESSION['currentEvent'])) (integer)$_SESSION['currentEvent'] = $_SESSION['eventsPerPage'];
		if (!isset($_SESSION['currentPage'])) (integer)$_SESSION['currentPage'] = 1;

		if ($debugInfo == 1) {
			echo "<p>[DEBUG] Number of rows in table: " . $_SESSION['numberOfRows'];
			echo "<p>[DEBUG] Actual number of events: " . $_SESSION['actualEvents'];
			echo "<p>[DEBUG] Events per page: " . $_SESSION['eventsPerPage'];
			echo "<p>[DEBUG] Number of pages: " . $_SESSION['numberOfPages'];
			echo "<p>[DEBUG] Current event: " . $_SESSION['currentEvent'];
			echo "<p>[DEBUG] Current page: " . $_SESSION['currentPage'];
		}
	//---- End of setting variables. ----
	//---- Show the events. ----
		for ($counter = ($_SESSION['eventsPerPage'] - $_SESSION['currentEvent']); $counter < $_SESSION['currentEvent']; $counter++) {
					echo "<p><b>[REAL] Array Element:</b> ". $counter;
					echo "<p>[REAL] ID: " . $_SESSION['eventID[ . $counter]'];
					echo "<p>[REAL] Title: " . $_SESSION['eventTitle[ . $counter]'];
					echo "<p>[REAL] Date: " . $_SESSION['eventDate[ . $counter]'];
		}	

 

What happens is that when the information is stored (ie - the second block of code), everything works fine. However, when the third block of code executes the arrays are empty.

 

I do know that it would probably be easier to create a class and simply have an array of that class, but I'd like to avoid that (for reasons I won't go in to). Also, if you're concerned about memory load on the server, it is guaranteed that no more than 5 machines are running this script at any given time.

 

So why does the array not get stored properly?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.