My site www.woodwerxofyorkshire.co.uk has been working correctly for the last 12 months but about a week ago, I started receiving error messages and certain functions no longer work correctly. One of these is the shopping basket. Items are placed in the basket correctly but when the quantity is changed within the basket, it no longer changes the quantity. This has only just started happening over the last week and having revisited the code, I cannot see why this would be. I have contacted my hosting provider (Ipage) and they have insisted that they haven't changed anything, however neither have I. I was wondering whether somebody could review my code (second pair of eyes) to see if I'm missing something in relation to the change of quantity. I have pasted the code below and any help would be massively appreciated.
<?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";
?>
<?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: shopping_basket.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"]);
}
}
?>
I looked at your code, and making a few assumptions I was able to duplicate the problem and arrive at a solution. The problem lies in section 3.
Try changing your code accordingly:
PHP Code:
$i = 0; foreach ($_SESSION["cart_array"] as $each_item) { 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, array(array("item_id" => $item_to_adjust, "quantity" => $quantity))); } // close if condition } // close while loop $i++; } // close foreach loop
Note that I made two changes.
(1) I moved $i++ to the bottom of the foreach
(2) I changed $i-1 to $i in the arrayslice() function call.
I've tried the amendments you suggested but I am still incurring the same error. Items can be placed in the basket but when changing the quantity, the page refreshes but the quantity doesn't actually change.
Frustrating to say the least because it was working perfectly until last week. Whether I was getting lucky as a newbie to php I'm not sure. Don't suppose you have any more suggestions? I really appreciate the help.
I've tried the amendments you suggested but I am still incurring the same error. Items can be placed in the basket but when changing the quantity, the page refreshes but the quantity doesn't actually change.
Frustrating to say the least because it was working perfectly until last week. Whether I was getting lucky as a newbie to php I'm not sure. Don't suppose you have any more suggestions? I really appreciate the help.
//Section 3 (if user chooses to adjust item quantity)
//execute some code
$item_to_adjust = "Two Blocks of Iroko No.2";
$quantity = "1";
$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 ($cart_array as $each_item) {
# print_r($each_item);
while (list($key, $value) = each($each_item)) {
# echo "*** [$key - $value] ***\n";
$item_id = "item_name_".$i;
$quantity_id = "quantity_".$i;
# change to true for debugging
if (false) {
echo "checking key [$key] [$item_id]\n";
if ($key == $item_id) {
echo "$key == $item_id\n";
}
echo "checking value [$value] [$item_to_adjust]\n";
if ($value == $item_to_adjust) {
echo "$value == $item_to_adjust\n";
}
}
if ($key == $item_id && $value == $item_to_adjust) {
# echo "found\n";
//That item is in cart already so let's adjust its quantity using array_splice()
array_splice($cart_array, $i, 1, array(array($item_id => $item_to_adjust, $quantity_id => $quantity)));
} // close if condition
# change to true for debugging
else if (false) {
echo "not found\n";
echo ">>> [$item_id - $value] <<<\n";
}
} // close while loop
$i++;
} // close foreach loop
Bookmarks