php 购物车例子

<?php
session_start();

include_once '../include/db.inc.php';
include_once '../include/template.inc.php';
include_once '../include/function.inc.php';

$act = $_REQUEST['act'];   //操作
$pid = $_REQUEST['pid'];   //产品id号
$num = $_REQUEST['num'];   //数量

$order_pid_arr = explode('|X|', $_SESSION['order_pid_str']); //把产品id session中的字符串转换成数组
$order_num_arr = explode('|X|', $_SESSION['order_num_str']); //把产品num session中的字符串转换成数组

if($act == 'del'){        //操作为删除模式
 for($i=0;$i<count($order_pid_arr);$i++){
  if($pid == $order_pid_arr[$i]){   //当要删除的产品id等于数组中id时
    $order_num_arr[$i] = 0;   //更新数量为0
  }
 }
 $_SESSION['order_num_str'] = implode('|X|', $order_num_arr); //产生新的产品num session

}else if($act == 'add'){
 if(!in_array($pid, $order_pid_arr)){  //如果产品id没有在产品id session数组中
  if($_SESSION['order_pid_str'] != ''){ // 如果原来的产品id session 不是空
   $_SESSION['order_pid_str'] = implode('|X|', $order_pid_arr). '|X|' .$pid;   // 新串 = 旧 |X| $pid
   $_SESSION['order_num_str'] = implode('|X|', $order_num_arr). '|X|' .intval($num); // 新数 = 旧 |X| $num
  }else{         //如果为空
   $_SESSION['order_pid_str'] = $pid;             // 新串 = $pid
   $_SESSION['order_num_str'] = intval($num);           // 新数 = $num
  }
 }else{           //如果存在,只增加数量
  for($i=0;$i<count($order_pid_arr);$i++){ //遍历产品id session
   if($pid == $order_pid_arr[$i]){   //当要增加的产品id等于数组中id时
    $order_num_arr[$i] = $order_num_arr[$i] + intval($num);  //更新数量
   }
  }
  $_SESSION['order_num_str'] = implode('|X|', $order_num_arr);  //产生新的产品num session

 }
}else if($act == 'edit'){
 for($i=0;$i<count($order_pid_arr);$i++){ //遍历产品id session
  if($pid == $order_pid_arr[$i]){   //当要增加的产品id等于数组中id时
   $order_num_arr[$i] = intval($num); //更新数量
  }
 }
 $_SESSION['order_num_str'] = implode('|X|', $order_num_arr);  //产生新的产品num session
}

// 事例化模板类 -----------------------------------
$tpl = new Template_PHPLIB();
// ----------------------------------------------

// 装载模板文件 -----------------------------------
$tpl->set_file('order', 'setOrder.shtml');
// ----------------------------------------------
$tpl->set_block('order', 'order_list', 'order_list_out');

// 输出 ------------------------------------------
$order_pid_arr = explode('|X|', $_SESSION['order_pid_str']); //把产品id session中的字符串转换成数组
$order_num_arr = explode('|X|', $_SESSION['order_num_str']); //把产品num session中的字符串转换成数组

$o_price_total_money = 0;

for($j=0;$j<count($order_num_arr);$j++){
 if(intval($order_num_arr[$j]) > 0){
  
  $get_product_sql = "SELECT `pro_model`,`pro_name`,`pro_price` FROM `pro_catalog` WHERE `pro_id` = '$order_pid_arr[$j]'";
  $get_product_rs = $db->Execute($get_product_sql);

  $pro_model = $get_product_rs->fields["pro_model"];
  $pro_name = $get_product_rs->fields["pro_name"];
  $pro_price = floatval(str_replace(',','',$get_product_rs->fields["pro_price"]));
  
  $pro_quantity = intval($order_num_arr[$j]);
  
  $tpl->set_var('o_no', $j+1);
  $tpl->set_var('o_model', $pro_model);
  $tpl->set_var('o_name', $pro_name);
  $tpl->set_var('o_price', number_format($pro_price, 2, '.', ''));

  $o_price_total = number_format($pro_price*$pro_quantity, 2, '.', '');
  $o_price_total_money += $o_price_total;

  $tpl->set_var('o_price_total', number_format($o_price_total, 2, '.', ''));

  $tpl->set_var('o_id', $order_pid_arr[$j]);
  $tpl->set_var('o_num', $order_num_arr[$j]);
  
  $tpl->parse('order_list_out', 'order_list', true);
 }
}

$tpl->set_var('o_price_total_money', number_format($o_price_total_money, 2, '.', ''));
// ----------------------------------------------

// 输出页面 --------------------------------------
$tpl->parse('output', 'order');
$tpl->p('output');
// ----------------------------------------------
?>
 

http://www.nqqn.com/ym/187/138170.htm(转自)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值