2017-11-06 3 views
0

私は現在AngularJS、PHP、webservice、およびいくつかのhtml/cssとXMLを使ってショッピングアプリケーションを開発しています。既存のセッションでの計算の作成

私は現在カートセッションで作業しており、合計価格、合計アイテム数、およびVAT計算を作成する際に問題が発生しました。

セッションはPHPで書かれています。

<?php 
 

 
session_start(); 
 

 
$product_id = $_REQUEST['product_id']; 
 
$product_name = $_REQUEST['product_name']; 
 
$product_price = round($_REQUEST['product_price'], 2); 
 
$product_size = $_REQUEST['product_size']; 
 
$product_quantity = $_REQUEST['product_quantity']; 
 
$total_product_price = $product_price*$product_quantity; 
 
round($total_product_price, 2); 
 
$total_items = $_SESSION['cart'][$product_quantity]; 
 
// Add new item to session 
 
$cartProduct = array(
 
    "id" => $product_id, 
 
    "name" => $product_name, 
 
    "price" => $product_price, 
 
    "size" => $product_size, 
 
    "quantity" => $product_quantity, 
 
    "total_price" => $total_product_price, 
 
    "total_items" => $total_items 
 
); 
 

 
/* 
 
* check if the 'cart' session array was created 
 
* if it is NOT, create the 'cart' session array 
 
*/ 
 
if(!isset($_SESSION['cart'])){ 
 
    $_SESSION['cart'] = array(); 
 
} 
 

 
// check if the item is in the array, if it is, do not add 
 
if(array_key_exists($product_id and $product_size, $_SESSION['cart'])){ 
 
    // redirect to product list and tell the user it was added to cart 
 
    echo "<script> alert('Dit product staat al in de winkelwagen')</script>])"; 
 
} 
 

 
// else, add the item to the array 
 
else{ 
 
    $_SESSION['cart'][$product_id]=$cartProduct; 
 
}

を上記のコードは、製品に関するすべての情報が要求された状態として:コードは、次の通りです。 product_idのような要求されたアイテムはarray()に設定されます。 array()は、いくつかのチェックの後、セショーション:$_SESSION['cart'];に追加されます。カートを追加して見せても問題ありません。私が抱えている問題は、カート内のtotal_items、カートのtotal_price、およびVAT計算を表示するのに必要な計算を作成することです。

さらに詳しい情報が必要な場合は、お知らせください。

ありがとうございます!あなたが3$product_quantityを持っている場合

$_SESSION['cart'][$product_id]=$cartProduct; 

そして

$cartProduct = array(
    "id" => $product_id, 
    "name" => $product_name, 
    "price" => $product_price, 
    "size" => $product_size, 
    "quantity" => $product_quantity, 
    "total_price" => $total_product_price, 
    "total_items" => $total_items 
); 

だから、あなたが$product_idを引いているので、これは私が見る問題

$total_items = $_SESSION['cart'][$product_quantity]; 

であなたのコードを見てみると

+0

作成する前にセッションを使用している可能性がありますか? '$ total_items = $ _SESSION ['cart'] [$ product_quantity];' –

答えて

1

「3」は意味をなさない。

$_SESSION['cart'][6] = array(
    "id" => 6, //$product_id, 
    "name" => $product_name, 
    "price" => $product_price, 
    "size" => $product_size, 
    "quantity" => 3, //$product_quantity, 
    "total_price" => $total_product_price, 
    "total_items" => $total_items 
); 

、なぜ私たちは、次の量ではなく製品IDを引き出します:私たちは、その後36$product_id$product_quantityを持っている場合、例えばので。 $product_quantity$total_items間の目的や違いは何ですので、

$_SESSION['cart'][$product_quantity]の代わりに、ここで季節だけの事は

$total_items = $_SESSION['cart'][$product_id]['total_items']; 

ですが、私はあなたにも他の問題を持っているかもしれないと思いますか?合計品目は製品の数量ではありません。または、1つは総製品数(一意の製品IDの数)であり、1つはその特定の製品(1つの製品IDの数)です。この場合、各製品の全製品数を追跡する必要はなく、エラーが発生しやすくなります。

あなたはまた、カートに製品を割り当てる前に、潜在的にセッションにアクセスして、ここで

$total_items = $_SESSION['cart'][$product_quantity]; 

これはおそらく間違っている

すぎ

array_key_exists($product_id and $product_size, $_SESSION['cart']) 

同様のものをテストするとき、私はこれを取得AS

警告:array_key_exists():最初の引数は文字列または整数である必要があります

これはおそらく$product_id and $product_sizetrueとブール比較しているためです。

私の評価。

+0

私は総額ではなく、異なるIDの総額を取得したいと思います。私は 'array_key'を知っていましたが、私の質問とは何か関係がないので、このケースでは問題が見えませんでした。私の質問です:どのように私は別の製品のIDを数え、それらを表示することができます:あなたは..カート内のアイテム。そして、カート価格とVATの合計金額を計算したいと思います。それが私の問題です。セッションについてのヒントをありがとうございます。そのうちのいくつかは非常に便利ですが、それはありません;私の質問に答えてください – Deathstorm

関連する問題