简体   繁体   English

如何使用会话存储收藏夹?

[英]How to store favorites using session?

Is there anyone who can help me with this question? 有谁可以帮助我解决这个问题? I'm trying to create a "favorites" function on my webpage. 我正在尝试在网页上创建“收藏夹”功能。 Visitors can see different product pages and select one product as favorite. 访客可以看到不同的产品页面,并选择一种产品作为收藏。 On top of the page is an image. 页面顶部是图像。 When the visitor clicks this image, a page is loaded with this line of code: 当访客单击此图像时,页面将加载以下代码行:

<?php 
session_start();

ini_set('display_errors', 1);
error_reporting(E_ALL);

define("PRODUCTNAME", 1);
define("inURL", 1);

if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
if (isset($_POST['productname']))
{
  AddToCart();
}
} 


function AddToCart()
{
$cart = isset($_SESSION['cart']) ? $_SESSION['cart'] : '';
$itemcount = isset($_SESSION['itemcount']) ? $_SESSION['itemcount'] : 0;
$event = $_POST['productname'];
$savelink = $_POST['url'];

$cart[PRODUCTNAME][$itemcount] = array('eventnaam' => $event, 'savelink' => $savelink, 'favSRC' => 'favJA');
$itemcount = $itemcount + 1;

$_SESSION['cart'] = $cart;
$_SESSION['itemcount'] = $itemcount;
header('Location: ' . $_POST['url']);
exit;
} 
?>

This code creates different sessionvalues, because on the product page I need some things displayed. 这段代码创建了不同的sessionvalue,因为在产品页面上,我需要显示一些东西。

On top of the product page there is the title. 产品页面顶部是标题。 Next to the title a star is displayed (an image with src=favNEE.png , a star without color). 在标题旁边显示一个星星(带有src=favNEE.png的图像,没有颜色的星星)。 At the right side on the page, in another div a list of marked products is displayed. 在页面右侧的另一个div中,将显示标记产品的列表。 This text (the productname) can be clicked so the visitor will go to the productpage again. 可以单击此文本(产品名称),以便访问者再次转到产品页面。 The code of the product page: 产品页面的代码:

<?php 
session_start();

ini_set('display_errors', 1);
error_reporting(E_ALL);

define("PRODUCTNAME", 1);

$cart = isset($_SESSION['cart']) ? $_SESSION['cart'] : '';
$itemcount = isset($_SESSION['itemcount']) ? $_SESSION['itemcount'] : 0;
$strHTML = "";

if ($itemcount == 0)
{
$strHTML = "<font class='bewaardeItems'>U heeft nog geen evenementen aangeklikt.    </font>";
$imageSRC = 'favNEE';
}
else
{
$strHTML = "<div style=\"overflow:auto; height=358px;\">"."\n";
$strHTML .= "<table border=\"0\" cellpadding=\"3\" cellspacing=\"2\"   width=\"100%\">"."\n";

for ($i=0; $i<$itemcount; $i++)
{
  $strHTML .= "<tr>"."\n";
  $strHTML .= "<td><a href='".$cart[PRODUCTNAME][$i]['savelink']."'    class='bewaardeItems'>".$cart[PRODUCTNAME][$i]['eventnaam']."</a></td>"."\n";
  $strHTML .= "</tr>"."\n";
  if ($cart[PRODUCTNAME][$i]['favSRC'] == "favJA" && $cart[PRODUCTNAME][$i]  ['eventnaam'] == "Blackout") {
        $imageSRC = 'favJA';
  }
  else {
        $imageSRC = 'favNEE';
  }
 }

 $strHTML .= "</table>"."\n";
 $strHTML .= "</div>"."\n";
};
?>

