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>";
}

 

Link to comment
Share on other sites

<?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?

Link to comment
Share on other sites

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;

?>

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;
}

Link to comment
Share on other sites

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 ++;
}

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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>
}
?>

 

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.