2014-06-24 4 views
0

저는 PHP를 처음 사용하고 제 웹 사이트의 장바구니를 만들고 있습니다. 문제는 "쇼핑 카트에 추가"버튼을 클릭 할 때입니다. cart.php 페이지로 이동하여 제품 이름, 세부 정보, 가격, 조정할 수량, 총 가격을 표시하려고하지만 항목을 제거하려고합니다. 제품에 대한 모든 세부 정보를 표시하지 않고 수량 만 표시합니다. 다음은 장바구니 제품을 표시하는 코드입니다. 제품을 보여장바구니에 PHP 보이기가 없습니다 제품 세부 사항

if (isset($_POST['pid'])) { 
    $pid = $_POST['pid']; 
    $wasFound = false; 
    $i = 0; 

    // If the cart session variable is not set or cart array is empty 
    if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) { 
    // RUN IF THE CART IS EMPTY OR NOT SET 
    $_SESSION["cart_array"] = array(0 => array("item_id" => $pid, "quantity"=> 1)); 
} else { 
    // RUN IF THE CART HAS AT LEAST ONE ITEM IN IT 
    foreach ($_SESSION["cart_array"] as $each_item) { 
     $i++; 
     while (list($key, $value) = each($each_item)) { 

      if ($key == "item_id" && $value == $pid) { 

      array_splice($_SESSION["cart_array"], $i-1, 1, 
      array(array("item_id" => $pid, "quantity" => $each_item['quantity'] + 1))); 
      $wasFound = true; 
      } // close if condition 
     } // close while loop 
    } // close foreach loop 

    if ($wasFound == false) { 
     array_push($_SESSION["cart_array"], array("item_id" => $pid, "quantity" => 1)); 
    } 
} 

header("location: cart.php"); 
exit(); 
} 

을 (사용자가 제품 페이지에서 장바구니에 무언가를 추가하려고하면)

$cartOutput=""; 
$cartTotal=""; 
if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) 
{ 
    $cartOutput = "<h2 align='center'>Your shopping cart is empty</h2>"; 
} 
else 
{ 
    $i=0; 
    foreach($_SESSION["cart_array"] as $each_item) 
     { 
     $i++; 
      $item_id = $each_item['item_id']; 
     $result = mysqli_query($con, "SELECT * FROM products WHERE id='$item_id' LIMIT 1 "); 
     if($result == FALSE) { 
      die(mysql_error()); // TODO: better error handling 
    } 
    while($row = mysqli_fetch_array($result)) 
     { 
      $id = $row["id"]; 
      $product_name = $row["product_name"]; 
      $price = $row["price"]; 
      $details = $row["details"]; 
     } 
     $pricetotal = $price * $each_item['quantity']; 
     $cartTotal = $pricetotal + $cartTotal; 
     // Dynamic Table row assembly 
     $cartOutput .= '<tr>'; 
     $cartOutput .= '<td align=\'center\' >' . $product_name . '<br/></td>'; 
     $cartOutput .= '<td align=\'center\' >' . $details . '</td>'; 
     $cartOutput .= '<td align=\'center\' >' . $price . '</td>'; 
     $cartOutput .= '<td> 
      <form action="cart.php" method="post"> 
      <input name="quantity" type="text" value="' . $each_item['quantity'] . '" size="1" maxlength="2" /> 
      <input name="adjustBtn' . $item_id . '" type="submit" value="change" /> 
      <input name="item_to_adjust" type="hidden" value="' . $item_id . '" /> 
      </form> 
      </td>'; 
     //$cartOutput .= '<td align=\'center\' >' . $each_item['quantity'] . '</td>'; 
     $cartOutput .= '<td align=\'center\' >' . $pricetotal . '</td>'; 
     $cartOutput .= '<td> <form action="cart.php" method="post"> 
      <input name="deleteBtn' . $item_id . '" type="submit" value="X" /> 
      <input name="index_to_remove" type="hidden" value="' . $i . '" /></form> </td>'; 
     $cartOutput .= '</tr>'; 
    $i++; 

    } 

} 

$dynamicList = ""; 

$result = mysqli_query($con,"SELECT * FROM products "); 
$productCount = mysqli_num_rows($result); // count the output amount 




