I've got a cart that displays products when they're added, but not simply viewed. Try it @ [URL unfurl="true"]http://lxyltd.com/products.html[/url]. I don't understand why the count($_SESSION["cart_array"]) displays '0' when there's clearly something in it!
Here's the code that sits inside an iFrame on the page:
Here's the code that sits inside an iFrame on the page:
Code:
<?php
session_start(); // Start session first thing in script
// Script Error Reporting
error_reporting(E_ALL);
ini_set('display_errors', '1');
// Connect to the MySQL database
include "storescripts/connect_to_mysql.php";
function money_format($format, $number) {
$regex = '/%((?:[\^!\-]|\+|\(|\=.)*)([0-9]+)?'.
'(?:#([0-9]+))?(?:\.([0-9]+))?([in%])/';
if (setlocale(LC_MONETARY, 0) == 'C') {
setlocale(LC_MONETARY, '');
}
$locale = localeconv();
preg_match_all($regex, $format, $matches, PREG_SET_ORDER);
foreach ($matches as $fmatch) {
$value = floatval($number);
$flags = array(
'fillchar' => preg_match('/\=(.)/', $fmatch[1], $match) ?
$match[1] : ' ',
'nogroup' => preg_match('/\^/', $fmatch[1]) > 0,
'usesignal' => preg_match('/\+|\(/', $fmatch[1], $match) ?
$match[0] : '+',
'nosimbol' => preg_match('/\!/', $fmatch[1]) > 0,
'isleft' => preg_match('/\-/', $fmatch[1]) > 0
);
$width = trim($fmatch[2]) ? (int)$fmatch[2] : 0;
$left = trim($fmatch[3]) ? (int)$fmatch[3] : 0;
$right = trim($fmatch[4]) ? (int)$fmatch[4] : $locale['int_frac_digits'];
$conversion = $fmatch[5];
$positive = true;
if ($value < 0) {
$positive = false;
$value *= -1;
}
$letter = $positive ? 'p' : 'n';
$prefix = $suffix = $cprefix = $csuffix = $signal = '';
$signal = $positive ? $locale['positive_sign'] : $locale['negative_sign'];
switch (true) {
case $locale["{$letter}_sign_posn"] == 1 && $flags['usesignal'] == '+':
$prefix = $signal;
break;
case $locale["{$letter}_sign_posn"] == 2 && $flags['usesignal'] == '+':
$suffix = $signal;
break;
case $locale["{$letter}_sign_posn"] == 3 && $flags['usesignal'] == '+':
$cprefix = $signal;
break;
case $locale["{$letter}_sign_posn"] == 4 && $flags['usesignal'] == '+':
$csuffix = $signal;
break;
case $flags['usesignal'] == '(':
case $locale["{$letter}_sign_posn"] == 0:
$prefix = '(';
$suffix = ')';
break;
}
if (!$flags['nosimbol']) {
$currency = $cprefix .
($conversion == 'i' ? $locale['int_curr_symbol'] : $locale['currency_symbol']) .
$csuffix;
} else {
$currency = '';
}
$space = $locale["{$letter}_sep_by_space"] ? ' ' : '';
$value = number_format($value, $right, $locale['mon_decimal_point'],
$flags['nogroup'] ? '' : $locale['mon_thousands_sep']);
$value = @explode($locale['mon_decimal_point'], $value);
$n = strlen($prefix) + strlen($currency) + strlen($value[0]);
if ($left > 0 && $left > $n) {
$value[0] = str_repeat($flags['fillchar'], $left - $n) . $value[0];
}
$value = implode($locale['mon_decimal_point'], $value);
if ($locale["{$letter}_cs_precedes"]) {
$value = $prefix . $currency . $space . $value . $suffix;
} else {
$value = $prefix . $value . $space . $currency . $suffix;
}
if ($width > 0) {
$value = str_pad($value, $width, $flags['fillchar'], $flags['isleft'] ?
STR_PAD_RIGHT : STR_PAD_LEFT);
}
$format = str_replace($fmatch[0], $value, $format);
}
return $format;
}
?>
<?php
///////////////////////////////////////////////////////////////////////////////////////////////
// Section 1 (if user attempts to add something to the cart from the product page) //
///////////////////////////////////////////////////////////////////////////////////////////////
if (isset($_POST['pid'])) {
$pid = $_POST['pid'];
$wasFound = false;
$i = 0;
// If the cart session variable is not set or cart array is empty
if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) {
// RUN IF THE CART IS EMPTY OR NOT SET
$_SESSION["cart_array"] = array(0 => array("item_id" => $pid, "quantity" => 1));
} else {
// RUN IF THE CART HAS AT LEAST ONE ITEM IN IT
foreach ($_SESSION["cart_array"] as $each_item) {
$i++;
while (list($key, $value) = each($each_item)) {
if ($key == "item_id" && $value == $pid) {
// That item is in cart already so let's adjust its quantity using array_splice()
array_splice($_SESSION["cart_array"], $i-1, 1, array(array("item_id" => $pid, "quantity" => $each_item['quantity'] + 1)));
$wasFound = true;
} // close if condition
} // close while loop
} // close foreach loop
if ($wasFound == false) {
array_push($_SESSION["cart_array"], array("item_id" => $pid, "quantity" => 1));
}
}
header("location: cart.php");
exit();
}
?>
<?php
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Section 2 (if user chooses to empty their shopping cart)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (isset($_GET['cmd']) && $_GET['cmd'] == "emptycart") {
unset($_SESSION["cart_array"]);
}
?>
<?php
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Section 3 (if user chooses to adjust item quantity)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (isset($_POST['item_to_adjust']) && $_POST['item_to_adjust'] != "") {
// execute some code
$item_to_adjust = $_POST['item_to_adjust'];
$quantity = $_POST['quantity'];
$quantity = preg_replace('#[^0-9]#i', '', $quantity); // filter everything but numbers
if ($quantity >= 100) { $quantity = 99; }
if ($quantity < 1) { $quantity = 1; }
if ($quantity == "") { $quantity = 1; }
$i = 0;
foreach ($_SESSION["cart_array"] as $each_item) {
$i++;
while (list($key, $value) = each($each_item)) {
if ($key == "item_id" && $value == $item_to_adjust) {
// That item is in cart already so let's adjust its quantity using array_splice()
array_splice($_SESSION["cart_array"], $i-1, 1, array(array("item_id" => $item_to_adjust, "quantity" => $quantity)));
} // close if condition
} // close while loop
} // close foreach loop
}
?>
<?php
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Section 4 (if user wants to remove an item from cart)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (isset($_POST['index_to_remove']) && $_POST['index_to_remove'] != "") {
// Access the array and run code to remove that array index
$key_to_remove = $_POST['index_to_remove'];
if (count($_SESSION["cart_array"]) <= 1) {
unset($_SESSION["cart_array"]);
} else {
unset($_SESSION["cart_array"]["$key_to_remove"]);
sort($_SESSION["cart_array"]);
}
}
?>
<?php
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Section 5 (render the cart for the user to view on the page)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
echo "Cart: ".count($_SESSION["cart_array"]);
$cartOutput = "";
$cartTotal = "";
$pp_checkout_btn = '';
$product_id_array = '';
if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) {
$cartOutput = "<h2 align='center'>Your shopping cart is empty</h2>";
} else {
// Start PayPal Checkout Button
$pp_checkout_btn .= '<form action="[URL unfurl="true"]https://www.paypal.com/cgi-bin/webscr"[/URL] method="post" target="_parent">
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="upload" value="1">
<input type="hidden" name="business" value="sales@lxyltd.com">';
// Start the For Each loop
$i = 0;
foreach ($_SESSION["cart_array"] as $each_item) {
$item_id = $each_item['item_id'];
$sql = mysql_query("SELECT * FROM products WHERE id='$item_id' LIMIT 1");
while ($row = mysql_fetch_array($sql)) {
$product_name = $row["product_name"];
$price = $row["price"];
$details = $row["details"];
}
$pricetotal = $price * $each_item['quantity'];
$cartTotal = $pricetotal + $cartTotal;
setlocale(LC_MONETARY, "en_GB");
$pricetotal = number_format($pricetotal, 2, '.', ''); //money_format("%10.2n", $pricetotal);
// Dynamic Checkout Btn Assembly
$x = $i + 1;
$pp_checkout_btn .= '<input type="hidden" name="item_name_' . $x . '" value="' . $product_name . '">
<input type="hidden" name="amount_' . $x . '" value="' . $price . '">
<input type="hidden" name="quantity_' . $x . '" value="' . $each_item['quantity'] . '"> ';
// Create the product array variable
$product_id_array .= "$item_id-".$each_item['quantity'].",";
// Dynamic table row assembly
/*$maxwidth = 80;
list($width,$height) = getimagesize("inventory_images/" . $item_id . ".jpg");
if ($width > $maxwidth) {
$newheight = $maxwidth/$width * $height;
} else{
$newheight = $height;
} */
$max_width = 80;
$max_height = 80;
$img = "inventory_images/". $item_id .".jpg";
list($original_width, $original_height) = getimagesize($img);
if (($original_width > $max_width) OR ($original_height > $max_height)){
//original width exceeds, so reduce the original width to maximum limit. calculate the height according to the maximum width.
if(($original_width > $max_width) AND ($original_height <= $max_height)){
$percent = $max_width/$original_width;
$new_width = $max_width;
$new_height = round ($original_height * $percent);
}
//image height exceeds, recudece the height to maxmimum limit. calculate the width according to the maximum height limit.
if(($original_width <= $max_width) AND ($original_height > $max_height)){
$percent = $max_height/$original_height;
$new_height = $max_height;
$new_width = round ($original_width * $percent);
}
//both height and width exceeds. but image can be vertical or horizontal.
if(($original_width > $max_width) AND ($original_height > $max_height)){
//if image has more width than height. resize width to maximum width.
if ($original_width > $original_height){
$percent = $max_width/$original_width;
$new_width = $max_width;
$new_height = round ($original_height * $percent );
}else{ //image is vertical or square. More height than width. resize height to maximum height.
$new_height = $max_height;
$percent = $max_height/$original_height;
$new_height = $max_height;
$new_width = round ($original_width * $percent);
}
}
}
$cartOutput .= "<tr>";
$cartOutput .= '<td valign="top"><a href="product.php?id=' . $item_id . '">' . $product_name . '</a><br /><div align="left"><img src="inventory_images/' . $item_id . '.jpg" alt="' . $product_name. '" width="'.$new_width.'" height="'.$new_height.'" border="0" /></div></td>';
$lim_details = substr($details,0,100);
//$cartOutput .= '<td valign="middle">' . $lim_details . '...</td>';
$cartOutput .= '<td align="right" valign="middle">£' . $price . '</td>';
$cartOutput .= '<td valign="middle" align="center">
<form action="cart.php" method="post"><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td><input name="quantity" type="text" value="' . $each_item['quantity'] . '" size="1" maxlength="2" /> </td><td> <input name="adjustBtn' . $item_id . '" type="submit" value="change" /><input name="item_to_adjust" type="hidden" value="' . $item_id . '" /></td></tr></table></form></td>';
//$cartOutput .= '<td>' . $each_item['quantity'] . '</td>';
$cartOutput .= '<td align="right" valign="middle">£' . $pricetotal . '</td>';
$cartOutput .= '<td align="center" valign="middle"><form action="cart.php" method="post"><input name="deleteBtn' . $item_id . '" type="submit" value="X" /><input name="index_to_remove" type="hidden" value="' . $i . '" /></form></td>';
$cartOutput .= '</tr>';
$i++;
}
setlocale(LC_MONETARY, "en_GB");
$cartTotal = number_format($pricetotal, 2, '.', ''); //money_format("%10.2n", $cartTotal);
$cartTotal = "<div style='font-size:18px; margin-top:12px;' align='right'>Cart Total : £".$cartTotal."</div>";
// Finish the Paypal Checkout Btn
$pp_checkout_btn .= '<input type="hidden" name="custom" value="' . $product_id_array . '">
<input type="hidden" name="notify_url" value="[URL unfurl="true"]https://www.lxyltd.com/storetemp/storescripts/my_ipn.php">[/URL]
<input type="hidden" name="return" value="[URL unfurl="true"]http://www.lxyltd.com/checkout_complete.html">[/URL]
<input type="hidden" name="rm" value="2">
<input type="hidden" name="cbt" value="Return to The Store">
<input type="hidden" name="cancel_return" value="[URL unfurl="true"]https://www.lxyltd.com/storetemp/paypal_cancel.php">[/URL]
<input type="hidden" name="lc" value="GB">
<input type="hidden" name="currency_code" value="GBP">
<input type="image" src="images/LXY_checkout.png" name="submit" alt="Make payments with PayPal - its fast, free and secure!">
</form>';
}
$copyYear = 2012;
$curYear = date('Y');
$dottydate = $copyYear . (($copyYear != $curYear) ? '-' . $curYear : '');
$sectionid = 10;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<title>LXY Ltd Hair Care Products - Your Basket</title>
<meta name="resource-type" content="document">
<meta name="rating" content="general">
<meta name="robots" content="ALL">
<meta name="description" content="" />
<meta name="abstract" content="">
<meta name="keywords" content="">
<meta name="author" content="Chris Gray @Qudos Internet">
<meta name="copyright" content="Chris Gray ©Qudos Internet <?php echo $dottydate ?>">
<meta name="distribution" content="Global">
<meta http-equiv="content-language" content="EN">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="style/generic.css" type="text/css" media="all" rel="stylesheet" />
<script src="storescripts/rollover.js"></script>
<link rel="icon" type="image/ico" href="/favicon.ico">
<link rel="stylesheet" href="style/style.css" type="text/css" media="screen" />
<style type="text/css">
<!--
html, body {
background-color: #FFF;
padding: 0;
margin-left: 0px;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 20px;
}
a:link,
a:visited {
text-decoration:none;
color:#666;
font-family: Georgia, "Times New Roman", Times, serif;
font-size: 14px;
}
a:hover,
a:active {
text-decoration:none;
color:#FF0000;
}
.oddtext{
font-family: Georgia, "Times New Roman", Times, serif;
font-size: 14px;
color: #000; text-decoration: none;
text-align: left;
}
.footerHeader{
font-family: Georgia, "Times New Roman", Times, serif;
font-size: 20px;
color: #000; text-decoration: none
text-align: left;
}
-->
</style>
</head>
<body>
<div align="center" id="mainWrapper">
<div id="pageContent">
<div style="margin:24px; text-align:left;">
<br />
<table width="700" align="center" border="0" cellspacing="0" cellpadding="6">
<tr>
<td width="250" align="left" bgcolor="#fff"><strong>Product</strong></td>
<td width="80" align="right" bgcolor="#fff"><strong>Unit Price</strong></td>
<td width="120" align="center" bgcolor="#fff"><strong>Quantity</strong></td>
<td width="90" align="right" bgcolor="#fff"><strong>Total</strong></td>
<td align="center" bgcolor="#fff"><strong>Remove</strong></td>
</tr>
<?php echo $cartOutput; ?>
<tr>
<td colspan="6" align="right"><?php echo $cartTotal; ?>
<br />
<div align="right"><?php echo $pp_checkout_btn; ?></div></td>
</tr>
<tr>
<td align="center" valign="top" background="images/LXY-Products-panel.png"><br />
<a href="cart.php?cmd=emptycart">Empty the Basket</a></td>
</tr>
</table>
</div>
</div>
</div>
</body>
</html>