本文实例讲述了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>
</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>
</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程序设计有所帮助。
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
更新日志
- 《汽车发烧音乐精选CD18》[FLAC+CUE][7.1G]
- 郑希怡.2004-Perfect.Date【英皇娱乐】【FLAC分轨】
- 群星.1994-宝丽金超级孖宝2CD【宝丽金】【WAV+CUE】
- 林忆莲.2019-0(2023精装版)【环球】【WAV+CUE】
- Supper.Moment.2016-The.Moment【寰亚】【FLAC分轨】
- 范莉丝.1979-送郎一朵牵牛花(LP版)【大联机构】【WAV+CUE】
- 刘宇.2024-宇宙诗人【哔哟哔哟文化】【FLAC分轨】
- 《全景声白噪音》沉浸式大自然助眠 带你听自然的声音[mp3]
- 纯音入心系列纯音乐《古筝精选 十指弹一赏心曲一心奏出世人心》1CD[MP3][582MB]
- 纯音入心系列纯音乐《古筝与现代音乐的交织》1CD[MP3][96MB]
- 王俪婷.2011-ROMANCE【华研国际】【WAV+CUE】
- 狮子合唱团.2016-LION(引进版)【华纳】【WAV+CUE】
- 范文芳.1996-FNNTASY【HYPE】【WAV+CUE】
- 纯音入心系列纯音乐《韵味古筝曲》1CD[MP3][146.2MB]
- 纯音入心系列纯音乐《古筝系列-传统乐曲》1CD[MP3][974.7MB]