2012-03-06 29 views
0

私はPHPショッピングカートを作っています。ホーム画面にすべての商品が表示されているので、それらをクリックしてあなたのバスケットに追加することができますカート画面。これは、1つの製品のみを表示しますが、追加された製品の数が異なる場合でも、すべてが異なるタイトルを持っていても表示されます。総価格と数量の仕事は、その唯一の製品名ではありません。これは私のコードです。PHPショッピングカートに数多くの商品が表示されていません

<?php 
session_start(); 
include "connect.php"; 
include "header.php"; 

function viewcart(){ 
if (isset($_SESSION['cart'])){ //if shopping cart is not empty 
    $cart = $_SESSION['cart']; //store the cart array into a variable then display the content 
    echo "<table border=\"1\"><tr><th>Product Name</th><th>Quantity</th><th>Delete</th></tr>"; 
    foreach ($cart as $product=>$quantity){ 
     $q = "SELECT ID, Name FROM Products"; 
     $result = mysqli_query($_SESSION['conn'],$q); 
     $row = mysqli_fetch_array($result); 
     $product_id = $row[0]; 
     echo "<tr><td>$product</td><td>$quantity</td><td><a href=\"?action=delete&product=$product_id\">-</a></td></tr>"; 
     mysqli_free_result($result); 
    } 
    echo "</table>"; 
    subtotal($cart); //display the subtotal 
} else { //if shopping cart is empty 
    echo "<p>There is no product in your shopping cart.</p>"; 
} 


} 

function addproduct($product_id, $product_qty){ 

$q = "SELECT ID, Name FROM Products"; 
$result = mysqli_query($_SESSION['conn'],$q) 
or die("Error: ".mysqli_error($_SESSION['conn'])); 
$row = mysqli_fetch_array($result); 
$product_name = $row[1]; //get the product name from product id because it is better to display name than id in the cart 
if (isset($_SESSION['cart'])){ //if shopping cart is not empty 
    $cart = $_SESSION['cart']; 
    if (array_key_exists($product_name, $cart)){ //if the product exists, update quantity 
     $cart[$product_name] += $product_qty; 
    } 
    else { //otherwise, add new product-quantity pair to the array 
     $cart[$product_name]=$product_qty; 
    } 
$_SESSION['cart'] = $cart; //write the updated array back to session variable 
} 
else { //if shopping cart is empty 
    $cart = array($product_name=>$product_qty); //add product and quantity to the shopping cart 
    $_SESSION['cart'] = $cart; //write the updated array back 
} 
mysqli_free_result($result); 
} 

function deleteproduct($product_id, $product_qty){ 

$q = "SELECT Name FROM Products"; 
$result = mysqli_query($_SESSION['conn'],$q); 
$row = mysqli_fetch_array($result); 
$product_name = $row['Name']; 
if (isset($_SESSION['cart'])){ //if shopping cart is not empty 
    $cart = $_SESSION['cart']; 
    if (array_key_exists($product_name, $cart)){ //if the product exists, update quantity 
     $cart[$product_name] -= $product_qty; 
     if ($cart[$product_name] == 0){ //if the quantity is 0, delete the key 
      unset($cart[$product_name]); 
     } 
    } 
    else { //exception 
     echo "<p>Error!</p>"; 
    } 
    $_SESSION['cart'] = $cart; //write the updated array back to session variable 
} else { 
    echo "<p>Error!</p>"; 
} 
mysqli_free_result($result); 
} 

function emptycart(){ 

if (isset($_SESSION['cart'])){ //if shopping cart is not empty 
    unset($_SESSION['cart']); 
} 
else { 
    echo "<p>Error!</p>"; 
} 
} 


function subtotal($cart){ //calculate the total value of products in the shopping cart 
$total = 0; //initialise total 
if (!empty($cart)){ 
    foreach ($cart as $product => $quantity){ 
     $q = "SELECT ID, Price FROM Products"; 
     $result = mysqli_query($_SESSION['conn'],$q); 
     $row = mysqli_fetch_array($result); 
     $price = $row['Price']; 
     $total += $price * $quantity; 
    } 
    echo "<p>Total: $total | <a href=\"?action=empty\">Empty cart</a></p>"; 
} else { 
    unset($_SESSION['cart']); //do not need to keep an empty cart, so delete it 
    echo "<p>There is no product in your shopping cart.</p>"; 
} 
} 


if (isset($_GET['action'])){ 

if ($_GET['action']=='view'){ 

    viewcart(); 

} elseif ($_GET['action']=='add'){ 
    if (isset($_GET['product'])){ 
     $product_id = $_GET['product']; 
     $product_qty = 1; //default product value 
     addproduct($product_id, $product_qty); 
     viewcart(); 
     echo "<p><a href=\"javascript:history.go(-1)\">back</a></p>"; 
    } else { 
     echo "<p>There is an error! Are you trying to attack this little poor shopping cart?</p>"; 
    } 
} elseif ($_GET['action'] == 'delete'){ 
    if (isset($_GET['product'])){ 
     $product_id = $_GET['product']; 
     $product_qty = 1; //default product value 
     deleteproduct($product_id, $product_qty); 
     viewcart(); 
    } 
    else { 
     echo "<p>There is an error! </p>"; 
    } 
} elseif ($_GET['action']=='empty'){ 
    emptycart(); 
    viewcart(); 
} 

else { 
    echo "<p>There is an error! </p>"; 
} 
} 
else { 
    echo "<p>There is an error! </p>"; 
} 


include "footer.php"; 

?> 
+0

問題のあるセクションを見つけるには、 '$ product'変数を' foreach'ループ( 'viewCart'関数)にエコーしてみてください。 出力を更新してください。 –

+0

エラーが生じますか? – Starx

+0

質問のコードの上に '$ _SESSION ['cart']'にあるデータ形式の仕様を追加してください。 – hakre

答えて

0

あなたの質問には誤字があります。製品の製品IDを抽出したいと思うかもしれませんが、実際にはテーブル内のすべてを選択しています。

$q = "SELECT ID, Name FROM Products"; 

それは、以下の例のようなものを読んで、これは問題に

$q = "SELECT ID, Name FROM `Products` WHERE name='$product' "; 

を修正し、後であなたは私はあなたが知っている願ってい

$row = mysqli_fetch_array($result); 

として結果を読んでいるかどうかを確認するための更新つまり、このようにして$rowはテーブルから最初の行だけを取得します。

+0

これは変更されていません。どうもありがとうございます。 –

+0

@carlaDessi、あなたの質問に 'var_dump($ _ SESSION ['cart'])'を貼り付けてください。 – Starx

関連する問題