0

im trying to pass a session between 2 div on the same page. i have links, when i click on one i want the id to pass to the other div to populate it with the info from the link, but it doesnt pass!!

this is the code from my links div

$q =mysqli_query($link, "SELECT * FROM products WHERE status = 1 ORDER BY id DESC");
while($row = mysqli_fetch_array($q)){
$data = $row['image'];
$file = substr($data, strpos($data, "/") + 1);
$_SESSION['id']=$row['id'];

echo"<div class='homedogs'>",
"<a href='merchandise.php' class='productchoice'>",
"<img class='nailthumb-container3' src='$file' alt='{$row['name']}. Image' />",
"</a>",
"<br />",
'NAME: ',$row['name'],"<br />",'PRICE: ',$row['price'],
"</div>";
}
}

and this is other div that i want to use the session in

include 'inc/connect.php';
$q = mysqli_query($link, "SELECT *  FROM products WHERE id = '".$_SESSION['id']."'") or  
                 die (mysql_error());
while($row = mysqli_fetch_array($q)){
$data = $row['image'];
$file = substr($data, strpos($data, "/") + 1);
echo"<div class='rehomediv'>",
"<img class='nailthumb-container2' src='$file' alt='{$row['name']}. Image' />","<br 
  />",
"<div class='nameagesex'>",
"<div class='item_name'>{$row['name']}</div>",
"<br />",
"<span class='item_price'>{$row['price']}</span>",
"</div>",
"<div class='description'>",
 nl2br($row['description']),
 "</div>",
</div>;

im sure its simple enought but i cant get it! can anyone help? thanks

EDIT!!!!!

on page load, pid is not set so i get an error, is there anyway to have it that if pid is not set then it just display the last record?

for anyone confused by this edit, check the accepted answer..

4

1 回答 1

1

您在循环$_SESSION['id']内多次分配。while这将最终只保留最后一个值,前提是您这样做session_start(),正如您声称的那样。

如果您希望它起作用,请不要使用$_SESSION,而是使用GET查询。将您的链接生成代码更改为:

"<a href='merchandise.php?pid={$row['id']}' class='productchoice'>"

...并且,merchandise.php检查$_GET['pid']以确定所请求的产品 ID:

if(isset($_GET['pid']))
  // show corresponding product (your second listing)
else
  // show something else, i.e. the product catalog (your first listing)
于 2013-10-27T12:08:45.810 回答