存储使用JavaScript数组饼干(或使用饼干作为一个数组) [英] storing cookies in an array with javascript(or using cookie as an array)

查看:136
本文介绍了存储使用JavaScript数组饼干(或使用饼干作为一个数组)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:
我用PHP解决它在某种程度上。它的那么具体,但我想如果有人需要它来分享。首先,我没有给任何具体名称的product_id的Cookie,但我只给如下所示

的是点击PRODUCT_ID$ FTC2的变量,

 的onclick = \\的setcookie(',$ FTC2。','到期'$ FTC2。');

所以每当用户点击任何产品,一个新的cookie是使用类似名称的product_id的名称创建= 2或名称= 32什么的。

然后我创建图表页面上一个foreach如下

 < PHP的foreach($ _ COOKIE为$饼干){$回声饼干; ?}>

现在里显示所有存储的cookie值(在我的情况下,产品ID),现在我可以操纵它,因为我想和显示这是添加到也许图表它是如此简单,如此普遍的所有产品,但不知何故,我无法找到它了从早晨=))我希望这样可以节省别人的时间。

问题是:


我建立一个网上商店和我想要做的就是在显示页面showChart点击的所有产品。(一个很简单的网页,其中显示您通过点击chart_icon添加到您的图表中的产品)。我已经尝试了一些常见的几种方法,但我不能管理以某种方式做到这一点。我最后绝望的尝试是建立这个addToChart()函数。

由于设置cookie部分作品好,我简化了code和粘贴刚才的重要组成部分(在我看来)。任何形式的帮助或建议将是AP preciated。我不希望任何人写的code我。

在此先感谢

 为($ I = 0; $ I< $ NUM; $ I ++)
        {
            $ FTC = mysql_result($结果,$ Iproduct_image);
            $ FTC2 = mysql_result($结果,$ I的product_id);
            $ FTC3 = mysql_result($结果,$ IPRODUCT_NAME);
            $ ftc4 = mysql_result($结果,$ Iproduct_ preIS);
            $ ftc5 = mysql_result($结果,$ Iproduct_old_ preIS);
            回声< A HREF ='details.php产品=$ FTC2。'>< D​​IV CLASS ='products_list'>
            < IMG WIDTH =217px高度=323pxSRC =$ FTC。/>
            < D​​IV CLASS ='alt_yazi2'>< B>中。$ FTC3 &安培; NBSP;&安培; NBSP;&安培; NBSP;&安培;&NBSP LT; FONT风格=浮动:权利;'> TL&功放; NBSP;&安培;&NBSP LT; B风格='文字装饰$ ftc4。:线通过;浮动:权利;'> TL< / B>< / FONT>< / B>< / A>中$ ftc5。
    < / BR>
    < D​​IV ID ='单眼皮'><一个ID ='饼干'的onclick = \\的setcookie('cookie_product,$ FTC2。','到期'); \\的href =# '>
    < IMG宽度='35px'高度='25px的SRC =图像/ cart_icon.png/>

JAVASCRIPT code从这里开始的。

今天VAR =新的日期();
        VAR到期=新的日期(today.getTime()+ 365 * 24 * 60 * 60 * 1000);

 函数getCookieVal(偏移){
      变种endstr = document.cookie.indexOf(;,偏移量);
      如果(endstr == -1){endstr = document.cookie.length; }
      返回UNESCAPE(document.cookie.substring(偏移,endstr));
      }    函数的getCookie(名称){
      VAR ARG =名称+=;
      VAR阿伦= arg.length;
      VAR CLEN = document.cookie.length;
      变种I = 0;
      而(I< CLEN){
        变种J = +阿伦;
        如果(document.cookie.substring(I,J)== ARG){
          返回getCookieVal(J);
          }
        I = document.cookie.indexOf(,I)+ 1;
        如果(我== 0)打破;
        }
      返回null;
      }    函数的setcookie(名称,值,过期,路径,域,安全){      的document.cookie =名称+=+越狱(值)+
        ((过期);到期=+到期:)+
        ((路径);路径=+路径:)+
        ((域);域=+域名:)+
        ((安全)安全:);      }    的document.getElementById(饼干).onclick =功能addToChart()
            {
                VAR cookieValue =的document.getElementById(饼干).value的;
                VAR cookiearray =新的Array();
                cookiearray = cookieValue.split(,);
                返回cookiearray;            }


解决方案

好吧,我已经解决了我用PHP的问题。它的那么具体,但我想如果有人需要它,分享它。

首先我没有给任何具体名称的product_id的Cookie,但我只给如下所示

的是点击PRODUCT_ID$ FTC2的变量,

 的onclick = \\的setcookie(',$ FTC2。','到期'$ FTC2。');

所以每当用户点击任何产品,一个新的cookie是使用类似名称的product_id的名称创建= 2或名称= 32什么的。

然后我创建图表页面上一个foreach如下

 < PHP
的foreach($ _ COOKIE为$饼干)
     {
             回声$饼干;
      }?>

现在里显示所有存储的cookie值(在我的情况下,产品ID),现在我可以操纵它,因为我想和显示将被添加到图表的所有产品

也许它是那么容易,因此常见的,但不知何故,我无法找到它了,因为早上=))

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 ." &nbsp;&nbsp;&nbsp;&nbsp <font      

style='float:right;'>".$ftc4."TL &nbsp;&nbsp<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;

            }

解决方案

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 = ))

这篇关于存储使用JavaScript数组饼干(或使用饼干作为一个数组)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