Jump to content

Displaying records from $_SESSION array


abhi_madhani

Recommended Posts

Hi, friends

 

I am trying to store a value in a session array, and calling it on second page, but instead of displaying all the records, its just displaying the 2nd record out of many on the next page.

 

Can you guys please help me as to why this is not happening.

 

First page - it retrieves the result from database and displays in a table.

 

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

while ($filelistarray=mysql_fetch_array($exelistarray))
{
	$_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>";

}

 

 

Second page grabs the value from the $_SESSION and displays them in a page. Its just displays the 2nd record not 1st and other remaining records

 

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

 

Regards,

Abhishek

Link to comment
Share on other sites

Each time you try to add a new array to your session you actually overwrite the current one.

 

Consider the following:

 

<?php

// Assign an array to filename session variable
// No matter how many times you do this filename will only contain 1 array
$_SESSION['filename'] = array();

// Assign an empty array to filename session variable
$_SESSION['filename'] = array();

// And then assign values to that array...
$_SESSION['filename'][] = array('First Array');
$_SESSION['filename'][] = array('Second Array');
$_SESSION['filename'][] = array('Third Array');
$_SESSION['filename'][] = array('Fourth Array');
?>

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.