CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   PHP (http://www.codingforums.com/forumdisplay.php?f=6)
-   -   Help with shopping cart, PHP code (http://www.codingforums.com/showthread.php?t=273355)

fightapilotdean 09-17-2012 12:50 PM

Help with shopping cart, PHP code
 
Hi, i have been following a tutorial to create a php/mysql shopping cart by TheTutSpace on youtube and i have come accross an error with the script. When i run it on my page i get

"Notice: Undefined index: quantity- in C:\xampp\htdocs\mySite\cart.php on line 9"

Code for cart.php :

<?php
if(isset($_POST['submit'])){
foreach($_POST as $key => $value){
$key = explode("-",$key);
$key = end($key);
$key = explode("submit",$key);
$key = end($key);

if($_POST['quantity-'.$key] == 0){
unset($_SESSION['cart'][$key]);
echo "Unset session";
} else {
$_SESSION['cart'][$key]['quantity'] = $value;
echo "Updated session";
}
}
} error_reporting(0);
?>
<h1>View Cart</h1>
<a href="index.php?page=products" title="Go back to products page">Go back to products page</a>
<?php $sql = "SELECT * FROM products WHERE id_products IN (";
foreach($_SESSION['cart'] as $id => $value){
$sql .= $id . ",";
}
$sql = substr($sql,0,-1).") ORDER BY name ASC";
$query = mysql_query($sql);
if(empty($query)){
echo "<br /><span class='i'>You need to add an item before you can view it here</span>";
}
?>
<form method="post" action="index.php?page=cart">
<fieldset>
<table>
<tr>
<th>Name</th>
<th>Quantity</th>
<th>Price per item</th>
<th>Total Cost</th>
</tr>
<?php

$sql = "SELECT * FROM products WHERE id_products IN (";
foreach ($_SESSION['cart'] as $id => $value){
$sql .= $id . ",";
}
$sql = substr($sql,0,-1).") ORDER BY name ASC";
$query = mysql_query($sql);
$total_price = 0;
if(!empty($query)){
while($row = mysql_fetch_array($query)){
$subtotal = $_SESSION['cart'][$row['id_products']]['products']*$row['price'];
$total_price += $subtotal;
?>
<tr>
<td><?php echo $row['name'];?></td>
<td><input type="text" name="quantity-<?php echo $row['id_products'];?>" size="5" value="<?php echo $_SESSION['cart'][$row['id_products']]['quantity'];?>" /></td>
<td><?php echo "£" . $row['price'];?></td>
<td><?php echo "£" . $_SESSION['cart'][$row['id_products']]['quantity']*$row['price'];?></td>
</tr>
<?php
} }
?>



<tr>
<td> </td>
<td></td>
<td>Total Price</td>
<td><?php echo "£" . $total_price;?></td>
</tr>
</table>
<input type="submit" name="submit" value="Update Cart" /></fieldset>
</form>
<p> To remove an item, set quantity to 0</p>

if relevant code for index.php:


<?php
session_start();
require_once("includes/connect.php");
if(isset($_GET['page'])){
$pages = array("products","cart");
if(in_array($_GET['page'],$pages)){
$page = $_GET['page'];
} else {
$page = "products";
}
} else {
$page = "products";
}
?>
<html>
<head>
<link rel="stylesheet" href="css/reset.css" />
<link rel="stylesheet" href="css/style.css" />
<title>Shopping Cart</title>
</head>
<body>
<div id="container">
<div id="main"><?php require($page . ".php"); ?></div>
<div id="sidebar"><h1>Cart</h1>
<?php
if(isset($_SESSION['cart'])){
$sql = "SELECT * FROM products WHERE id_products IN (";
foreach($_SESSION['cart'] as $id => $value){
$sql .= $id. ",";
}
$sql = substr($sql,0,-1) . ") ORDER BY id_products ASC";
$query = mysql_query($sql);
while($row = mysql_fetch_assoc($query)){
?>
<p><?php echo $row['name']; ?><?php echo " " . $_SESSION['cart'][$row['id_products']]['quantity']; ?></p>
<a href="index.php?page=cart">Got to cart</a>
<?php
}
} else {
echo "<p>Your cart is empty. <br />Please add some products</p>";
}

?></div>
</div>
</body>
</html>

if anyone could give me some pointers as to which bit is wrong it would be greatly appreciated. with the video tutorials i got a little confused when it came to the updates that were made. i have tried restarting several times and i seem to get the same error everytime :(
thanks for any help in advance

nevenne 09-17-2012 11:55 PM

l can make all script for you

tangoforce 09-18-2012 12:23 AM

Quote:

Originally Posted by fightapilotdean (Post 1270610)
"Notice: Undefined index: quantity- in C:\xampp\htdocs\mySite\cart.php on line 9"

PHP Code:

<?php
    
if(isset($_POST['submit'])){
        foreach(
$_POST as $key => $value){
            
$key explode("-",$key);
            
$key end($key);
            
$key explode("submit",$key);
            
$key end($key);
            
            if(
$_POST['quantity-'.$key] == 0){


Your $key variable contains no value. Look at the error message - your index in the $_POST array doesn't exist. Sure, you might have $_POST['quantity-1'], $_POST['quantity-2'], $_POST['quantity-3'] etc but where is your $_POST['quantity-'] ? - Chances are there isn't one.

Something is wrong with $key. As you're generating your form dynamically, you need to show us the outputted html form for us to have a better understanding of the problem here. It could be that your form isn't being output correctly or that you're not processing it correctly. Either way, going for a generic value of quantity-X where X is a key from the post array is asking for trouble - you're in essence looking for a index in your array with every form field name such as quanitity-submit (yup, thats right, you're even testing for the submit button in your code).

A better way would be:
PHP Code:

print "<input type=\"text\" name=\"quantity[$row[id_products]]\" value=\"\">" 

The [] means its an array. You then process it in PHP like:
PHP Code:

<?php
foreach ($_POST['quantity'] as $key => $value//$key is the $row['id_products'] from your db
?>

Finally, there is a sticky topic in the main forum about using [php] and [/php] tags. It applies to everyone here and its considered a bit rude to post many lines of code as you have and ask us to fix it without using them. You have 13 posts now so you've had time to read the sticky so please act on it! The php tags have the effect you've seen in my reply and make it a lot easier for us to read your code. Pretty please would you use them in the future? :thumbsup:


All times are GMT +1. The time now is 02:29 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.