1、cart.php 购物车
<?php
session_start();
$conn=mysql_connect()("localhost","root","admin");
mysql_select_db("songyu");
//检查数组元素出现次数
function check_count($array,$element)
{
$times=0;
for($i=0;$i<count($array);$i++)
{
if($element==$array[$i])
{
$times++;
}
}
return $times;
}
if(isset()($_GET["p_id"]))
{
$p_id=$_GET["p_id"];
}
$total_price=0;
array_push($_SESSION["cart"], $p_id);
$cart=$_SESSION["cart"];
echo "your cart:<br/>";
$new_array=array_count_values($cart);
foreach ($new_array as $key => $value)
{
$sql="select * from product where id='".$key."'";
$result=mysql_query()($sql);
$out=mysql_fetch_array($result);
echo $out[name]."---个数:".$value."--".($out[price]*$value)."<br/>";
$total_price=$total_price+($out[price]*$value);
}
echo "<br/><br/>";
echo "-----------总价--------------<br/>";
echo $total_price;
?>
<a href="/blog_article/product.html">回s</a>
2、login.php 登录页
if(isset($_SESSION["user"]))
{
unset($_SESSION["user"]);
}
if(isset($_SESSION["cart"]))
{
unset($_SESSION["cart"]);
}
?>
<form action="/blog_article/product_index.html" method="post">
username:<input type="text" name="username"/><br/><br/>
password:<input type="password" name="password"/><br/><br/>
<input type="submit" name="submit"/>
</form>
3、product.php 产品页
session_start();
$conn=mysql_connect("localhost","root","admin");
mysql_select_db("songyu");
$sql_product="select * from product";
$res=mysql_query($sql_product);
if(!isset($_SESSION["cart"]))
{
$_SESSION["cart"]=array();
}
while($out2=mysql_fetch_array($res))
{
echo "<a href='/blog_article/cart/p_id/.html".$out2[id]."'>".$out2[name]."</a><br/>";
echo $out2[price]."<br/>";
echo "<hr>";
}
?>
4、product_index.php 产品索引页
$conn=mysql_connect("localhost","root","admin");
mysql_select_db("songyu");
$sql="select * from user where username='".$_POST["username"]."' and password='".$_POST["password"]."'";
$result=mysql_query($sql);
$out=mysql_fetch_array($result); //www.
if(!$out)
{
echo "wrong!";
}
else
{
$_SESSION["user"]=$out[id];
echo "<script>window.location.href='product.php'</script>";
}
?>
1、ShopCar.php 购物车类
class Shopcar
{
//商品列表
public $productList=array();
/**
*
* @param unknown_type $product 传进来的商品
* @return true 购物车里面没有该商品
*/
public function checkProduct($product)
{
for($i=0;$i<count($this->productList);$i++ )
{
if($this->productList[$i]['name']==$product['name'])
return $i;
}
return -1;
}
//添加到购物车
public function add($product)
{
$i=$this->checkProduct($product);
if($i==-1)
array_push($this->productList,$product);
else
$this->productList[$i]['num']+=$product['num'];
}
//删除
public function delete($product)
{
$i=$this->checkProduct($product);
if($i!=-1)
array_splice($this->productList,$i,1);
}
//返回所有的商品的信息
public function show()
{
return $this->productList;
}
}
2、productList.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>php购物车-商品列表页面-www.</title>
<script type="text/javascript" src='/blog_article/jquery.min.js'></script>
<script type="text/javascript">
function buy(i)
{
var num=$(':input[name=num]')[i].value;
var name=$('[name=name]')[i].innerHTML;
var price=$('[name=price]')[i].innerHTML;
alert(num+name+price);
$.ajax({
type:'post', //传送的方式,get/post
url:'index.php', //发送数据的地址
cache:'false',
data:'num='+num+"&name="+name+"&price="+price,
success:function(data)
{
alert(data);
}
})
}
</script>
</head>
<body>
<table>
<tr><td>商品编号</td><td>商品名称</td><td>价格</td><td>数量</td><td>购买</td></tr>
<tr><td>0</td><td><label name='name' >商品1</label></td><td><label name='price'>1</label>
</td><td><input name='num' type='text' value='1' /></td><td><a onclick='buy(0)'><u><font color='blue'>购买</font></u></a></td></tr>
<tr><td>1</td><td><label name='name' >商品2</label></td><td><label name='price'>2</label>
</td><td><input name='num' type='text' value='1' /></td><td><a onclick='buy(1)'>购买</a></td></tr>
<tr><td>2</td><td><label name='name' >商品3</label></td><td><label name='price'>1</label>
</td><td><input name='num' type='text' value='1' /></td><td><a onclick='buy(2)'>购买</a></td></tr>
<tr><td>3</td><td><label name='name' >商品4</label></td><td><label name='price'>1</label>
</td><td><input name='num' type='text' value='1' /></td><td><a onclick='buy(3)'>购买</a></td></tr>
<tr><a href='/blog_article/show.html'>查看购物车</a></tr>
</table>
</body>
</html>
3、index.php
require 'Shopcar.class.php';
session_start();
$name=$_POST['name'];
$num=$_POST['num'];
$price=$_POST['price'];
$product=array('name'=>$name,'num'=>$num,'price'=>$price);
print_r($product);
if(isset()($_SESSION['shopcar']))
$shopcar=unserialize($_SESSION['shopcar']);
else
$shopcar=new Shopcar();
$shopcar->add($product);
$_SESSION['shopcar']=serialize($shopcar);
?>
4、show.php
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>商品信息展示页_www.</title>
</head>
<body>
<table>
<tr><td>商品编号</td><td>商品名称</td><td>价格</td><td>数量</td></tr>
<?php
require 'Shopcar.class.php';
session_start();
$shopcar=unserialize($_SESSION['shopcar']);
print_r($shopcar);
$productList=$shopcar->productList;
foreach ($productList as $product){
?>
<tr><td>1</td><td><label ><?php echo $product['name']?></label></td><td><label name='price'><?php echo $product['price']?></label>
</td><td><input name='num' type='text' value='<?php echo $product['num']?>' /></td></tr>
<?php }?>
</table>
</body>
</html>
本文内容共分为二个部分,一是html表单,二是php处理部分。
1、表单部分:
<input name="suliang[<?php echo $c_rs['sp_id'];?>]" type="text" id="suliang[<?php echo $c_rs['sp_id'];?>]" value="<?php echo $c_rs['suliang'];?>"/>
<input type="submit" name="button" id="button" value="更新购物车" />
</form>
2、php处理代码
<?php
/**
* php购物车 处理代码
* Edit www.
*/
require 'config.inc.php'; //配置文件
require 'checklogin.php'; //登录检测
$username = $_SESSION['username'];
$action = $_GET['action'];
switch ($action) {
case "edit_num":
$arr = $arr = $_POST['suliang'];
foreach($arr as $key=>$value){
$sqlgx = "update `cartemp` set suliang='$value' where username='".$username."' and flag=0 and sp_id='".$key."'";
mysql_query()($sqlgx, $conn);
echo "<script>location.href='/blog_article/shopcat.html'</script>";
}
break;
case "null":
$null_sql = "delete from `cartemp` where username='$username' and flag=0 ";
mysql_query($null_sql, $conn);
echo "<script>location.href='/blog_article/shopcat.html'</script>";
break;
case "del":
$id = $_GET['id'];
$del_sql = "delete from `cartemp` where id=$id";
mysql_query($del_sql, $conn);
echo "<script>location.href='/blog_article/shopcat.html'</script>";
break;
}
?>