Winkelwagen voegt maar 1 product toe
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
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<!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 . ' € ' . number_format($get_row['price'], 2) . ' = € ' . 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: € ' . number_format($total, 2) . '';
?>
<?php
}
}
function cart_qty(){
///how many items are in cart
}
?>
<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 . ' € ' . number_format($get_row['price'], 2) . ' = € ' . 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: € ' . number_format($total, 2) . '';
?>
<?php
}
}
function cart_qty(){
///how many items are in cart
}
?>
Edit:
Code tussen UBB-code-tags geplaatst. Gelieve dit voortaan zelf te doen in het vervolg.
Alvast bedankt.
Alvast bedankt.
Gewijzigd op 15/08/2013 22:06:06 door - Ariën -
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 ;)
Gewijzigd op 15/08/2013 20:22:13 door Erik van Beek
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
opslaan als cart.php
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?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>';
}
?>
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>';
}
?>
Gewijzigd op 15/08/2013 22:43:34 door Frank Nietbelangrijk
dit scriptje werk perfect dankje frank