//提交订单
function add_order(){
session_start();
// var_dump($_SESSION);
// die();
// session_destroy();
// die(); $ary =$_POST['order'];
$ary= json_decode($ary, true);
// var_dump($data1);
// die(); $shop_list = $ary['order'];
$total_money = $ary['order_money'];
$obj =UserModel::getInstance();
//获得user_id
$user_id = $_SESSION['uid'];
//获得对应的cart_id
$sql = "select id from cart where user_id = '$user_id'";
$cart_id = $obj->getOne($sql); // $arr = $_SESSION['gwc'];
foreach($shop_list as $k=> $shop){
// var_dump($k);
// die();
// 获取对应商品的订单金额
$xiaoji = $ary[$k]; // 获取8位随机值作为order_id $strs="QWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnm";
$random=substr(str_shuffle($strs),mt_rand(0,strlen($strs)-11),8);
$order_id = strval(time())."$random"; //插入一条订单
$sql = "INSERT INTO orders(id,user_id,xiaoji) VALUES ('$order_id', '$user_id',$xiaoji)";
$obj->exec($sql);
// die();
// echo "$sql";
// die();
foreach($shop as $sid=> $sku_id){ // 拼接sku_id
// $sku_id = "$goods_id"."_"."$color_id"."_"."$size_id";
$number ="num_$sku_id";
$price = "price_$sku_id";
$num = $ary[$number];
$pay_price = $ary[$price];
//插入一条sku_orders记录
$sql = "INSERT INTO orders_sku(sku_id,orders_id,num,pay_price) VALUES ('$sku_id','$order_id',$num,$pay_price)";
// echo("$sql");
// die();
$obj->exec($sql); // $sql = "select count(*) from cart_sku";
// echo("$sql");
// die();
// $_SESSION["goods_number"] = $obj->getOne($sql);
$_SESSION["goods_number"]--; $sql = "delete from cart_sku where sku_id = '$sku_id' and cart_id = '$cart_id'";
// echo("$sql");
// die();
$obj->exec($sql); } } echo "OK";
exit; // die(); }
json_decode($ary, true);这句是把ajax方式传过来的json串,转为php的数组数组形式,还有一种是转为对象。
$strs="QWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnm";
$random=substr(str_shuffle($strs),mt_rand(0,strlen($strs)-11),8);
上面两句是从str字符串(包含大小写和数字)获取一个8位的随机值。
$order_id = strval(time())."$random";这句是使用时间戳和一个随机值拼接形成订单的id。