php - How to store favorites using session? -
is there can me question? i'm trying create "favorites" function on webpage. visitors can see different product pages , select 1 product favorite. on top of page image. when visitor clicks image, page loaded 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 product page need things displayed.
on top of product page there title. next title star displayed (an image src=favnee.png
, star without color). @ right side on page, in div
list of marked products displayed. text (the productname) can clicked visitor go productpage again. code of 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"; ($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, can mark multiple products favorite, unique names displayed text on right side of page, image src changed favja
(the same star color, visitor knows it's marked). but, click product , mark 1 favorit, changes image src, want, forgets other products marked before. list on right contains unique names, remembers products marked, image doesn't. image recognizes last product. new product marked unmarks other images.
what missing here? how can keep image marked regardless number of products marked?
as doing labels, you'll need craft array of images, , add every sources it. , display them in loop function (foreach
or whatever want)
Comments
Post a Comment