欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

php購物車實(shí)現(xiàn)方法

 更新時(shí)間:2015年01月03日 11:04:36   投稿:shichen2014  
這篇文章主要介紹了php購物車實(shí)現(xiàn)方法,通過4個(gè)文件實(shí)現(xiàn)購物車的功能,且使用txt文件保存購物車內(nèi)容,簡單實(shí)用,需要的朋友可以參考下

本文實(shí)例講述了php購物車實(shí)現(xiàn)方法。分享給大家供大家參考。具體分析如下:

這里我們?yōu)槟闾峁﹤€(gè)簡單的php購物車代碼,從增加購物產(chǎn)品與發(fā)生購買了,在商城開發(fā)中,這個(gè)功能是少不了的,我們不需要數(shù)據(jù)庫,用了txt文本文件來操作用戶購物的內(nèi)容.

增加商品到購物車,代碼如下:

復(fù)制代碼 代碼如下:
<?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 "已經(jīng)注冊(cè)";
 
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];
}
?>
<html>
<head>
    <title>
    <?php if ($_POST[ordered]) {  ?>
        已經(jīng)添加 <?php echo $curr_product[name]; ?> 到您的購物籃
    <?php } else {  ?>
        添加 <?php echo $curr_product[name]; ?> 到您的購物籃
    <?php } ?>
    </title>
</head>
<body>
<?php if ($_POST[ordered]) {  ?>
    <h1><?php echo $curr_product[name]; ?>
        添加至購物籃成功</h1>
 
    <a href="cart.php">返回</a> 商品列表頁面.
<?php }  else {  ?>
    <h1>添加 <?php echo $curr_product[name]; ?> 到您的購物籃</h1>
 
    <form action="<?php echo $PHP_SELF; ?>" method="post">
    商品名稱: <?php echo $curr_product[name]; ?>
    <br>
    商品說明: <?php echo $curr_product[desc]; ?>
    <br>
    商品單價(jià): RMB<?php echo $curr_product[price]; ?>
    <br>
    商品數(shù)量: <input type="text" size="7" name="quantity">
    <input type="hidden" name="id" value="<?php echo $_GET[id]; ?>">
    <input type="hidden" name="ordered" value="1">
    <input type="submit" value="添加至購物欄">
    </form>
<?php } ?>
</body>
</html>

查看購物車的商品,代碼如下:

復(fù)制代碼 代碼如下:
<?php
//
// cart.php:
//
session_start();
 
require 'lib.inc.php';
//判斷購物籃會(huì)話變量cart是否注冊(cè),不注冊(cè)則注冊(cè)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(); //載入物品列表
?>
<html>
<head>
    <title>演示會(huì)話跟蹤的購物籃程序</title>
</head>
 
<body>
 
<h1>歡迎進(jìn)入網(wǎng)上商店</h1>
 
<?php
if ($_SESSION[cart][num_items]) {  // If there is something to show
?>
<h2>當(dāng)前在購物籃里的物品</h2>
<br>
<table border="2" cellpadding="5" cellspacing="2">
<tr>
    <th>
        商品名稱
    </th>
    <th>
        商品說明
    </th>
    <th>
        單價(jià)
    </th>
    <th>
        數(shù)量
    </th>
    <th>&nbsp;
        
    </th>
</tr>
<?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];
?>
<tr>
    <td>
        <?php echo $master_products_list[$product_id][name]; ?>
    </td>
    <td>
        <?php echo $master_products_list[$product_id][desc]; ?>
    </td>
    <td>
        <?php echo $master_products_list[$product_id][price]; ?>
    </td>
    <td>
        <form action="change_quant.php" method="post">
        <input type="hidden" name="id" value="<?php echo $i; ?>">
        <input type="text" size="3" name="quantity"
                value="<?php echo $quantity; ?>">
    </td>
    <td>
        <input type="submit" value="數(shù)量更改">
        </form>
    </td>
</tr>
<?php
    }
?>
<tr>
    <td colspan="2" ALIGN="right">
       <b>合計(jì): </b>
    </td>
    <td colspan="2">
        RMB:<?php echo $total; ?>
    </td>
 <td>&nbsp;</td>
</tr>
</table>
<br>
<br>
<?php
}
?>
 
<h2>商店待出售的商品</h2>
<br>
<i>
    我們提供以下商品待售:
</i>
<br>
<table border="2" cellpadding="5" cellspacing="2">
<tr>
    <th>
        商品名稱
    </th>
    <th>
        商品說明
    </th>
    <th>
        單價(jià)
    </th>
    <th>&nbsp;
        
    </th>
</tr>
<?php
    // Show all of the products
    foreach ($master_products_list as $product_id => $item) {
?>
<tr>
    <td>
        <?php echo $item[name]; ?>
    </td>
    <td>
        <?php echo $item[desc]; ?>
    </td>
    <td>
        $<?php echo $item[price]; ?>
    </td>
    <td>
        <a href="add_item.php?id=<?php echo $product_id; ?>">
            添加至購物籃
        </a>
    </td>
</tr>
<?php
    }
 
?>
</table>

修改購物車的數(shù)量,代碼如下:

復(fù)制代碼 代碼如下:
<?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>

功能頁面,用戶把購物車?yán)锩娴膬?nèi)容保存到txt數(shù)據(jù)庫,代碼如下:

復(fù)制代碼 代碼如下:
<?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); //去掉首尾特殊符號(hào)
        $master_products_list[$id] = array("name" =>  $name, //名稱
                                           "desc" =>  $desc, //說明
                                           "price" => $price); //單價(jià)
    }
 
    @fclose($fp)  //關(guān)閉文件
        or die("關(guān)閉 $filename 文件失敗");
}
?>

