简体   繁体   中英

add item into add cart

I'm trying to add product into add cart. Please tell me what is the best way. My mess code is.

if(isset($_SESSION['id'])) {
    echo "IF part";
     $_SESSION['id'] = $_SESSION['id'] + $_SESSION['id'];
     $k = $_SESSION['id'];
    // store session data
$_SESSION[$k]['product_name']=$_REQUEST['product_name_value'];
$_SESSION[$k]['product_price']=$_REQUEST['product_price_value'];
$_SESSION[$k]['shop_name']=$_REQUEST['shop_name_value'];
$_SESSION[$k]['Quantity']=$_REQUEST['selquantity'];
$_SESSION[$k]['color']=$_REQUEST['txtcolor'];
$_SESSION[$k]['Size']=$_REQUEST['selsize'];
$_SESSION[$k]['Product_Type']=$_REQUEST['selproducttype'];
$_SESSION[$k]['Remarks']=$_REQUEST['Remarks'];
$_SESSION[$k]['final_price']=$_REQUEST['final_price_value'];
$_SESSION[$k]['txturl']=$_REQUEST['txturl'];

}else {
    echo "else part";
    $_SESSION['id'] = 1;
    // store session data
    $_SESSION[$k]['product_name']=$_REQUEST['product_name_value'];
    $_SESSION[$k]['product_price']=$_REQUEST['product_price_value'];
    $_SESSION[$k]['shop_name']=$_REQUEST['shop_name_value'];
    $_SESSION[$k]['Quantity']=$_REQUEST['selquantity'];
    $_SESSION[$k]['color']=$_REQUEST['txtcolor'];
    $_SESSION[$k]['Size']=$_REQUEST['selsize'];
    $_SESSION[$k]['Product_Type']=$_REQUEST['selproducttype'];
    $_SESSION[$k]['Remarks']=$_REQUEST['Remarks'];
    $_SESSION[$k]['final_price']=$_REQUEST['final_price_value'];
    $_SESSION[$k]['txturl']=$_REQUEST['txturl'];
}

I'm trying to add these product details into array. thanks

Don't repeat yourself ;) means if you have two lines of code that are identical check if you really need to write it twice!

if(!isset($_SESSION['cart'])) {
  // create cart
  $_SESSION['cart'] = array();
}

// create item
$item = array();

// fill item

$item['product_name']=$_REQUEST['product_name_value'];
$item['product_price']=$_REQUEST['product_price_value'];
$item['shop_name']=$_REQUEST['shop_name_value'];
$item['Quantity']=$_REQUEST['selquantity'];
$item['color']=$_REQUEST['txtcolor'];
$item['Size']=$_REQUEST['selsize'];
$item['Product_Type']=$_REQUEST['selproducttype'];
$item['Remarks']=$_REQUEST['Remarks'];
$item['final_price']=$_REQUEST['final_price_value'];
$item['txturl']=$_REQUEST['txturl'];

// add item to cart

$_SESSION['cart'][] = $item;

You should first do some factorization on your code with creating a function to add the data to the $_SESSION .

Then, it's impossible to give you an answer, there's no "best way", there's only a way that suits your need and you're the only one which can find it.

见下文。

$sql = "INSERT INTO ordertb (time, cust_id, prod_id, quantity, pmt_mode, city, delivery_state,amtval) VALUES ( '".date("Y-m-d H:i:s")."','".$idval1."','".$item["product_code"]."','".$item["qty"]."','".$_SESSION['spmt1']."','".$_SESSION['scity']."','1','".$item["subtotal"]."')";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM