Storing and Retrieving data in/from $ _ SESSION variable in while loop

Hi,

I have two pages. Firstpage.php retrieves a data from database, and displays them in a table format, and stores them in the $_SESSION. both are acheived using while() loop.

In the secondpage.php , I try to retrieve data from $_SESSION variable and display them using foreach loop.

But as of now the $_SESSION variables on firstpage.php is just able to save one record from the database. As it only displays one record in secondpage.php

Below is the code mentioned for both the pages.

Firstpage.php


<?php session_start();
	$filelistSQL="select * from files";
	$exelistarray=mysql_query($filelistSQL) or die (mysql_error);

	while ($filelistarray=mysql_fetch_array($exelistarray))
	{
		//adding the new value of session['filename'] to the previously addedd value.
		$_SESSION['filename'] += $_SESSION['filename'] = array('id' => $filelistarray['id'],
		'merchantid' => $filelistarray['merchantid'],
		'filelocation' => $filelistarray['flocation']);
										
		echo "<tr>";
		echo "<td>".$filelistarray['id']."</td>";
		echo "<td>".$filelistarray['merchantid']."</td>";
		echo "<td>".$filelistarray['flocation']."</td>";
		echo "</tr>";
								
	}
?>

Secondpage.php

<?php
session_start();

	foreach ($_SESSION['filename'] as $i => $filevalue)
	{
		echo $_SESSION['filename'][$i];
	}
?>

Please awaiting help.

Not sure what you are trying to achieve…

I think this bit:


$_SESSION['filename'] += $_SESSION['filename'] = array('id' => $filelistarray['id'], 'merchantid' => $filelistarray['merchantid'],'filelocation' => $filelistarray['flocation']);

doesn’t make much sense, shouldn’t it be:


$_SESSION['filename'] = 
array('id' => $filelistarray['id'],
        'merchantid' => $filelistarray['merchantid'],
        'filelocation' => $filelistarray['flocation']);

In the below code, I am storing a value in $_SESSION variable that is fetched from mysql database.

When the while loop reads each records, I am saving that record in $_SESSION variable. If 2 record are fetched from mysql, then while loop runs 2 times, hence the $_SESSION variable will each time store the new value that is currently read by while loop.

But when storing the 2nd value in $_SESSION, i want to make sure that previous 1st value stored in $_SESSION is not erased. So I append the new value to previous value by using += in the code below.

		$_SESSION['filename'] += $_SESSION['filename']

[But this techniques doesn’t seem to work, any suggestions as how can this be achieved.]

[QUOTE=abhi_madhani;4558630]But when storing the 2nd value in $_SESSION, i want to make sure that previous 1st value stored in $_SESSION is not erased. So I append the new value to previous value by using += in the code below.

From your code I see that you want to store an array in the session.

You will see on the array operators page that they work in a way that is different to what you expect.

If you want to store multiple arrays, they need to be stored inside a container array.

if (empty($_SESSION['filename'])) {
    $_SESSION['filename'] = array();
}

To add an array in to the container array, you use the syntax

$_SESSION['filename'][] = array(
   '...' => '...',
   '...' => '...'
);

See modifying arrays