0

Following is my code which retrives cookie value and shows recently viewed products

//get cookie vlue
$myCookieArr = array_unique(json_decode($_COOKIE['myshopping']));
//reverce cookie order
$myCookieArry = array_reverse($myCookieArr);
//loop through
foreach ($myCookieArry as $key => $value) {
    $allCProduct .= $value.",";
}
$allCProduct = rtrim($allCProduct,',');

$getRandProduct = $conn->query("SELECT * FROM product WHERE id IN (".$allCProduct.")" );

It Works fine but issue is it is not in order of array_reverse($myCookieArr). so how can i make order by my array value.

So what happen is first viewed product will be last if second one product is viewed by user.let suppose i am getting array as

(
 [0] => 35
 [0] => 39
 //This is Two product 
 //i have already seen
)

Now if i show third product 40 so order should like

(
 [0] => 40
 [1] => 35
 [2] => 39
)

NOTE : i don't have issue of security

So how can i achieve that ?

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345

0 Answers0