2012-03-06 7 views
0

PHP 장바구니를 만들었습니다. 홈 화면에 모든 제품을 표시하고이를 클릭하여 장바구니에 추가 한 다음 쇼핑을 시작합니다 장바구니 화면. 이것은 하나의 제품 만 보여 주지만 다른 타이틀을 가지고 있더라도 추가 한 제품의 수량이 다양합니다. 총 가격과 수량 작업, 그 유일한 제품 이름 않습니다. 내 코드입니다.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

관련 문제