Reputation: 871
Edit: I solved it somehow with PHP. Its so specific but I want to share it in case somebody needs it. First of all I didnt give any specific name as product_id for the cookie but I just give the variable of the product_id "$ftc2" which is clicked, shown as below
onclick=\"SetCookie('".$ftc2."', '".$ftc2."', 'expiry');
So whenever user clicks on any product, a new cookie is created with the name of the product_id like name=2 or name=32 whatever.
then I have created a foreach on the chart page as below
<?php foreach($_COOKIE as $cookies) { echo $cookies; } ?>
It displayes now all stored cookie values(in my case product ids) and now I can manipulate it as I want and display all products which is added to chart Maybe it is so easy and so common but somehow I couldnt find it out since morning = )) I hope it saves someone's hours
THE QUESTION WAS:
I am building an onlineshop and What I want to do is displaying all clicked products in showChart page.(a very simple page which shows you the products that you added to your chart by clicking on chart_icon). I have tried some several common methods but I couldnt manage to do it somehow. My last desperate try was creating this addToChart() function.
Since setting the cookie part works good I simplified the code and paste just the important parts (in my opinion). Any kind of help or suggestions would be appreciated. I dont expect anyone to write the code for me.
thanks in advance
for($i=0;$i<$num;$i++)
{
$ftc = mysql_result($result, $i, "product_image");
$ftc2 = mysql_result($result, $i, "product_id");
$ftc3 = mysql_result($result, $i, "product_name");
$ftc4 = mysql_result($result, $i, "product_preis");
$ftc5 = mysql_result($result, $i, "product_old_preis");
echo "<a href='details.php?product=".$ftc2."'><div class='products_list'>
<img width='217px' height='323px' src='".$ftc."'/>
<div class='alt_yazi2'><b>".$ftc3 ."   <font
style='float:right;'>".$ftc4."TL  <b style='text-decoration:line-through; float:right;'>".$ftc5."TL </b></font></b></a>
</br>
<div id='sepet'><a id='cookie' onclick=\"SetCookie('"cookie_product"', '".$ftc2."', 'expiry');\" href='#'>
<img width='35px' height='25px' src='images/cart_icon.png'/>
JAVASCRIPT CODE BEGINS HERE
var today = new Date(); var expiry = new Date(today.getTime() + 365 * 24 * 60 * 60 * 1000);
function getCookieVal (offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1) { endstr = document.cookie.length; }
return unescape(document.cookie.substring(offset, endstr));
}
function GetCookie (name) {
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen) {
var j = i + alen;
if (document.cookie.substring(i, j) == arg) {
return getCookieVal (j);
}
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
return null;
}
function SetCookie (name,value,expires,path,domain,secure) {
document.cookie = name + "=" + escape (value) +
((expires) ? "; expires=" + expires : "") +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
((secure) ? "; secure" : "");
}
document.getElementById(cookie).onclick = function addToChart()
{
var cookieValue = document.getElementById(cookie).value;
var cookiearray = new Array();
cookiearray = cookieValue.split(",");
return cookiearray;
}
Upvotes: 1
Views: 505
Reputation: 871
Ok I have solved my problem with PHP. Its so specific but I want to share it in case somebody needs it.
First of all I didnt give any specific name as product_id for the cookie but I just give the variable of the product_id "$ftc2" which is clicked, shown as below
onclick=\"SetCookie('".$ftc2."', '".$ftc2."', 'expiry');
So whenever user clicks on any product, a new cookie is created with the name of the product_id like name=2 or name=32 whatever.
then I have created a foreach on the chart page as below
<?php
foreach($_COOKIE as $cookies)
{
echo $cookies;
}
?>
It displayes now all stored cookie values(in my case product ids) and now I can manipulate it as I want and display all products which is added to chart
Maybe it is so easy and so common but somehow I couldnt find it out since morning = ))
Upvotes: 1