资源描述
PHP基础教程(118) 通过session实现购物车功能(1)
时间:2010-01-23来源:PHP教程网 作者:PHP教程网 点击: 437次
conn.php: 数据库连接页 ?php $id=mysql_connect('localhost','root','mysql'); //数据库的帐号密码请修改mysql_select_db(db_database05,$id);mysql_query(set names gb2312);? index.php: htmlheadmeta http-equiv=Content-Type content=text/html; charse
conn.php: 数据库连接页
<?php
$id=mysql_connect('localhost','root','mysql'); //数据库的帐号密码请修改
mysql_select_db("db_database05",$id);
mysql_query("set names gb2312");
?>
index.php:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>通过session实现购物车功能</title>
<style type="text/css">
<!--
body {
margin-left: 0px;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
}
-->
</style>
</head>
<body>
<table width="392" height="249" border="0" cellpadding="0" cellspacing="0"">
<form name="form1" method="post" action="index_ok.php">
<tr>
<td height="100" colspan="3"> </td>
</tr>
<tr>
<td width="157" height="51"> </td>
<td width="138"><input name="user" type="text" id="user3" size="15"></td>
<td width="97">
<input name="imageField" type="image" src="images/139s.jpg" width="50" height="25" border="0">
</td>
</tr>
<tr>
<td height="30"> </td>
<td><input name="pass" type="password" id="pass2" size="15"></td>
<td><input name="imageField2" type="image" src="images/139ss.jpg" width="50" height="25" border="0">
</td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
</tr>
</form>
</table>
</body>
</html>
index_ok.php:
<?php session_start();
$user=$_POST[user];
$pass=$_POST[pass];
if($user!="" && $pass!=""){ //判断该用户和密码是否正确
echo "登录成功!";
//session_register(user); //注册新的session变量
//session_register(pass);
$_SESSION['user'] = $user;
$_SESSION['pass'] = $pass;
// session_register("producelist"); //发给用户一个购物车
$_SESSION['producelist'];
$producelist="";
//session_register("quatity"); //初始化购物车内没有商品
$_SESSION['quatity'];
$quatity="";
echo "<meta http-equiv=\"refresh\" content=\"3;url=shop.php\">3秒钟转入主页,请稍等......";
}else{
echo "<script>alert('登录失败!');history.back();</script>"; ;
}
?>
PHP基础教程(119) 通过session实现购物车功能(2)
时间:2010-01-23来源:PHP教程网 作者:PHP教程网 点击: 1242次
shop.php: ?php session_start(); include(conn.php);if($_SESSION['user']== $_SESSION['pass']==){echo 对不起,您没有正确登录本站!!;echo meta http-equiv=\refresh\ content=\3 url=index.php\3秒后转入登录页面......;}else{?htmlheadmeta http-equiv=C
shop.php:
<?php
session_start();
include("conn.php");
if($_SESSION['user']=="" && $_SESSION['pass']==""){
echo "对不起,您没有正确登录本站!!";
echo "<meta http-equiv=\"refresh\" content=\"3 url=index.php\">3秒后转入登录页面......";
}
else
{
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>通过session实现购物车功能</title>
<style type="text/css">
<!--
.style1 {font-size: 13px}
.style2 {font-size: 12px}
body {
margin-left: 0px;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
}
a:link {
text-decoration: none;
}
a:visited {
text-decoration: none;
}
a:hover {
text-decoration: none;
}
a:active {
text-decoration: none;
}
-->
</style>
</head>
<body>
<table width="480" border="1" cellpadding="0" cellspacing="0">
<form name="form1" method="post" action="">
<tr align="center">
<td height="25" colspan="4"><span class="style1">商品展示</span></td>
</tr>
<?php
$query="select * from tb_shop order by id desc ";
$result=mysql_query($query);
if($result==true){
while($myrow=mysql_fetch_array($result)){;
?>
<tr>
<td width="120" rowspan="2" align="right">
<img src="<?php echo $myrow[image];?>" width="115" height="100"></td>
<td width="180" height="50" align="center" valign="middle" class="style2">
<span class="style2">商品编号:</span>
<input name="number" type="text" id="number" value="<?php echo $myrow[number];?>" size="12">
</td>
<td colspan="2" align="center" valign="middle" class="style2">商品名称:
<input name="name" type="text" id="name" value="<?php echo $myrow[name];?>" size="12"></td>
</tr>
<tr>
<td height="50" align="center" valign="middle" class="style2">
<span class="style2">商品价格:</span>
<input name="counts" type="text" id="counts" value="<?php echo $myrow[price];?>" size="10">
</td>
<td colspan="2"> </td>
</tr>
<tr>
<td> </td>
<td height="22" colspan="3" align="center" class="style2">
<a href="shop_ok.php?lmbs=<?php echo $myrow[id];?>">添加到购物车</a></td>
</tr>
</form>
<?php }}?>
</table>
</body>
</html>
<?php }
?>
shop_ok.php:
<?php
session_start();
if($_SESSION[user]=="" && $_SESSION[pass]=="")
{
echo "<script>alert('请先登录后购物!');history.back();</script>";
exit;
}
$lmbs=strval($_GET['lmbs']); //获取变量的字符串值
$array=explode("@",$_SESSION[producelist]); //切开后的字符串返回到数组变量中
for($i=0;$i<count($array)-1;$i++) //为什么减1?
//Array ( [0] => 1 [1] => 2 [2] => 3 [3] => ) 原因在这
{
if($array[$i]==$lmbs)
{
echo "<script>alert('该商品已经在您的购物车中!');history.back();</script>";
exit;
}
}
$_SESSION[producelist]=$_SESSION[producelist].$_GET[lmbs]."@";
$_SESSION[quatity]=$_SESSION[quatity]."1@";
header("location:gwc.php");
?>
PHP基础教程(120) 通过session实现购物车功能(3)
时间:2010-01-23来源:PHP教程网 作者:PHP教程网 点击: 394次
gwc.php ?php session_start(); include(conn.php); if($_SESSION[user]==){ echo scriptalert('请先登录,后购物!');history.back();/script;exit; } ?style type=text/css!--.style1 {font-size: 13px}.style2 {font-size: 12px}--/styletitle通过session
gwc.php
<?php
session_start();
include("conn.php");
if($_SESSION[user]==""){
echo "<script>alert('请先登录,后购物!');history.back();</script>";
exit; }
?>
<style type="text/css">
<!--
.style1 {font-size: 13px}
.style2 {font-size: 12px}
-->
</style>
<title>通过session实现购物车功能</title>
<table width="480" border="0" align="center" cellpadding="0" cellspacing="0">
<form name="form1" method="post" action="gwc.php">
<tr>
<td height="32">
欢迎: <?php echo $_SESSION[user];?></td>
</tr>
<tr>
<td bgcolor="#555555">
<table width="480" border="0" align="center" cellpadding="0" cellspacing="1">
<?php
//session_register("total");
$_session['total'];
if($_GET[qk]=="yes"){
$_SESSION[producelist]="";
$_SESSION[quatity]=""; }
$arraygwc=explode("@",$_SESSION[producelist]);
$s=0;
for($i=0;$i<count($arraygwc);$i++){
$s+=intval($arraygwc[$i]);//获取变量的整数值
}
if($s==0){
echo "<tr>";
echo" <td height='25' colspan='6' bgcolor='#FFFFFF' align='center'>您的购物车为空!</td>";
echo"</tr>";
}else{
?>
<tr>
<td width="120" align="center" bgcolor="#FFFFFF"><span class="style1">商品编号</span></td>
<td width="120" height="25" bgcolor="#FFFFFF" class="style1">
<div align="center">商品名称</div></td>
<td width="60" bgcolor="#FFFFFF" class="style1"><div align="center">数量</div></td>
<td width="60" bgcolor="#FFFFFF" class="style1"><div align="center">市场价</div></td>
<td width="60" bgcolor="#FFFFFF" class="style1"><div align="center">小计</div></td>
<td width="60" bgcolor="#FFFFFF" class="style1"><div align="center">操作</div></td>
</tr>
<?php
$total=0;
$array=explode("@",$_SESSION[producelist]);
$arrayquatity=explode("@",$_SESSION[quatity]);
while(list($name,$value)=each($_POST)){
//list把数组中的值赋给一些变量 ,each返回数组中当前的键/值对并将数组指针向前移动一步
for($i=0;$i<count($array)-1;$i++){
if(($array[$i])==$name){
$arrayquatity[$i]=$value; }
}
}
$_SESSION[quatity]=implode("@",$arrayquatity);
for($i=0;$i<count($array)-1;$i++){
$lmbs=$array[$i];
$num=$arrayquatity[$i];
if($lmbs!=""){
$sql=mysql_query("select * from tb_shop where id='".$lmbs."'",$id);
$info=mysql_fetch_array($sql);
$total1=$num*$info[price];
$total+=$total1;
$_SESSION["total"]=$total;
?>
<tr>
<td height="25" bgcolor="#FFFFFF">
<div align="center" class="style2"><?php echo $info[number];?></div></td>
<td height="25" bgcolor="#FFFFFF">
<div align="center" class="style2"><?php echo $info[name];?></div></td>
<td height="25" bgcolor="#FFFFFF"><div align="center" class="style2">
<input type="text" name="<?php echo $info[id];?>" size="2" class="inputcss" value=<?php echo $num;?>>
</div></td>
<td height="25" bgcolor="#FFFFFF">
<div align="center" class="style2"><?php echo $info[price];?>元</div></td>
<td height="25" bgcolor="#FFFFFF">
<div align="center" class="style2"><?php echo $info[price]*$num."元";?></div></td>
<td height="25" bgcolor="#FFFFFF">
<div align="center" class="style2"><a href="delete.php?id=<?php echo $info[id]?>">移除</a>
</div></td>
</tr>
<?php
}
}
?>
<tr>
<td height="25" colspan="7" bgcolor="#FFFFFF"><div align="right">
<table width="500" height="25" border="0" align="center" cellpadding="0" cellspacing="0">
<tr class="style1">
<td width="125"><div align="center">
<input type="submit" value="更改商品数量" class="buttoncss">
</div></td>
<td width="125"> </td>
<td width="125"><div align="center"><a href="gwc.php?qk=yes">清空购物车</a></div></td>
<td width="125"><div align="left">总计:<?php echo $total;?></div></td>
</tr>
</table>
</div></td>
</tr>
<?php
}
?>
</table></td>
</tr>
</form>
</table>
delete.php
<?php
$id=$_GET[id];
session_start();
$arraysp=explode("@",$_SESSION[producelist]);
$arraysl=explode("@",$_SESSION[quatity]);
for($i=0;$i<count($arraysp);$i++)
{
if($arraysp[$i]==$id)
{
$arraysp[$i]="";
$arraysl[$i]="";
}
}
$_SESSION[producelist]=implode("@",$arraysp);
$_SESSION[quatity]=implode("@",$arraysl);
header("location:gwc.php");
?>
展开阅读全文