Forum Moderators: coopster

Message Too Old, No Replies

Need to pass an array in php sessions

         

mani2604

8:36 am on Aug 31, 2010 (gmt 0)



Well we do have an array of latitudes & longitudes in the mysql database which we do fetch to perform a query..now we do have sessions where the client gives these lat,longs of his choice and i wanna fetch them in a different php file... Since I am relatively new to this; wanna know what to replace the below line with so that the array which we usually fetch from the database can be replaced wid the ones that are stored in the session variables.. i am sory if I am not clear enough.. all i want is to retrieve those rows of arrays in a session
while ( $row = mysql_fetch_array($result,MYSQL_NUM) )


mysql fetch
$query = ("SELECT * FROM table WHERE (LONG_HI<'".$_POST['Ymax']."')");
$result = mysql_query($query);

// save each row of result in an array

$i = 0;
while ( $row = mysql_fetch_array($result,MYSQL_NUM) ) {
$qresult[$i] = $row;
$i++;
}

// return array of results

return $qresult;


Array Session file


<?php
session_start();

$_SESSION['table'] = array(
// array('id','state','lat_hi','long_hi','lat_low','long_low'),
array(1,'andaman',13.59,94.22,06.73,92.53),
array(2,'AP' , 19.87,84.78,12.59,76.45),
array(3,'Gujarat',24.72,74.55,20.15,68.35),
array(4,'kerala' ,12.75,77.37,08.18,74.86),
array(5,'punjab' ,32.46,76.93,29.53,73.87),
);
var_dump($_SESSION['table']);
?>

rocknbil

6:14 pm on Aug 31, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome aboard mani2604, try this.

$qresult[$i] = (isset($_SESSION['tables'][$i]))?$_SESSION['tables'][$i]:$row[$i];

Alternatively you can skip the query altogether if they exist.


$arraylen=4; // zero based
i=0;
$allValuesPresent=1;
if (isset($_SESSION['tables'])) {
foreach ($_SESSION['tables'] as $arr) {
if (! isset($arr[$i])) {
$allValuesPresent = 0;
break;
}
// Will get overwritten by query if any are missing
$qresult[$i]=$arr[$i];
$i++;
}
//
if ($allValuesPresent==0) {
// do your query
}

eelixduppy

11:11 pm on Aug 31, 2010 (gmt 0)



Sometimes, I also like to serialize the array (or object) before storing it in a session variable: [us.php.net...]

e.g.,


$values = array(
array(1,'andaman',13.59,94.22,06.73,92.53),
array(2,'AP' , 19.87,84.78,12.59,76.45),
array(3,'Gujarat',24.72,74.55,20.15,68.35),
array(4,'kerala' ,12.75,77.37,08.18,74.86),
array(5,'punjab' ,32.46,76.93,29.53,73.87),
);
#
$_SESSION['table'] = serialize($values);

mani2604

7:33 am on Sep 1, 2010 (gmt 0)



@rocknbill:-


Really thanks a lot... It worked:- )