if ($productCount > 0) { 
while($row = mysqli_fetch_array($result)){ 
       $id = $row["id"]; 
     $product_name = $row["product_name"]; 
     $product_code = $row["product_code"]; 
     $availability = $row["availability"]; 
     $price = $row["price"]; 
     $category = $row["category"]; 
     $subcategory = $row["subcategory"]; 
     $details = $row["details"]; 
     $date_added = strftime("%b %d, %Y", 
strtotime($row["date_added"])); 
     // To show latest items on the home page 

     $dynamicList .= ' <div class="product-items"> 
    <div class="product-block"> 
    <div class="product-block-inner"> 

       <div class="image"> 
        <a href="product.php?id=' . $id . '"><img 
src="product_images/' . $id . '.jpg" /> </a> 
       </div> 

       <div class="name"> 
        <a href="product.php?id=' . $id . '">' . 
$product_name . '</a> 
       </div> 
       <div class="price"> 
        RS. ' . $price . ' 
        <br /> 
        Availability: ' . $availability . ' 
       </div> 
       <div class="cart"> 
        <form id="form1" name="form1" 
method="post" action="cart.php"> 
         <input type="input" name="pid" id="pid" 
value="<?php echo $id; ?>" /> 
         <input type="submit" name="button" 
id="button" class="button" value="Add to Shopping Cart" /> 
         </form> 

       </div> 
</div> 
</div> 
</div> 






         '; 


} 
} else { 
$dynamicList = "We have no products listed in our store yet"; 

} 
+1

왜 PHP를 처음 사용하는 사람이라면 * 작게 시작하지 마십시오. 전자 시스템은 상점 시스템이 좋은 시작 프로젝트가 아닙니다. – DanFromGermany

+0

그래서 DanFromGermany를 더 잘 들어야합니다. – Liberat0r

+0

나는 당신이하고있는 일을 이해하고 어떤 특정한 문제가없는 한 다른 사람들에게 당신의 일을하도록 요구하는 것을 제안합니다. – Liberat0r

답변

0

노력이 하나

<?php 
    $cartOutput=""; 
    $cartTotal=""; 
    if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) 
    { 
    $cartOutput = "<h2 align='center'>Your shopping cart is empty</h2>"; 
    } 
    else 
    { 
    $i=0; 
    foreach($_SESSION["cart_array"] as $each_item) 
     { 
     $i++; 
      $item_id = $each_item['item_id']; 
     $result = mysqli_query($con, "SELECT * FROM products WHERE id='$item_id' 
    LIMIT 1 "); 
     if($result == FALSE) { 
      die(mysql_error()); // TODO: better error handling 
     } 
     $row = mysqli_fetch_array($result); 

       $id = $row["id"]; 
       $product_name = $row["product_name"]; 
       $price = $row["price"]; 
       $details = $row["details"]; 

      $pricetotal = $price * $each_item['quantity']; 
      $cartTotal = $pricetotal + $cartTotal; 
      // Dynamic Table row assembly 
      $cartOutput .= '<tr>'; 
      $cartOutput .= '<td align=\'center\' >' . $product_name . '<br 
    /></td>'; 
      $cartOutput .= '<td align=\'center\' >' . $details . '</td>'; 
      $cartOutput .= '<td align=\'center\' >' . $price . '</td>'; 
      $cartOutput .= '<td> 
      <form action="cart.php" method="post"> 
    <input name="quantity" type="text" value="' . $each_item['quantity'] . '" size="1" 
    maxlength="2" /> 
    <input name="adjustBtn' . $item_id . '" type="submit" value="change" /> 
    <input name="item_to_adjust" type="hidden" value="' . $item_id . '" /> 
    </form> 
    </td>'; 
    //$cartOutput .= '<td align=\'center\' >' . $each_item['quantity'] . '</td>'; 
    $cartOutput .= '<td align=\'center\' >' . $pricetotal . '</td>'; 
    $cartOutput .= '<td> <form action="cart.php" method="post"> 
     <input name="deleteBtn' . $item_id . '" type="submit" value="X" /> 
     <input name="index_to_remove" type="hidden" value="' . $i . '" /></form> </td>'; 
    $cartOutput .= '</tr>'; 
    $i++; 


     } 

    } 

    ?> 
관련 문제