PHP小白之路3--PHP之购物车模块设计

来源:互联网 发布:wine for mac 编辑:程序博客网 时间:2024/05/29 03:24

此项目主要功能是显示数据库商品并带有基础查询功能,点击商品图片下方标题可以显示商品具体信息,可以立即购买或者添加到购物车保存在Cookie中,购物车模块有简单的价格统计,取消购物车某一件商品以及清空购物车的功能.

页面商品列表展示:


查询页面:


点击商品标题显示商品信息:


购物车展示:


清空购物车:


项目目录:


数据库:



代码如下:

goods.php:

<?phpinclude "mysqli.php";?><div style='width:800px;float:none'>    <h1>商品列表</h1><!--    显示当前系统时间-->    <h3><p id="demo"></p>        <script>            var myVar=setInterval(function(){myTimer()},1000);            function myTimer() {                var d = new Date();                document.getElementById("demo").innerHTML = d.toLocaleTimeString();            }        </script>    </h3>    <form method="get" action="">    <table style="100%">        <tr><td><select name="gid">                    <option value="0">请选择商品</option><?phpfunction show($fid,$i){    global $mysqli;    $sql = "select *from goodstype where fid=$fid";    $result = $mysqli->query($sql);    $str=" ";    $i++;    for($n=1;$n<$i;$n++) {        $str .= "---";    }    $id=$_GET["gid"];    ?>    <?php    while ($row = $result->fetch_assoc()) {        ?>        <option <?php if($id==$row['id']){echo "selected";}?> id="<?php echo $str.$row["classname"] ?>" value="<?php echo $row["id"] ?>">            <?php echo $str.$row["classname"] ?>        </option>        <?php        show($fid=$row["id"],$i);        ?>        <?php    }}show(0,0);    ?>     <input id="select" type="submit" value="查询"></select></td></tr>    </table></form></div><div style="float: none;width: 600px"><?php    $id=isset($_GET["gid"])?$id=$_GET["gid"]:0;    if(!empty($id)){        $sql="select *from goods where goodstypefid=$id or goodstypefstr like '%$id%' and checkinfo=1 and delstate=0";    }else{        $sql="select *from goods";    }    $result=$mysqli->query($sql);    ?><table>    <tr><?php  while($row=$result->fetch_assoc()){?><td>    <image width="200px" height="200px" src="<?php echo $row["picurl"]?>"></image>    <a title="查看商品详细信息" href="goodsshow.php?id=<?php echo $row["id"]?>"><?php echo $row["title"]?></a></td><?php  }      ?>    </tr></table></div>

goodsshow.php:

<?phpinclude "mysqli.php";?><script src="../2017829/web/jquery-1.11.0.js"></script><script>    //立即购买    function buynow(){        addshoppingcart("buy");    }    //添加到购物车    function addshoppingcart(a){            $.ajax({                url:"shoppingcart.php?a=addshoppingcart",                type:"post",                data:{'buynum':$("#buynum").val(),'id':$("#id").val()},                dataType:"html",                success:function (data) {                    if(a=="buy"){                        location.href="shoppingcart.php?a=buynow";                    }else{                        if(data){                            alert("添加购物车成功!");                        }                    }                }            })    }</script><?php$id=$_GET["id"];$sql="update goods set hits=hits+1 where id=".$id;$mysqli->query($sql);$sql="select *from goods where id=".$id;$result=$mysqli->query($sql);while($row=$result->fetch_assoc()){?><div>    <span><?php echo $row["title"] ?></span>    <hr>    <image src="<?php echo $row["picurl"] ?>" width="200"></image>    <br>    数量:-<input type="text" id="buynum" value="1">+    价格:    <del>市场价:<?php echo $row["marketprice"] ?></del>    出售价:<?php echo $row["salesprice"] ?>    <input type="hidden" id="id" value="<?php echo $row["id"] ?>">    <hr>    内容:<?php echo $row["content"] ?><br>    <a href="javascript:;" onclick="buynow()">立刻购买</a>  <a href="javascript:;" onclick="addshoppingcart()">加入购物车</a>    <?php    }    ?></div>

