无为清净楼资源网 Design By www.qnjia.com

本文实例讲述了php购物车实现方法。分享给大家供大家参考。具体分析如下:

这里我们为你提供个简单的php购物车代码,从增加购物产品与发生购买了,在商城开发中,这个功能是少不了的,我们不需要数据库,用了txt文本文件来操作用户购物的内容.

增加商品到购物车,代码如下:
复制代码 代码如下:<"已经注册";
 
if ($_POST[ordered]) {  // If they have chosen the product
 
    array_push($_SESSION[cart][products], array(trim($_POST[id]), $_POST[quantity]));
    $_SESSION[cart][num_items] += $_POST[quantity];
}
"cart.php">返回</a> 商品列表页面.
<"<" method="post">
    商品名称: <"text" size="7" name="quantity">
    <input type="hidden" name="id" value="<">
    <input type="hidden" name="ordered" value="1">
    <input type="submit" value="添加至购物栏">
    </form>
<"codetitle">复制代码 代码如下:<"num_items" => 0,
                  "products"  => array());
}
// From site_lib.inc, Loads the $master_products_list array
LoadProducts(); //载入物品列表
"2" cellpadding="5" cellspacing="2">
<tr>
    <th>
        商品名称
    </th>
    <th>
        商品说明
    </th>
    <th>
        单价
    </th>
    <th>
        数量
    </th>
    <th>&nbsp;
        
    </th>
</tr>
<"change_quant.php" method="post">
        <input type="hidden" name="id" value="<">
        <input type="text" size="3" name="quantity"
                value="<">
    </td>
    <td>
        <input type="submit" value="数量更改">
        </form>
    </td>
</tr>
<"2" ALIGN="right">
       <b>合计: </b>
    </td>
    <td colspan="2">
        RMB:<"2" cellpadding="5" cellspacing="2">
<tr>
    <th>
        商品名称
    </th>
    <th>
        商品说明
    </th>
    <th>
        单价
    </th>
    <th>&nbsp;
        
    </th>
</tr>
<"add_item.php">
            添加至购物篮
        </a>
    </td>
</tr>
<"codetitle">复制代码 代码如下:<"cart.php">返回</a> 商品列表页面.
</body>
</html>

功能页面,用户把购物车里面的内容保存到txt数据库,代码如下:
复制代码 代码如下:<"r")
        or die("打开 $filename 文件失败");
    @flock($fp, 1)
        or die("锁定 $filename 文件失败");
 
    //读取文件内容
    while ($line = fgets($fp, 1024)) {
        list($id, $name, $desc, $price) = explode('|', $line); //读取每行数据,数据以| 格开
        $id = trim($id); //去掉首尾特殊符号
        $master_products_list[$id] = array("name" =>  $name, //名称
                                           "desc" =>  $desc, //说明
                                           "price" => $price); //单价
    }
 
    @fclose($fp)  //关闭文件
        or die("关闭 $filename 文件失败");
}
?>
很简单,我们只用了4个文件就实现用php 做好购物车功能,好了这只是一款简单的php购物车代码更复杂的需要考虑更多更好.

希望本文所述对大家的php程序设计有所帮助。

标签:
php,购物车,方法

无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com