hallo allemaal gebruik dit webshopscriptje en heb hem wat aangepast hij werkt al redelijk alleen voegt mijn shoppingcart maar een product toe als ik een volgend product 'add' gooit hij het eerste er weer uit
iemand een mening hier over ? ( ben pas 3 dagen bezig met php dus sorry voor domme vragen )er zitten nog wat gebreken in maar dit is me voornaamste probleem

kunt het werkend zien op http://www.unlimited-trading.nl/index.php

hier is mijn script
index.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
<?php

require 'cart.php';
 
?>

	<div class="container"><!-- start container -->
		<div class="sixteen columns">
			<h1 class="remove-bottom" style="margin-top: 40px">Test Shpping Cart </h1>
			<hr />
		</div>
        <div id="products" class="two-thirds column">
            <h1>Products In Our Store</h1>
            <?php
            echo products();
            ?>
        </div>
        <div id="shoppingcart" class="one-third column">
            <h3>Your Cart</h3>
<?php
            echo cart();
                
        echo cart_qty();

?>
            <br>
        <br>
        <br>hoeveel producten
       
	</div></div><!-- end container -->


</body>
</html>

cart.php
<?php

	// Start the session
	
session_start;
//session_destroy();
//error_reporting(0);
	
//heb ik veranderd//
$page = 'index.php';
//heb ik veranderd//
mysql_connect('localhost', 'unlimi1q_user', '****') or die(mysql_error());
mysql_select_db('unlimi1q_webshop') or die(mysql_error());
// heb deze array zelf geplaatst//
$_SESSION = array();
// add item to cart
if (isset($_GET['add'])) {
$quantity = mysql_query('SELECT id, quantity FROM Tblproducts WHERE id=' . mysql_real_escape_string((int)$_GET['add']));  //prevents SQL injections
while($quantity_row = mysql_fetch_assoc($quantity)) {
if($quantity_row['quantity'] != $_SESSION['cart_' . (int)$_GET['add']]) {
$_SESSION['cart_' . (int)$_GET['add']] += '1';
			}
		}
header('Location: ' . $page);
	}
	
	
	// remove one item from cart
	if (isset($_GET['remove'])) {
		$_SESSION['cart_' . (int)$_GET['remove']] --;
		header('Location: ' . $page);
	}
	
	
	// delete item item from cart
	if (isset($_GET['delete'])) {
		$_SESSION['cart_' . (int)$_GET['delete']] = '0';
header('Location: ' . $page);
	}
	
	
	// display list of products
	function products() {
		
		 $get = mysql_query('SELECT id, name, description, price FROM Tblproducts WHERE quantity > 0 ORDER BY id ASC');
		
		if (mysql_num_rows($get) ==0) { 
			echo 'There are no products to display!';
			
			
		} else {
			while($get_row = mysql_fetch_assoc($get)) {
				echo '<p>' . $get_row['name'] . '<br>' . $get_row['description'] . '<br>' . number_format($get_row['price'], 2) . '<br>' .  '<br><a href="index.php?add=' . $get_row['id'] . '">Add</a></p>';
			}
		}
	}
	
	
	
					
		
	
	//display how many items are in the cart
	

		
	function cart() {
		foreach($_SESSION as $name => $value) {
			if($value>0) {
				if(substr($name, 0, 5) == 'cart_') {
					
					//get exact number after "'cart_'$id"
					$id = substr($name, 5, (strlen($name)-5));
					//echo $id;
					
					$get = mysql_query('SELECT id, name, price FROM Tblproducts WHERE id=' . mysql_real_escape_string((int)$id));  //prevents SQL injections
					while($get_row = mysql_fetch_assoc($get)) {
						$sub = $get_row['price'] * $value;
						echo $get_row['name'] . ' x ' . $value . '  &euro; ' . number_format($get_row['price'], 2) . ' = &euro; ' .  number_format($sub, 2) . '<a href="index.php?remove=' . $id .'">-</a> <a href="index.php?add=' . $id .'">+</a> <a href="index.php?delete=' . $id .'">delete</a><br>';
					}
				}
				$total += number_format($sub, 2);
			} 
		}
		if ($total == 0) {
			echo "Your cart is empty.";
		} 
		else {
			echo 'Total: &euro; ' . number_format($total, 2) . '';

?>
<?php
		}
	}
 
 
	function cart_qty(){ 
		///how many items are in cart
	}

?>

Hoi,

In cart.php doe je $_SESSION = array();
Dus elke keer dat cart.php word aangeroepen gooi je je sessie leeg en daarom komt er maar 1 product in je winkelwagen.

$_SESSION = array(); weghalen en het zou moeten werken.
Het is niet nodig om $_SESSION te declareren, deze is er al voor je ;)
heb ik gedaan maar hij werkt nog niet en krijg deze melding

Warning: Invalid argument supplied for foreach() in /home/unlimi1q/public_html/cart.php on line 68
Your cart is empty.
kijk maar http://www.unlimited-trading.nl/index.php
Goed, een WERKEND voorbeeld:

opslaan als cart.php
<?php

function addToCart($id, $amount = 1)
{
if(isset($_SESSION['cart'][$id]))
$_SESSION['cart'][$id] += $amount;
else
$_SESSION['cart'][$id] = $amount;

header('Location: cart.php');
}

function removeFromCart($id)
{
unset($_SESSION['cart'][$id]);
header('Location: cart.php');
}

session_start();

mysql_connect('localhost', 'root', '');
mysql_select_db('test');

if(!isset($_SESSION['cart']))
$_SESSION['cart'] = array();

if(isset($_GET['add']))
addToCart($_GET['add']);

if(isset($_GET['remove']))
removeFromCart($_GET['remove']);

/*
echo '<h3>inhoud van $_SESSION[\'cart\']:</h3>';
echo '<pre>';
print_r($_SESSION['cart']);
echo '</pre>';
*/

echo '<h2>Mijn webshop:</h2>';
$query = 'SELECT id, name, description, price FROM Tblproducts ORDER BY id ASC';
$result = mysql_query($query);

while($row = mysql_fetch_assoc($result))
{
echo $row['name'] . ' ' . number_format($row['price'], 2, ',', '.') . ' euro <a href="cart.php?add=' . $row['id'] . '">toevoegen</a><br>';
}

// laat de winkelwagen zien
if(count($_SESSION['cart']))
{
$query = 'SELECT id, name, description, price FROM Tblproducts WHERE id IN('.implode(',', array_keys($_SESSION['cart'])).') ORDER BY id ASC';
$result = mysql_query($query);
$total = 0;

echo '<h2>in winkelwagen:</h2>';
while($row = mysql_fetch_assoc($result))
{
$totalprice = $_SESSION['cart'][$row['id']] * $row['price'];
echo $_SESSION['cart'][$row['id']] . ' stuks ' . $row['name'] . ' ' . number_format($totalprice, 2, ',', '.') . ' <a href="cart.php?remove=' . $row['id'] . '">verwijderen</a><br>';
$total += $totalprice;
}
echo '<strong>totaalbedrag: '.number_format($total, 2, ',', '.').'</strong>';
}
?>
dit scriptje werk perfect dankje frank

Reageren