shoppingcart.php:

<style>    .shoppingcartempty {background: #00FF00}</style><?phpheader("Content-type:text/html;charset=utf-8");include "mysqli.php";$a=isset($_GET["a"])?$_GET["a"]:"";//添加购物车if($a=="addshoppingcart"){    $buynum=$_POST["buynum"];    $id=$_POST["id"];//    echo "<script>alert($buynum+$id)</script>";    if(!empty($_COOKIE["shoppingcart"]))        $shoppingcart=unserialize($_COOKIE["shoppingcart"]);    else        $shoppingcart=array();    if(isset($id) && isset($buynum)){        $id=intval($id);        $buynum=intval($buynum);        $shoppingcart[]=array($id,$buynum);    }    setcookie('shoppingcart',serialize($shoppingcart));    echo "true";    exit();}//取消购物车里的一件商品elseif($a=="delone"){    $key=$_GET["key"];    $shoppingcart=unserialize($_COOKIE["shoppingcart"]);    unset($shoppingcart[$key]);    if(empty($_COOKIE)){        setcookie($shoppingcart,"",time()-3600);    }else{        setcookie("shoppingcart",serialize($shoppingcart));    }    header("location:shoppingcart.php");    exit();}//清空购物车elseif($a=="empty"){    unset($_COOKIE["shoppingcart"]);    setcookie("shoppingcart","",time()-3600);    header("location:shoppingcart.php");    exit();}if(!empty($_COOKIE["shoppingcart"])){    ?><table width="100%" border="0" cellspacing="0" cellpadding="0">    <tr>        <td width="20%" >商品ID</td>        <td width="35%" height="30">商品名称</td>        <td width="25%">购买数量</td>        <td  width="15%">价格</td>        <td width="5%">操作</td>    </tr>    <tr>        <td height="10" colspan="4"></td>    </tr>        <?php        $totalprice="";    $shoppingcart=unserialize($_COOKIE["shoppingcart"]);    foreach ($shoppingcart as $key=>$value){        $keys=array($key);        ?>    <tr>        <td><?php echo $value[0]?></td>        <td height="30">        <?php        $sql="select *from goods where id=".intval($value[0]);        $result=$mysqli->query($sql);        $row=$result->fetch_assoc();        $totalprice+=$row["salesprice"]*$value[1];        echo '<a href="goodsshow.php?cid='.$row['goodstypeid'].'&id='.$row['id'].'" class="title" target="_blank">'.$row['title'].'</a>';        ?>        </td>        <td><?php echo $value[1]?></td>        <td><?php echo $row["salesprice"]*$value[1]?></td>        <td><a href="shoppingcart.php?a=delone&key=<?php echo $key?>" onclick="">取消</a></td>    </tr>        <?php    }    ?></table>    <hr>    <span style="float: right;width: 250px;height: 150px">        总价格:<?php echo $totalprice?><a href="">下一步</a>  <a href="shoppingcart.php?a=empty">清空购物车</a>    </span><?php}else{    echo "<div class='shoppingcartempty'>您的购物车目前没有商品!跳回首页......</div>";  //两秒后跳回首页   echo <<<end   <script>function showpage(){ location.href="goods.php";}setTimeout("showpage()",2000);</script> end;}?>

conn.inc.php:

<?phpdefine("HOST",'localhost');define("USER",'root');define("PWD",'root');define("DBNAME",'onecms');

mysqli.php:

<?phpinclude 'conn.inc.php';$mysqli=new mysqli(HOST,USER,PWD,DBNAME);if($mysqli->connect_errno){    die('数据库链接出错'.$mysqli->connect_error);}

以上为全部代码,简单购物车功能就实现了.


原创粉丝点击