Jump to content

store while statment data in array using session and retrieve?


sayedsohail

Recommended Posts

Hi all,

 

I wish to store retreve value from my sql using session array and retrieve this value.

 

here is my while statement, which is displaying records from my database. I want to store all this value in an array using session and than retrieve this value on click of the table row.  Please help.

 

while(list($id, $name) = mysql_fetch_array($result)) 
{print "<tr><td>$id></td><td>$name></td></tr>";
}

 

this is my blank array, but how do i pass the value into this array.  please help.

 

session_start();

 

// create an blank array,

$my_array=array();

 

// put the array in a session variable

$_SESSION['values']=$my_array;

 

 

<?php

session_start();

// create an blank array,
$my_array=array();

while(list($id, $name) = mysql_fetch_array($result)) 
{  $my_array[] = "<tr><td>$id></td><td>$name></td></tr>";
}
// put the array in a session variable
$_SESSION['values']=$my_array;
?>

 

Is that what you mean?

Thanks, this is exactly what i needed, plus i wish to retain the values of the tr(row)on onclick method, so i can refer this value on page refresh, any help would be greatly appreciated.<?php

 

session_start();

 

// create an blank array,

$my_array=array();

 

while(list($id, $name) = mysql_fetch_array($result))

{  "<tr onclick=$my_array[] ><td>$id></td><td>$name></td></tr>";

}

// put the array in a session variable

$_SESSION['values']=$my_array;

?>

 

Wow dude you just obliterated that.

 

<?php

session_start();

// create an blank array,
$my_array=array();

while(list($id, $name) = mysql_fetch_array($result))
{  $my_array[] = "<tr onclick='javacodehere'><td>$id></td><td>$name></td></tr>";
}
// put the array in a session variable
$_SESSION['values']=$my_array;
?>

 

Also the [ code ] and [ /code ] tags are your friends, not your enemies.

 

Remember to define a variable = to a value it goes $variable = $value; you cannot set a variable to a value by doing it how you were trying to above.

Hi,

 

I changed the values, but how do i refer the row click values from the session values.

 

Here is the modified code:

 

<?php

session_start();

// create an blank array,
$my_array=array();

while(list($id, $name) = mysql_fetch_array($result))
{  
$my_array[] = "<tr onclick='passvar($id, &name,this)'><td>$id></td><td>$name></td></tr>";
}
// put the array in a session variable
$_SESSION['values']=$my_array;
?>


function passvar(pan,rec,obj)
{

var bage = pan;
var cid = rec;
document.location.href="sitedetail.php?page="+bage+"&cid="+cid;
}

i had also tried this method, but its not retaining the values of the row i clicked, instead its retained the value of the last row inside the table.

 

$count = 1;
while ($row= mysql_fetch_array($result)) {
  $name = $row["NAME"];
  $ID = $row["ID"];
  $_SESSION['s_name'] = $name;
  $_SESSION['s_ID'] = $ID;
          echo "$name";
          echo "$ID";
          $count ++;
}

 

foreach ($_SESSION['values'] as $array) {
  $name = $array["NAME"];
  $ID = $array["ID"];
  $_SESSION['s_name'] = $name;
  $_SESSION['s_ID'] = $ID;
          echo "$name";
          echo "$intersection";
}

thanks, but its making me crazy.

 

this is my while statement

 

count = 1;
while(list($id, $name) = mysql_fetch_array($result))
{  
$my_array[] = "<tr onclick='passvar($id, &name,this)'><td>$id></td><td>$name></td></tr>";
}

// put the array in a session variable
$_SESSION['values']=$my_array;
count++;
?>

my javascript funciton retains all the values and i can refer all the values in my array, but how do i refer the value of the clicked row from the array.  please help

 

the next

 

Redarrow, thanks it works, but only one problem, since the i got loads of records, the $_SESSION['name']=$name; retains only the the last row $name and $id.  I would like to refer the click row values to next page.  Any possibility of doing so.. please help.

 

<?php session_start();

while(list($id, $name) = mysql_fetch_array($result)) {

$_SESSION['name']=$name;
$_SESSION['id']=$id;	
echo "<TR onclick="<a href='whatever.php?id=".$_SESSION['id'].">go to new page</a>>"
<TD>$name</td><TD>$id</td></tr>
}
?>

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.