php購物車代碼_PHP教程

來源:互聯網
上載者:User
這裡我們為你提供個簡單的php購物車代碼哦,從增加購物產品與發生購買了,在商城開發中,這個功能是少不了的,我們不需要資料庫,用了txt文字檔來操作使用者購物的內容。

這裡我們為你提供個簡單的php教程購物車代碼哦,從增加購物產品與發生購買了,在商城開發中,這個功能是少不了的,我們不需要資料庫教程,用了txt文字檔來操作使用者購物的內容。

增加商品到購物車

//
// add_item.php:
// Add an item to the shopping cart.
//
session_start();
if (session_is_registered('cart')) {
session_register('cart');
}

require 'lib.inc.php'; // LoadProducts()

LoadProducts(); // Load products in $master_products_list

// Make $curr_product global
$curr_product = array();

// Loop through all the products and pull up the product
// that we are interested in

foreach ($master_products_list as $prod_id => $product) {
if (trim($prod_id) == trim($_GET[id])) {
$curr_product = $product;
}
}


// Register our session
//session_register('cart');
//if(session_is_registered('cart')) echo "已經註冊";

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];

}
?>



<br /> <?php if ($_POST[ordered]) { ?><br /> 已經添加 <?php echo $curr_product[name]; ?> 到您的購物籃<br /> <?php } else { ?><br /> 添加 <?php echo $curr_product[name]; ?> 到您的購物籃<br /> <?php } ?><br />




添加至購物籃成功

返回 商品列表頁面.

添加 到您的購物籃




查看購物車的商品

//
// cart.php: www.bkjia.com
//
session_start();

require 'lib.inc.php';
//判斷購物籃會話變數cart是否註冊,不註冊則註冊cart變數
if (session_is_registered('cart')) {
session_register('cart');
}


// 如果購物籃沒有初始化,則初始化購物籃
if (!isset($_SESSION[cart][num_items])) {
$_SESSION[cart] = array("num_items" => 0,
"products" => array());
}


// From site_lib.inc, Loads the $master_products_list array
LoadProducts(); //載入物品列表
?>



示範會話跟蹤的購物籃程式

歡迎進入網上商店

if ($_SESSION[cart][num_items]) { // If there is something to show
?>

當前在購物籃裡的物品
















$total += $quantity *
(double)$master_products_list[$product_id][price];
?>














// Loop through the products foreach ($_SESSION[cart][products] as $i => $product) { $product_id = $product[0]; $quantity = $product[1]; }?>

商品名稱

商品說明

單價

數量











合計:

RMB:





}
?>

商店待出售的商品





我們提供以下商品待售:



















?>

// Show all of the products foreach ($master_products_list as $product_id => $item) {?> }

商品名稱

商品說明

單價







$

">
添加至購物籃

修改購物車的數量

//
// 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);
?>



<br /> 數量修改<br />


將數量: 更改為


返回 商品列表頁面.

功能頁面,使用者把購物車裡面的內容儲存到txt資料庫

//物品數組
$master_products_list = array();


//載入物品資料函數
function LoadProducts() {
global $master_products_list;
$filename = 'products.txt';

$fp = @fopen($filename, "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購物車代碼更複雜的需要考慮更多更好。


http://www.bkjia.com/PHPjc/444925.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/444925.htmlTechArticle這裡我們為你提供個簡單的php購物車代碼哦,從增加購物產品與發生購買了,在商城開發中,這個功能是少不了的,我們不需要資料庫,用...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.