只作为了解购物车入门,可以参考ci框架的的购物车类。也是引用了其他一位博主的文章,链接忘了。。
sql文件 库名shop
/*
Navicat MySQL Data Transfer
Source Server : localhost_3306
Source Server Version : 50540
Source Host : localhost:3306
Source Database : shop
Target Server Type : MYSQL
Target Server Version : 50540
File Encoding : 65001
Date: 2015-06-11 14:17:36
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for `product`
-- ----------------------------
DROP TABLE IF EXISTS `product`;
CREATE TABLE `product` (
`pid` int(11) NOT NULL AUTO_INCREMENT,
`pname` varchar(255) DEFAULT NULL,
`pimg` varchar(255) DEFAULT NULL,
`price` varchar(255) DEFAULT NULL,
PRIMARY KEY (`pid`)
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of product
-- ----------------------------
INSERT INTO product VALUES ('1', 'ipad', 'http://a.com/error.png', '12');
INSERT INTO product VALUES ('2', 'air', 'http://a.com/error.png', '13');
INSERT INTO product VALUES ('3', 'pro', 'http://a.com/error.png', '14');
INSERT INTO product VALUES ('4', 'shuffle', 'http://a.com/error.png', '15');
我自己配置的vhost a.com。可以直接在主目录下添加一个图片即可。也可以直接无视图片。
一共是coon.php,index.php,buy.php,delete.php,car.php
*coon
<?php
$conn=mysql_connect("localhost","root","root");
mysql_select_db("shop",$conn);
mysql_query("set names utf8");
?>
*index.php
<?php
include ("coon.php");
error_reporting(0);
$sql="select * from product";//查询所有商品
$rs=mysql_query($sql,$conn);//执行sql语句,得到一个结果集
while($row=mysql_fetch_array($rs))//遍历结果集
{
?>
<table width="343" height="152" border="1">
<tr>
<td width="124"rowspan="3"><img src="<?php echo $row["pimg"]?>" width="123" height="121" border="0"/></td>
<td width="203"height="35">货物名称:<?php echo$row["pname"]?></td>
</tr>
<tr>
<tdheight="28">货物价格:<?php echo $row["price"]?></td>
</tr>
<tr>
<td height="27"align="center"><a href="buy.php?id=<?php echo $row["pid"]?>&pname=<?php
echo $row["pname"]?>">购买</a></td>
</tr> </table>
<?php
}
?>
*buy.php
<?php
session_start();
ob_start();
$pid=$_GET['id'];
$name=$_GET['pname'];
$arr=$_SESSION['mycar'];
if(is_array($arr)){
if(array_key_exists($pid,$arr)){
// 买过此商品,数量加1,返回给购物车数组
$goods=$arr[$pid];
$goods['num']+=1;
$arr[$pid]=$goods;
}else{
$arr[$pid]=array('pid'=>$pid,'name'=>$name,'num'=>'1');
}
}else{
$arr[$pid]=array('pid'=>$pid,'name'=>$name,'num'=>'1');
}
$_SESSION['mycar']=$arr;
// session_destroy();
ob_clean();
header('location:car.php');
*delete.php
<?php
session_start();
ob_start();
$pid=$_GET['id'];
$arr=$_SESSION['mycar'];
foreach ($arr as $key => $value) {
if($key==$pid){
unset($arr[$key]);
}
}
$_SESSION['mycar']=$arr;
ob_clean();
header('location:car.php');
*car.php
<?php
session_start();
$arr=$_SESSION['mycar'];
if(count($arr)>0){
?>
<table width="600" height="37"border="1">
<tr>
<tdwidth="96">商品ID</td>
<tdwidth="158">商品名称</td>
<tdwidth="154">商品数量</td>
<tdwidth="177">删除</td>
</tr>
<?php
foreach($arr as $a)//遍历这个二维数组
{
?>
<tr>
<td width="96"><?php echo $a["pid"]?></td>
<td width="158"><?php echo $a["name"]?></td>
<td width="154"><?php echo $a["num"]?></td>
<td width="177"><a href="delete.php?id=<?php echo $a['pid']?>">删除</a></td>
</tr>
<?php
}
?>
</table>
</form>
<a href="index.php">返回继续购物</a>
<?php
}else{
echo 'empty car '.'<a href="index.php"> 去购物</a>';
}
?>