查看: 1073|回复: 3
|
shopping cart 的 error ...不能add multiple 的record's row
[复制链接]
|
|
Main Page:index.php
<?php
require_once("functions.inc.php");
include("dbfinfo.inc.php");
mysql_connect("localhost",$username,$password);
@mysql_select_db($database) or die (" Unable to select database");
// Start the session
session_start();
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>PHP Shopping Cart Demo · Bookshop</title>
<link rel="stylesheet" href="css/styles.css" />
</head>
<body>
<div id="shoppingcart">
<h1>Your Shopping Cart</h1>
<?php
echo writeShoppingCart();
?>
</div>
<div id="booklist">
<h1>Ticket Reservation</h1>
<?php
$sql = 'SELECT * FROM booking ORDER BY id';
$result=mysql_query($sql);
$num_rows=mysql_num_rows($result);
$output[] = '<ul>';
while ($row = mysql_fetch_row($result)) {
$output[] = '<li>"'.$row[0].'": '.$row[1].'<br />'.$row[2].'<br />'.$row[3].'<br />'.$row[4].'<br />£'.$row[5].'<br />
<a href="cart.php?action=add&id=1">Add to cart</a></li>';
}
$output[] = '</ul>';
echo join('',$output);
?>
</div>
</body>
</html>
Calculation :functions.inc.php
<?php
function writeShoppingCart() {
$cart = $_SESSION['cart'];
if (!$cart) {
return '<p>You have no items in your shopping cart</p>';
} else {
// Parse the cart session variable
$items = explode(',',$cart);
$s = (count($items) > 1) ? 's':'';
return '<p>You have <a href="cart.php">'.count($items).' item'.$s.' in your shopping cart</a></p>';
}
}
function showCart() {
$cart = $_SESSION['cart'];
if ($cart) {
$items = explode(',',$cart);
$contents = array();
foreach ($items as $item) {
$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
}
$output[] = '<form action="cart.php?action=update" method="post" id="cart">';
$output[] = '<table>';
foreach ($contents as $id=>$qty) {
$sql = 'SELECT * FROM booking WHERE id = '.$id;
$result=mysql_query($sql);
$num_rows=mysql_num_rows($result);
$row = mysql_fetch_row($result);
extract($row);
$output[] = '<tr>';
$output[] = '<td><a href="cart.php?action=delete&id='.$id.'" class="r">Remove</a></td>';
$output[] = '<td>'.$row[0].'</td>';
$output[] = '<td>'.$row[1].'</td>';
$output[] = '<td>'.$row[2].'</td>';
$output[] = '<td>£'.$row[5].'</td>';
$output[] = '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
$output[] = '<td>£'.($row[5] * $qty).'</td>';
$total += $row[5] * $qty;
$output[] = '</tr>';
}
$output[] = '</table>';
$output[] = '<p>Grand total: <strong>£'.$total.'</strong></p>';
$output[] = '<div><button type="submit">Update cart</button></div>';
$output[] = '</form>';
} else {
$output[] = '<p>You shopping cart is empty.</p>';
}
return join('',$output);
}
?>
Add,delete and update cart:cart.php
<?php
require_once("functions.inc.php");
include("dbfinfo.inc.php");
mysql_connect("localhost",$username,$password);
@mysql_select_db($database) or die (" Unable to select database");
session_start();
// Process actions
$cart = $_SESSION['cart'];
$action = isset($_GET["action"])?$_GET["action"]:"";
switch ($action) {
case 'add':
if ($cart) {
$cart .= ','.$_GET['id'];
} else {
$cart = $_GET['id'];
}
break;
case 'delete':
if ($cart) {
$items = explode(',',$cart);
$newcart = '';
foreach ($items as $item) {
if ($_GET['id'] != $item) {
if ($newcart != '') {
$newcart .= ','.$item;
} else {
$newcart = $item;
}
}
}
$cart = $newcart;
}
break;
case 'update':
if ($cart) {
$newcart = '';
foreach ($_POST as $key=>$value) {
if (stristr($key,'qty')) {
$id = str_replace('qty','',$key);
$items = ($newcart != '') ? explode(',',$newcart) : explode(',',$cart);
$newcart = '';
foreach ($items as $item) {
if ($id != $item) {
if ($newcart != '') {
$newcart .= ','.$item;
} else {
$newcart = $item;
}
}
}
for ($i=1;$i<=$value;$i++) {
if ($newcart != '') {
$newcart .= ','.$id;
} else {
$newcart = $id;
}
}
}
}
}
$cart = $newcart;
break;
}
$_SESSION['cart'] = $cart;
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>PHP Shopping Cart Demo · Cart</title>
<link rel="stylesheet" href="css/styles.css" />
</head>
<body>
<div id="shoppingcart">
<h1>Your Shopping Cart</h1>
<?php
echo writeShoppingCart();
?>
</div>
<div id="contents">
<h1>Please check quantities...</h1>
<?php
echo showCart();
?>
<p><a href="index.php">Back to bookshop...</a></p>
</div>
</body>
</html>
请问是什么问题???以上的code 是在Internet 找来的.谢谢帮忙.... |
|
|
|
|
|
|
|
发表于 18-9-2006 11:05 PM
|
显示全部楼层
你的cart我看过(在internet的)
是没有问题。
因为我都有一个,我也test过。
还有你的是什么error |
|
|
|
|
|
|
|
楼主 |
发表于 21-9-2006 09:47 PM
|
显示全部楼层
回复 #2 红发 的帖子
shopping cart 可以add multiple row 但是我add 来 add 去都是同样的 row??
Add 不到multiple row into cart ......
还有undefined variable total 的问题。。。。
$total += $row[5] * $qty;
对不起,迟了回复。谢谢发哥 |
|
|
|
|
|
|
|
发表于 21-9-2006 10:56 PM
|
显示全部楼层
原帖由 小今 于 21-9-2006 09:47 PM 发表
shopping cart 可以add multiple row 但是我add 来 add 去都是同样的 row??
Add 不到multiple row into cart ......
还有undefined variable total 的问题。。。。
$total += $row * $qty;
对不起, ...
如何说同样的row呢??
同样的东西在同一个row
还是不一样的东西在同一个row??
+=
给你一个exmaple
$x += $y
将$X+$Y的value给$x,也就是说是这样啦
$x = $x+$y
$total += $row[5] * $qty;
($row[5] * $qty) = $y
$total = $total + $y
这样就是了 |
|
|
|
|
|
|
| |
本周最热论坛帖子
|