Reputation: 4974
I have this code (in Floral.php) that I was given to me as a shopping cart app which works on one (1) web page; I had to dismember it because of the structure of my web pages and now I'm trying to put it back together so it works with multiple web pages (files):
<?php
session_start();
define("PRODUCTCODE", 0);
define("PRODUCTNAME", 1);
define("QUANTITY", 2);
define("PRICE", 3);
var_dump($_POST);
$action = isset($_POST['action']) ? $_POST['action'] : '';
if($action == 'Recalculate') {
RecalculateCart();
}
else if(isset($_POST['Check Out'])) {
header("Location: "."./customer.php");
}
function RecalculateCart() { // something changed, so recalculate everything
$cart = isset($_SESSION['cart']) ? $_SESSION['cart'] : ''; // get current cart for this session (it's not saved between browser sessions!))
$itemcount = isset($_SESSION['itemcount']) ? $_SESSION['itemcount'] : 0; // get the item count
echo "itemcount from SESSION: " .$itemcount;
for ($i=0; $i < $itemcount; $i++) {
$quantity = $_POST['quantity'.[$i]]; // get quantity of new item
echo "quantity from POST: ".$quantity;
if (empty($quantity)) { // quantity is 'empty' so make it zero (0))
$quantity = 0;
}
else if (($quantity < 0) || (!is_numeric($quantity))) { // validate it
$quantity = 0;
}
$cart[QUANTITY][$i] = intval($quantity); // passed validation, so move it to the cart
}
for ($j=0; $j < $itemcount; $j++) {
$quantity = $cart[QUANTITY] [$j]; // add the quantity of the new item to accumulation
if ($quantity == 0) { // remove item from the cart
$itemcount--;
$curitem = $j;
while(($curitem+1) < count($cart[0])) {
for($k = 0; $k < 4; $k++) {
$cart[$k][$curitem] = $cart[$k][$curitem+1];
$cart[$k] [$curitem+1] = '';
}
$curitem++;
}
}
}
$_SESSION['itemcount'] = $itemcount; // save the item count
$_SESSION['cart'] = $cart; // save cart contents
}
?>
$itemcount is 4, according to the echo, which is correct. $quantity is set in another file and becomes part of $cart (it's from a text field in a form; there is a submit button in that form which does the POST). This is that code (in viewCart.php):
<?php
session_start();
define("PRODUCTCODE", 0);
define("PRODUCTNAME", 1);
define("QUANTITY", 2);
define("PRICE", 3);
$action = isset($_POST['action']) ? $_POST['action'] : '';
if (isset($_POST['productcode'])) {
AddToCart();
}
function AddToCart() {
$cart = isset($_SESSION['cart']) ? $_SESSION['cart'] : '';
$itemcount = isset($_SESSION['itemcount']) ? $_SESSION['itemcount'] : 0;
$cart[PRODUCTCODE][$itemcount] = $_POST['productcode'];
$cart[PRODUCTNAME][$itemcount] = $_POST['productname'];
$cart[QUANTITY][$itemcount] = intval($_POST['quantity']);
$cart[PRICE][$itemcount] = $_POST['price'];
$itemcount = $itemcount + 1;
$_SESSION['cart'] = $cart;
$_SESSION['itemcount'] = $itemcount;
echo "addToCart-itemcount: ".$itemcount;
}
?>
Being a somewhat of a noob at PHP, I am assuming that the POST in the first code segment is correct, except for the fact it's in another file which makes it NOT global in scope. So does the global keyword apply here? and if so, where do I put it? (in which file and which statement)?
Upvotes: 1
Views: 85
Reputation: 12419
You're using incorrect syntax to access the quantity array item:
$quantity = $_POST['quantity'.[$i]];
The [$i]
actually creates a new array containing the item $i
(using the array syntax introduced in PHP 5.4), and when you try to use it as a string it gets output as just 'Array'. So it evaluates to $_POST['quantityArray']
. If you have notices enabled you should see an "Array to string conversion" notice to help you catch this sort of thing.
So simply change it to:
$quantity = $_POST['quantity'.$i];
Or:
$quantity = $_POST["quantity$i"];
BTW, notices can be quite helpful for detecting bugs, so make sure you have them enabled. Either enable display_errors
and error_reporting
in php.ini or add this to your code (but make sure you only show errors on your local or development server, since reporting them on a production server can be a security risk):
ini_set('display_errors', 1);
error_reporting(E_ALL);
Upvotes: 3