码迷,mamicode.com
首页 > Web开发 > 详细

第七十四天休息 PHP登入控制,主界面显示,添加购物车和下单

时间:2016-05-23 00:42:21      阅读:333      评论:0      收藏:0      [点我收藏+]

标签:

登陆界面 index.php

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>优先显示界面</title>
<link rel="shortcut icon" type="image/x-icon" href="my-img/title.ico"/>

<form action="intest.php" method="post">
<div>用户名:<input type="text" name="uid" /></div>
<div>密码:<input type="text" name="word" /></div>
<div><input type="submit" value="提交" /></div>
</form>

主界面(水果店)index-fruit.php

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>优先显示界面</title>
<link rel="shortcut icon" type="image/x-icon" href="my-img/title.ico"/>

<h1 align="center">水果店</h1>
<table align="center" width="1000px" border=‘1px‘>
    <tr align="center">
        <td>水果名称</td>
        <td>水果价格</td>
        <td>水果产地</td>
        <td>水果库存</td>
        <td>*</td>
    </tr>
<?php
session_start();
if(!isset($_SESSION[‘uid‘]))
{
    header(‘location:index.php‘);
}
include "class/uniondatabase-class.php";
$db=new unionDatabase();
$sql="select * from fruit";
$result=$db->query($sql);

foreach($result as $i)
{
    echo"<tr align=‘center‘>
         <td>$i[1]</td>
         <td>$i[2]</td>
         <td>$i[3]</td>
         <td>$i[4]</td>
         <td>
             <a href=‘intest-shoppingcart.php?code={$i[0]}‘>
                 加入购物车
             </a>
         </td>
        </tr>";
}
?>
</table><br>
<div>
    <a href="index-shoppingcart.php" target="_blank">
        查看购物车
    </a>
</div>

<style type="text/css">
table
{
    border-color:#0F0;
}
a
{
    text-decoration:none;
    color:#00F;
}
a:hover
{
    color:#F00;
}
div
{
    position:absolute;
    left:75%;
    font-weight:bold;
    font-size:16px;
}
</style>

购物车界面 index-shoppingcart.php

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>购物车</title>

<h1 align="center">购物车明细</h1>
<table width="1000px" border="1px" align="center" bordercolor="#00FF00">
    <tr align=‘center‘>
        <td>水果名称</td>
        <td>水果价格</td>
        <td>挑选数量</td>
        <td>*</td>
    </tr>
<?php
session_start();
if(!isset($_SESSION[‘uid‘]))
{
    header(‘location:index.php‘);
}
else if(isset($_SESSION[‘FruitName‘]))
    {    
    include "class/uniondatabase-class.php";
    $db=new unionDatabase();
    $attr=$_SESSION[‘FruitName‘];
    foreach($attr as $i)
    {
        $sql="select * from fruit where Ids=‘{$i[0]}‘";
        $result=$db->query($sql);
        echo "<tr align=‘center‘>
                 <td>{$result[0][1]}</td>
                 <td>{$result[0][2]}</td>
                 <td>{$i[1]}</td>
                 <td>
                 <a href=‘intest-deletefruit.php?code=$i[0]‘>
                     删除数量
                 </a>
                 </td>
             </tr>";    
    }
}
?>
</table><br />
<div><a  href=‘intest-upfruit.php‘>提交订单</a></div>


<style type="text/css">
table
{
    border-color:#0F0;
}
a
{
    text-decoration:none;
    color:#00F;
}
a:hover
{
    color:#F00;
}
div
{
    position:absolute;
    left:80%;
    font-weight:bold;
    font-size:16px;
}
</style>

登入处理界面

<?php
session_start();
include "class/uniondatabase-class.php";
$db=new unionDatabase();

$uid=$_POST[‘uid‘];
$word=$_POST[‘word‘];

$sql="select count(*) from user where Uid=‘$uid‘ and PassWord=‘$word‘";
$result=$db->queryStr($sql);

if($result==1)
{
    $_SESSION[‘uid‘]=$uid;
    header("location:index-fruit.php");
}

else
{
    header(‘location:index.php‘);
}

商品加入购物车处理界面 intest-shoppingcart.php

<?php
session_start();
$code=$_GET[‘code‘];

//第一次点击
if(!isset($_SESSION[‘FruitName‘]))
{
    $attr=array(array($code,1));   //存放商品和个数的数组
    $_SESSION[‘FruitName‘]=$attr;
}

//第多次点击
else
{
    $attr=$_SESSION[‘FruitName‘];
    
    if(FruitIsset($code))
    {
        foreach($attr as $k=>$i)
        {
            if($i[0]==$code)
            {
                $attr[$k][1]=$i[1]+1;
                
            }
        }
        $_SESSION[‘FruitName‘]=$attr;
    }
    else
    {
        $arr=array($code,1);
        array_push($attr,$arr);
        $_SESSION[‘FruitName‘]=$attr;
    }
}

header(‘location:index-fruit.php‘);

//判断购物车是否存在某种水果的方法
function FruitIsset($code)
{
    $attr=$_SESSION[‘FruitName‘];
    $t=false;
    foreach($attr as $i)
    {
        $t=$t || in_array($code,$i);
    }
    return $t;
}

购物车商品删除处理界面 intest-deletefruit.php

<?php
session_start();
$code=$_GET[‘code‘];
$attr=$_SESSION[‘FruitName‘];
foreach($attr as $k=>$i)
{
    if($i[0]==$code)
    {
        $attr[$k][1]=$i[1]-1;
        if($attr[$k][1]==0)
        {
            array_splice($attr,$k,1);
        }
    }
    $_SESSION[‘FruitName‘]=$attr;
}
header(‘location:index-shoppingcart.php‘);

下单处理界面  intest-upfruit.php

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
session_start();

$Fruit=$_SESSION[‘FruitName‘];
$Uid=$_SESSION[‘uid‘];

include "class/uniondatabase-class.php";
$db=new unionDatabase();

$sql1="select UserName from user where Uid=‘$Uid‘";
$UserName=$db->queryStr($sql1);           //获取用户名
$Time=date("Y-m-d H:i:s",time());         //获取下单时间

echo $Uid;
echo $UserName;
echo $Time;

$sql2="insert into order values(‘‘,‘$Uid‘,‘$UserName‘,‘$Time‘)";
$db->query($sql2);

foreach($Fruit as $i)
{
    $sql3="insert into orderdetails values(‘‘,‘$Uid‘,‘$i[0]‘,‘$i[1]‘)";
    $db->query($sql3);
}

 

第七十四天休息 PHP登入控制,主界面显示,添加购物车和下单

标签:

原文地址:http://www.cnblogs.com/lovling/p/5518298.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!