php購物車實現(xiàn)方法(2)_PHP教程
教程Tag:暫無Tag,歡迎添加,賺取U幣!
推薦:PHP實現(xiàn)格式化文件數(shù)據(jù)大小顯示的方法這篇文章主要介紹了PHP實現(xiàn)格式化文件數(shù)據(jù)大小顯示的方法,通過一個自定義函數(shù)實現(xiàn)針對文件大小的精確格式化,具有一定的參考借鑒價值,需要的朋友可以參考下 本文實例講述了PHP實現(xiàn)格式化文件數(shù)據(jù)大小顯示的方法。分享給大家供大家參考。具體分析如下: 有時候我們需要在
修改購物車的數(shù)量,代碼如下:
代碼如下: <?php//
// change_quant.php:
// Change the quantity of an item in the shopping cart.
//
session_start();
if (session_is_registered('cart')) {
session_register('cart');
}
// Typecast to int, making sure we access the
// right element below
$i = (int)$_POST[id];
// Save the old number of products for display
// and arithmetic
$old_num = $_SESSION[cart][products][$i][1];
if ($_POST[quantity]) {
$_SESSION[cart][products][$i][1] = $_POST[quantity]; //change the quantity
} else {
unset($_SESSION[cart][products][$i]); // Send the product into oblivion
}
// Update the number of items
$_SESSION[cart][num_items] = ($old_num >$_POST[quantity]) ?
$_SESSION[cart][num_items] - ($old_num-$_POST[quantity]) :
$_SESSION[cart][num_items] + ($_POST[quantity]-$old_num);
?>
<html>
<head>
<title>
數(shù)量修改
</title>
</head>
<body>
<h1> 將數(shù)量: <?php echo $old_num; ?> 更改為
<?php echo $_POST[quantity]; ?></h1>
<a href="cart.php">返回</a> 商品列表頁面.
</body>
</html>
功能頁面,用戶把購物車里面的內(nèi)容保存到txt數(shù)據(jù)庫,代碼如下:
代碼如下: <?php//物品數(shù)組
$master_products_list = array();
//載入物品數(shù)據(jù)函數(shù)
function LoadProducts() {
global $master_products_list;
$filename = 'products.txt';
$fp = @fopen($filename, "r")
or die("打開 $filename 文件失敗");
@flock($fp, 1)
or die("鎖定 $filename 文件失敗");
//讀取文件內(nèi)容
while ($line = fgets($fp, 1024)) {
list($id, $name, $desc, $price) = explode('|', $line); //讀取每行數(shù)據(jù),數(shù)據(jù)以| 格開
$id = trim($id); //去掉首尾特殊符號
$master_products_list[$id] = array("name" => $name, //名稱
"desc" => $desc, //說明
"price" => $price); //單價
}
@fclose($fp) //關閉文件
or die("關閉 $filename 文件失敗");
}
?>
很簡單,我們只用了4個文件就實現(xiàn)用php 做好購物車功能,好了這只是一款簡單的php購物車代碼更復雜的需要考慮更多更好.
希望本文所述對大家的php程序設計有所幫助。
分享:php自定義加密與解密程序實例這篇文章主要介紹了php自定義加密與解密程序,實例分析了自定義加密解密類文件及相關用法,具有一定參考借鑒價值,需要的朋友可以參考下 本文實例講述了php自定義加密與解密程序。分享給大家供大家參考。具體分析如下: PHP3 Cryption是一個非常容易被破解,不安全的加密功
相關PHP教程:
- 相關鏈接:
- 教程說明:
PHP教程-php購物車實現(xiàn)方法(2)
。