The above code works, I can mark multiple products as favorite, all unique names are displayed as text on the right side of the page, the image src is changed perfectly to favJA (the same star but with color, so the visitor knows it's marked). 上面的代码可以正常工作,我可以将多个产品标记为收藏,所有唯一的名称都以文本形式显示在页面右侧,图像src完美地更改为favJA (同一颗星,但是带有颜色,因此访问者知道favJA其标记为)。 But, as soon as I click another product and mark this one as favorit, it changes the image src, as I want, but forgets the other products that are marked before. 但是,当我单击另一种产品并将其标记为收藏时,它会根据需要更改图像src,但会忘记之前标记的其他产品。 The list on the right contains all the unique names, it remembers all the products that are marked, but the image doesn't. 右侧的列表包含所有唯一的名称,它会记住所有标记的产品,但图像不会。 The image only recognizes the last product. 图像仅识别最后的产品。 As soon as a new product is marked it unmarks all the other images. 一旦标记了新产品,它将不标记所有其他图像。

What am I missing here? 我在这里想念什么? How can I keep the image marked regardless the number of products which are marked? 无论标记了多少产品,如何保持图像被标记?

As you are doing with labels, you'll need to craft an array of images, and add every sources to it. 在处理标签时,您需要制作图像数组,并向其中添加所有来源。 And display them in a loop function ( foreach or whatever you want) 并在循环功能中显示它们( foreach或您想要的任何东西)

So I've created a workaround. 因此,我创建了一个解决方法。 I've added a new SESSION value to the code, holding the name of the productpage and the value of the image. 我在代码中添加了一个新的SESSION值,其中包含产品页面的名称和图像的值。

<?php 
session_start();

ini_set('display_errors', 1);
error_reporting(E_ALL);

 define("PRODUCTNAME", 1);
define("inURL", 1);

if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
   if (isset($_POST['productname']))
   {
      AddToCart();
   }
} 


function AddToCart()
{
   $cart = isset($_SESSION['cart']) ? $_SESSION['cart'] : '';
   $itemcount = isset($_SESSION['itemcount']) ? $_SESSION['itemcount'] : 0;
   $event = $_POST['productname'];
   $savelink = $_POST['url'];
   $longeventname = $_POST['longeventname'];
   $favIMGsrc = 'favJA';

   $cart[PRODUCTNAME][$itemcount] = array('eventnaam' => $event, 'savelink' =>     $savelink);
   $itemcount = $itemcount + 1;

   $_SESSION['cart'] = $cart;
   $_SESSION['event_url_'.$longeventname] = $favIMGsrc;
   $_SESSION['itemcount'] = $itemcount;
   header('Location: ' . $_POST['url']);
exit;
} 
?>

On the product page, simply call the session and let php compare it's value. 在产品页面上,只需调用session并让php比较它的价值即可。 If the value matches YES, the img src is set to YES. 如果该值与YES相匹配,则img src设置为YES。 If the value returns a NO, the img src is set to NO. 如果该值返回NO,则img src设置为NO。

<?php 
session_start();

ini_set('display_errors', 1);
error_reporting(E_ALL);

define("PRODUCTNAME", 1);

$cart = isset($_SESSION['cart']) ? $_SESSION['cart'] : '';
$eventURL = isset($_SESSION['event_url_blackout']) ? $_SESSION['event_url_blackout'] : '';
$itemcount = isset($_SESSION['itemcount']) ? $_SESSION['itemcount'] : 0;
$strHTML = "";

if ($itemcount == 0)
{
   $strHTML = "<font class='bewaardeItems'>U heeft nog geen evenementen aangeklikt.    </font>";
   $imageSRC = 'favNEE';
}
else
{
   $strHTML = "<div style=\"overflow:auto; height=358px;\">"."\n";
   $strHTML .= "<table border=\"0\" cellpadding=\"3\" cellspacing=\"2\"     width=\"100%\">"."\n";

   for ($i=0; $i<$itemcount; $i++)
   {
      $strHTML .= "<tr>"."\n";
      $strHTML .= "<td><a href='".$cart[PRODUCTNAME][$i]['savelink']."'     class='bewaardeItems'>".$cart[PRODUCTNAME][$i]['eventnaam']."</a></td>"."\n";
      $strHTML .= "</tr>"."\n";
   }

   $strHTML .= "</table>"."\n";
   $strHTML .= "</div>"."\n";
};

if ($eventURL == "favJA"){
    $imageSRC = 'favJA';
} else {
            $imageSRC = 'favNEE';
      }
?>

That solved the problem! 那解决了问题!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM