phpカート実現コード
5701 ワード
ShopCard.php
<?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;
}
}
productList.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src='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='show.php'> </a></tr>
</table>
</body>
</html>
index.php
<?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);
show.php
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></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>