【www.gdgbn.com--php入门】

主要是购物车部分代码的讲解,和对session操作的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 />    



   


        添加至购物篮成功

    返回 商品列表页面.

   

添加 到您的购物篮

   


    商品名称:
   

    商品说明:
   

    商品单价: rmb
   

    商品数量:
   
   

   
   





php代码

//
// cart.php:  the main file
//
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
?>

当前在购物篮里的物品






   
        商品名称
   
   
        商品说明
   
   
        单价
   
   
        数量
   
   
         
   

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

   
       
   
   
       
   
   
       
   
   
       


       
                        value="">
   
   
       
       

   

    }
?>

   
       合计:
   
   
        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 />    


   

将数量: 更改为
        


    返回 商品列表页面.

打开文件

//物品数组
$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 文件失败");
}
?>

本文来源:http://www.gdgbn.com/jiaocheng/27800/