茄子在线看片免费人成视频,午夜福利精品a在线观看,国产高清自产拍在线观看,久久综合久久狠狠综合

    <s id="ddbnn"></s>
  • <sub id="ddbnn"><ol id="ddbnn"></ol></sub>

  • <legend id="ddbnn"></legend><s id="ddbnn"></s>

    php購(gòu)物車實(shí)現(xiàn)方法
    來(lái)源:易賢網(wǎng) 閱讀:923 次 日期:2015-03-31 11:08:10
    溫馨提示:易賢網(wǎng)小編為您整理了“php購(gòu)物車實(shí)現(xiàn)方法”,方便廣大網(wǎng)友查閱!

    這篇文章主要介紹了php購(gòu)物車實(shí)現(xiàn)方法,通過(guò)4個(gè)文件實(shí)現(xiàn)購(gòu)物車的功能,且使用txt文件保存購(gòu)物車內(nèi)容,簡(jiǎn)單實(shí)用,需要的朋友可以參考下

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

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

    增加商品到購(gòu)物車,代碼如下:

    代碼如下:

    <?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]; ?> 到您的購(gòu)物籃

    <?php } else { ?>

    添加 <?php echo $curr_product[name]; ?> 到您的購(gòu)物籃

    <?php } ?>

    </title>

    </head>

    <body>

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

    <h1><?php echo $curr_product[name]; ?>

    添加至購(gòu)物籃成功</h1>

    <a href="cart.php">返回</a> 商品列表頁(yè)面.

    <?php } else { ?>

    <h1>添加 <?php echo $curr_product[name]; ?> 到您的購(gòu)物籃</h1>

    <form action="<?php echo $PHP_SELF; ?>" method="post">

    商品名稱: <?php echo $curr_product[name]; ?>

    <br>

    商品說(shuō)明: <?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="添加至購(gòu)物欄">

    </form>

    <?php } ?>

    </body>

    </html>

    查看購(gòu)物車的商品,代碼如下:

    代碼如下:

    <?php

    //

    // cart.php:

    //

    session_start();

    require 'lib.inc.php';

    //判斷購(gòu)物籃會(huì)話變量cart是否注冊(cè),不注冊(cè)則注冊(cè)cart變量

    if (session_is_registered('cart')) {

    session_register('cart');

    }

    // 如果購(gòu)物籃沒(méi)有初始化,則初始化購(gòu)物籃

    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ì)話跟蹤的購(gòu)物籃程序</title>

    </head>

    <body>

    <h1>歡迎進(jìn)入網(wǎng)上商店</h1>

    <?php

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

    ?>

    <h2>當(dāng)前在購(gòu)物籃里的物品</h2>

    <br>

    <table border="2" cellpadding="5" cellspacing="2">

    <tr>

    <th>

    商品名稱

    </th>

    <th>

    商品說(shuō)明

    </th>

    <th>

    單價(jià)

    </th>

    <th>

    數(shù)量

    </th>

    <th>

    </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> </td>

    </tr>

    </table>

    <br>

    <br>

    <?php

    }

    ?>

    <h2>商店待出售的商品</h2>

    <br>

    <i>

    我們提供以下商品待售:

    </i>

    <br>

    <table border="2" cellpadding="5" cellspacing="2">

    <tr>

    <th>

    商品名稱

    </th>

    <th>

    商品說(shuō)明

    </th>

    <th>

    單價(jià)

    </th>

    <th>

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

    添加至購(gòu)物籃

    </a>

    </td>

    </tr>

    <?php

    }

    ?>

    </table>

    修改購(gòu)物車的數(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> 商品列表頁(yè)面.

    </body>

    </html>

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

    代碼如下:

    <?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, //說(shuō)明

    "price" => $price); //單價(jià)

    }

    @fclose($fp) //關(guān)閉文件

    or die("關(guān)閉 $filename 文件失敗");

    }

    ?>

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

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

    更多信息請(qǐng)查看IT技術(shù)專欄

    更多信息請(qǐng)查看網(wǎng)絡(luò)編程
    易賢網(wǎng)手機(jī)網(wǎng)站地址:php購(gòu)物車實(shí)現(xiàn)方法
    由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

    2026上岸·考公考編培訓(xùn)報(bào)班

    • 報(bào)班類型
    • 姓名
    • 手機(jī)號(hào)
    • 驗(yàn)證碼
    關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢 | 簡(jiǎn)要咨詢須知 | 新媒體/短視頻平臺(tái) | 手機(jī)站點(diǎn) | 投訴建議
    工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
    云南網(wǎng)警備案專用圖標(biāo)
    聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
    咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
    云南網(wǎng)警報(bào)警專用圖標(biāo)