溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

使用php怎么編寫一個購物車功能

發布時間:2021-02-03 11:08:06 來源:億速云 閱讀:230 作者:Leah 欄目:開發技術

使用php怎么編寫一個購物車功能?針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

 代碼如下:

<?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];
}
?>
<html>
<head>
    <title>
    <?php if ($_POST[ordered]) {  ?>
        已經添加 <?php echo $curr_product[name]; ?> 到您的購物籃
    <?php } else {  ?>
        添加 <?php echo $curr_product[name]; ?> 到您的購物籃
    <?php } ?>
    </title>
</head>
<body>
<?php if ($_POST[ordered]) {  ?>
    <h2><?php echo $curr_product[name]; ?>
        添加至購物籃成功</h2>
 
    <a href="cart.php">返回</a> 商品列表頁面.
<?php }  else {  ?>
    <h2>添加 <?php echo $curr_product[name]; ?> 到您的購物籃</h2>
 
    <form action="<?php echo $PHP_SELF; ?>" method="post">
    商品名稱: <?php echo $curr_product[name]; ?>
    <br>
    商品說明: <?php echo $curr_product[desc]; ?>
    <br>
    商品單價: RMB<?php echo $curr_product[price]; ?>
    <br>
    商品數量: <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>

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

復制代碼 代碼如下:

<?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(); //載入物品列表
?>
<html>
<head>
    <title>演示會話跟蹤的購物籃程序</title>
</head>
 
<body>
 
<h2>歡迎進入網上商店</h2>
 
<?php
if ($_SESSION[cart][num_items]) {  // If there is something to show
?>
<h3>當前在購物籃里的物品</h3>
<br>
<table border="2" cellpadding="5" cellspacing="2">
<tr>
    <th>
        商品名稱
    </th>
    <th>
        商品說明
    </th>
    <th>
        單價
    </th>
    <th>
        數量
    </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="數量更改">
        </form>
    </td>
</tr>
<?php
    }
?>
<tr>
    <td colspan="2" ALIGN="right">
       <b>合計: </b>
    </td>
    <td colspan="2">
        RMB:<?php echo $total; ?>
    </td>
 <td>&nbsp;</td>
</tr>
</table>
<br>
<br>
<?php
}
?>
 
<h3>商店待出售的商品</h3>
<br>
<i>
    我們提供以下商品待售:
</i>
<br>
<table border="2" cellpadding="5" cellspacing="2">
<tr>
    <th>
        商品名稱
    </th>
    <th>
        商品說明
    </th>
    <th>
        單價
    </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>

修改購物車的數量,代碼如下:

復制代碼 代碼如下:

<?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>
        數量修改
    </title>
</head>
<body>
    <h2> 將數量: <?php echo $old_num; ?> 更改為
         <?php echo $_POST[quantity]; ?></h2>
    <a href="cart.php">返回</a> 商品列表頁面.
</body>
</html>

功能頁面,用戶把購物車里面的內容保存到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 文件失敗");
}
?>


關于使用php怎么編寫一個購物車功能問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

php
AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女