Malekian
Malekian

Reputation: 315

save cookies with same name but different values in php

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.

The problem:

consider that i visit this page localhost/viewmore.php?ID=10 thus when you go to favorite page u see the ad data which belong to this id but when you visit another ad like localhost/viewmore.php?ID=11 and you go to the favorite page you see the ad data which belong to id=11 and previous add is gone. i want to save both of them in my favorite page or as a matter of fact save all the posts i visit.

how can i do that?

//reviewmore.php
<!doctype html>
<?php
(is_numeric($_GET['ID'])) ? $ID = $_GET['ID'] : $ID = 1;
?>
<?php 
$cookie_name = "favoritepost";
$cookie_value ="$ID";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
?>
<html>
  <body>
    <?php
 error_reporting(0);
include("config.php");
(is_numeric($_GET['ID'])) ? $ID = $_GET['ID'] : $ID = 1;
$result = mysqli_query($connect,"SELECT*FROM ".$db_table." WHERE idhome = $ID");
?>
<?php
 error_reporting(0);
include("config.php");
(is_numeric($_GET['ID'])) ? $ID = $_GET['ID'] : $ID = 1;
$result = mysqli_query($connect,"SELECT*FROM ".$db_table." WHERE idhome = $ID");
?>
<?php $row = mysqli_fetch_array($result):
$price=$row['price'];
$rent=$row['rent'];
$room=$row['room'];
$date=$row['date'];
?>
<?php 
echo"price";
echo"room";
echo"date";
?>
</body>
  </html>

favoritepage.php

<!doctype html>
<html>
  <body>
 <?php 
$cookie_name = "favoritepost";
?>
<?php
 error_reporting(0);
include("config.php");
$result = mysqli_query($connect,"SELECT*FROM ".$db_table." WHERE idhome = $_COOKIE[$cookie_name]");
?>
<?php $row = mysqli_fetch_array($result):
$price=$row['price'];
$rent=$row['rent'];
$room=$row['room'];
$date=$row['date'];
?>
<?php 
echo"price";
echo"room";
echo"date";
?>
    </body>
  </html>

Upvotes: 1

Views: 603

Answers (1)

RiggsFolly
RiggsFolly

Reputation: 94642

Store the ID's as an array in your cookie like this for example, you have to serialize()/unserialize() or json_encode()/json_decode() the array into the cookie as it only supports text and not complex data

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

Now the last cookie entered I assume will be the one that you want to use in your query so just get

<!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";
?>

Upvotes: 1

Related Questions