save multiple pages in favorite page based on cookies in php

时间:2016-07-11 19:47:09

标签: php cookies

hi I am designing a real estate website i have many ads in my website and when user click on a certain ad it goes to another page viewmore.php which gives user more details about that certain ad.
Now as you see in viewmore.php file I save ad's id in cookies and send ad's id to the favorite page and user can review that post any time he or she wants in favorite page.
now the problem
consider a user visit two post
viewmore.php?id=10
and
viewmore.php?id=7
in this case he or she wants to see both ads in favorite page not, only the last one. in my code it just shows the last add with id=7 how can i change my codeso that previous add does not disappear. any idea? thank u
viewmore.php

<?php
$ID = is_numeric($_GET['ID']) ? $_GET['ID'] : 1;

$cookie_name = "favoritepost";

if ( isset($_COOKIE[$cookie_name]) ) {
    $kookie = unserialize($_COOKIE[$cookie_name]);
} else {
    $kookie = array();
}
if ( ! in_array($ID, $kookie) ) {
    $kookie[] = $ID;
}

setcookie($cookie_name, serialize($kookie), time() + (86400 * 30), "/"); // 86400 = 1 day
?>
<html>

<!doctype html>
<html>
<body>
<?php
$cookie_name = "favoritepost";
include("config.php");
if ( ! isset($_COOKIE[$cookie_name]) ) {
    echo 'NO COOKIE SET';
    exit;
}

$kookie = unserialize($_COOKIE[$cookie_name]);
$ID = $kookie[count($kookie)-1];
$result = mysqli_query($connect,"SELECT * FROM $db_table WHERE idhome = $ID");

$row = mysqli_fetch_array($result):
$price=$row['price'];
$rent=$row['rent'];
$room=$row['room'];
$date=$row['date'];

echo"price";
echo"room";
echo"date";
?>

0 个答案:

没有答案