添加到购物车的编码是正确的形式?

时间:2015-10-22 13:38:00

标签: php

<?php
session_start();
include("conn.php");

$action = $_POST['action'];
$user = $_SESSION['username'];

if(empty($user)){
echo"<script>alert('Please log in!');window.location='Log In.php';</script>";
exit;
}

if($action == 'add'){
$cart_arr = array(
  'foodID'=>$_POST['foodID'],
  'order_num'=>$_POST['order_num'],
  'food_type'=>$_POST['food_type'],
  );

  $cart_session = $_SESSION['cart_'.$user];
  if(empty($cart_session)){
      $cart_session[$cart_arr['foodID']] = $cart_arr;
  } else if(!empty($cart_session[$cart_arr['foodID']])){
      $cart_session[$cart_arr['foodID']]['order_num']+=$cart_arr['order_num'];
  } else {
      echo $cart_session[$cart_arr['foodID']] = $cart_arr;
  }

  $_SESSION['cart_'.$user] = $cart_session;
  } else if($action == 'clear'){
      $_SESSION['cart_'.$user]=array();
      echo"<script>alert('Shopping cart is empty, return home!');window.location='homepage.php';</script>";
      exit;

  } else if($action == 'change'){
      $temp_cart = $_SESSION['cart_'.$user];
      foreach($temp_cart as $k=>$v){
          if($_POST['goods_'.$k]!= $v['order_num']){
              $temp_cart[$k]['order_num'] = $_POST['goods_'.$k];
          }

    if($_POST['goods_'.$k] == 0){
        unset($temp_cart[$k]);
    }
   }

   $_SESSION['cart_'.$user] = $temp_cart;
   }

   if(empty($_SESSION['cart_'.$user])){
       echo"<script>alert('Shopping cart is empty, please add some orders!');window.location = 'homepage.php';</script>";

    exit;
 }

 $goods_id = array();
 $cart = $_SESSION['cart_'.$user];
 $v['food_type'] = $_POST['food_type'];
 foreach($cart as $k=>$v){
     $goods_id[$v['foodID']] = $v['foodID'];
 }

 $goods_id_str = implode(",",$goods_id);
 mysql_query("set names utf8");
 $sql = "select * from foodmenu where foodID IN (".$goods_id_str.")";
 $query = mysql_query($sql);
 $cart_goods = array();
 while($arr = mysql_fetch_array($query)){
     $cart_goods[$arr['foodID']] = $arr;
 }

 foreach($cart as $k=>$v){
     $cart[$k]['food_name'] = $cart_goods[$k]['food_name'];
     $cart[$k]['food_img'] = str_replace("../","",$cart_goods[$k]['food_img']);
     $cart[$k]['food_price'] = $cart_goods[$k]['food_price'];
     $cart[$k]['food_description'] = $_POST['food_description'];
 }

?>

我可以知道这个编码是否正确? 因为它在点击上一个php上的按钮时显示空白页面,用于添加到购物车的目的,它只显示顶部的正常标题。 我将附上表格来访问这个php。

<div class="detailtop">
    <?php    

        $result = mysql_query("SELECT * FROM foodmenu where foodID = '$foodID'"); 
        while($row=mysql_fetch_array($result)){
             ?>
        <dl>
            <dt>
            <img src="<?php echo  $row["food_img"];?>" /> </dt>
            <dd>
    <form action="order.php" method="get" name="send" onSubmit="return Check()"  enctype="multipart/form-data">
                <h3><?php echo  $row["food_name"];?></h3>
                <div class="detailtips">
                    <?php echo  $row["food_description"];?>
                </div>
                <p><span>Restaurant:</span><strong><?php echo  $row["restaurant_name"];?></strong></p>
                <p><span>Type :</span><strong><?php echo  $row["food_type"];?></strong></p>
                <p><span>Price :</span>RM &nbsp;<strong><?php echo  $row["food_price"];?><input name="num"  type="hidden" class="num" value="<?php echo  $row["food_price"];?>" /></strong></p>

                <div class="order" style=" padding-top:20px; padding-left:20px;">
                <input name="id" type="hidden" value="<?php echo  $row["foodID"];?>" />
                <input name="" type="submit"  value="" class="ordersubmit" style=" margin-left:30px; margin-top:20px;">
                </div>
                </form>
            </dd>
        </dl>
        <?php }?>
    </div>

0 个答案:

没有答案