php購物車實現方法,php購物車實現_PHP教程

來源:互聯網
上載者:User

php購物車實現方法,php購物車實現


本文執行個體講述了php購物車實現方法。分享給大家供大家參考。具體分析如下:

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

增加商品到購物車,代碼如下:
複製代碼 代碼如下:<?php
//
// 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 />


<?php if ($_POST[ordered]) { ?>

<?php echo $curr_product[name]; ?>
添加至購物籃成功



返回 商品列表頁面.
<?php } else { ?>

添加 <?php echo $curr_product[name]; ?> 到您的購物籃




<?php } ?>

查看購物車的商品,代碼如下:
複製代碼 代碼如下:<?php
//
// cart.php:
//
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(); //載入物品列表
?>


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




歡迎進入網上商店



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

當前在購物籃裡的物品




































<?php // Loop through the products foreach ($_SESSION[cart][products] as $i => $product) { $product_id = $product[0]; $quantity = $product[1]; $total += $quantity * (double)$master_products_list[$product_id][price]; ?> <?php } ?>

商品名稱

商品說明

單價

數量



<?php echo $master_products_list[$product_id][name]; ?>

<?php echo $master_products_list[$product_id][desc]; ?>

<?php echo $master_products_list[$product_id][price]; ?>



合計:

RMB:<?php echo $total; ?>





<?php
}
?>

商店待出售的商品





我們提供以下商品待售:
























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

商品名稱

商品說明

單價



<?php echo $item[name]; ?>

<?php echo $item[desc]; ?>

$<?php echo $item[price]; ?>

">
添加至購物籃

修改購物車的數量,代碼如下:
複製代碼 代碼如下:<?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);
?>



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


將數量: <?php echo $old_num; ?> 更改為
<?php echo $_POST[quantity]; ?>


返回 商品列表頁面.

功能頁面,使用者把購物車裡面的內容儲存到txt資料庫,代碼如下:
複製代碼 代碼如下:<?php
//物品數組
$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購物車代碼更複雜的需要考慮更多更好.

希望本文所述對大家的php程式設計有所協助。

http://www.bkjia.com/PHPjc/936801.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/936801.htmlTechArticlephp購物車實現方法,php購物車實現 本文執行個體講述了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.