以下のコードを使用していて、セッション配列の数量を増やしたいのですが、機能しません
if(!empty($_SESSION['quote']))
{
$prdata=$_SESSION['quote'];
}
$product_id = $data['id'];
$product_image = $data['image'];
$product_name = $data['pname'];
$product_quantity = $data['quantity'];
$product_price = $data['price'];
foreach ($_SESSION["quote"] as $cart_itm){
if($cart_itm['product_id'] == $product_id){ //the item exist in array
$newdat = array('product_id' => $product_id,'quantity'=>$product_quantity,'price'=>$product_price,'name'=>$product_name,'image'=>$product_image);
$found = true;
}else{
$newdat = array('product_id' => $product_id,'quantity'=>$product_quantity,'price'=>$product_price,'name'=>$product_name,'image'=>$product_image);
}
product_id
再度置く必要はなく、量を変えるだけです。コードはより短く、より明確になり、他の考えられるタイプミスなどを回避できます。
if($cart_itm['product_id'] == $product_id) {
$cart_itm['quantity'] += $product_quantity; // expect in $product_quantity is the amount you want to add to the current one
// it's a shorter variant of $cart_itm['quantity'] = $cart_itm['quantity'] + $product_quantity;
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加