很簡單,我們只用了4個(gè)文件就實(shí)現(xiàn)用php 做好購物車功能,好了這只是一款簡單的php購物車代碼更復(fù)雜的需要考慮更多更好.

希望本文所述對(duì)大家的php程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • PHP在linux上執(zhí)行外部命令的方法

    PHP在linux上執(zhí)行外部命令的方法

    下面小編就為大家?guī)硪黄狿HP在linux上執(zhí)行外部命令的方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-02-02
  • PHP函數(shù)篇詳解十進(jìn)制、二進(jìn)制、八進(jìn)制和十六進(jìn)制轉(zhuǎn)換函數(shù)說明

    PHP函數(shù)篇詳解十進(jìn)制、二進(jìn)制、八進(jìn)制和十六進(jìn)制轉(zhuǎn)換函數(shù)說明

    中文字符編碼研究系列第一期,PHP函數(shù)篇詳解十進(jìn)制、二進(jìn)制、八進(jìn)制和十六進(jìn)制互相轉(zhuǎn)換函數(shù)說明,主要掌握各進(jìn)制轉(zhuǎn)換的方法,以應(yīng)用于實(shí)際開發(fā)
    2011-12-12
  • 詳解PHP json_decode()函數(shù)的使用方法

    詳解PHP json_decode()函數(shù)的使用方法

    PHP中的json_decode()函數(shù)是用于將JSON字符串解碼為PHP變量的函數(shù),它將JSON字符串轉(zhuǎn)換為與其對(duì)應(yīng)的PHP數(shù)據(jù)類型,例如,將JSON字符串轉(zhuǎn)換為關(guān)聯(lián)數(shù)組、對(duì)象或其他數(shù)據(jù)類型,本文就給大家介紹一下PHP json_decode()函數(shù)的使用方法,需要的朋友可以參考下
    2023-08-08
  • php實(shí)現(xiàn)的生成排列算法示例

    php實(shí)現(xiàn)的生成排列算法示例

    這篇文章主要介紹了php實(shí)現(xiàn)的生成排列算法,結(jié)合實(shí)例形式分析了php基于遞歸、遍歷字符串實(shí)現(xiàn)全排列相關(guān)算法實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2019-07-07
  • 通過緩存數(shù)據(jù)庫結(jié)果提高PHP性能的原理介紹

    通過緩存數(shù)據(jù)庫結(jié)果提高PHP性能的原理介紹

    眾所周知,緩存數(shù)據(jù)庫查詢的結(jié)果可以顯著縮短腳本執(zhí)行時(shí)間,并最大限度地減少數(shù)據(jù)庫服務(wù)器上的負(fù)載。如果要處理的數(shù)據(jù)基本上是靜態(tài)的,則該技術(shù)將非常有效。這是因?yàn)閷?duì)遠(yuǎn)程數(shù)據(jù)庫的許多數(shù)據(jù)請(qǐng)求最終可以從本地緩存得到滿足,從而不必連接到數(shù)據(jù)庫、執(zhí)行查詢以及獲取結(jié)果
    2012-09-09
  • PHP程序員必須知道的兩種日志實(shí)例分析

    PHP程序員必須知道的兩種日志實(shí)例分析

    這篇文章主要介紹了PHP程序員必須知道的兩種日志,結(jié)合實(shí)例形式分析了php-fpm 慢日志及php-error 錯(cuò)誤日志相關(guān)原理與使用技巧,需要的朋友可以參考下
    2020-05-05
  • PHP中使用imagick生成PSD文件縮略圖教程

    PHP中使用imagick生成PSD文件縮略圖教程

    這篇文章主要介紹了PHP中使用imagick生成PSD文件縮略圖教程,本文講解了安裝ImageMagick、安裝imagick、檢查安裝是否成功、生成PSD文件縮略圖等內(nèi)容,需要的朋友可以參考下
    2015-01-01
  • PHP實(shí)現(xiàn)在windows下配置sendmail并通過mail()函數(shù)發(fā)送郵件的方法

    PHP實(shí)現(xiàn)在windows下配置sendmail并通過mail()函數(shù)發(fā)送郵件的方法

    這篇文章主要介紹了PHP實(shí)現(xiàn)在windows下配置sendmail并通過mail()函數(shù)發(fā)送郵件的方法,較為詳細(xì)的分析了Windows平臺(tái)sendmail配置步驟與相應(yīng)的使用技巧,需要的朋友可以參考下
    2017-06-06
  • 解決163/sohu/sina不能夠收到PHP MAIL函數(shù)發(fā)出郵件的問題

    解決163/sohu/sina不能夠收到PHP MAIL函數(shù)發(fā)出郵件的問題

    最近準(zhǔn)備改改自己的個(gè)人主頁,加上發(fā)郵件到功能,發(fā)現(xiàn)居然不是那么好用。gmail和msn可以收到郵件,但是163/sina/sohu的郵箱居然都收不到,非常郁悶。其實(shí)代碼也很簡單。 php manual 已經(jīng)說得相當(dāng)清楚了。
    2009-03-03
  • PHP+RabbitMQ實(shí)現(xiàn)消息隊(duì)列的完整代碼

    PHP+RabbitMQ實(shí)現(xiàn)消息隊(duì)列的完整代碼

    這篇文章主要給大家介紹了關(guān)于利用PHP+RabbitMQ實(shí)現(xiàn)消息隊(duì)列的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用PHP具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03

最新評(píng)論