2016-06-20 34 views
2

我想用簡單的PHP和MySQL代碼創建購物車這裏是一個代碼來查看購物車,我想看到在一個表中的項目和刪除按鈕爲這裏每個 我創建了一個車,SEESION有我想要一個簡單的購物車所以誰能告訴我怎樣可以在每個項目上顯示一個刪除按鈕,也顯示在購物車購物車顯示項目與每個人刪除按鈕

<?php 
 
session_start(); 
 
include_once("config.php"); 
 

 

 

 

 
if(empty($_SESSION['cart'])) 
 
    $_SESSION['cart'] = array(); 
 
if(isset($_POST['product_code'])){ 
 
\t \t $product_code = $_POST["product_code"]; 
 
\t \t $product_name = $_POST["product_name"]; 
 
\t \t $product_qty = $_POST["product_qty"]; 
 
\t \t $product_price = $_POST["price"]; 
 
\t \t $product_color = $_POST["product_color"]; 
 
\t \t $_SESSION['cart'][] = array('name' => $product_name, 'product_code' => $product_code, 'qnty' => $product_qty, 'color' =>$product_color, 'price' => $product_price); 
 
\t \t 
 
\t \t 
 
\t \t 
 
} 
 

 
print_r ($_SESSION['cart']);/*header("Location: view_cart.php");*/ 
 

 
     if(isset($_POST['empty']) ){ 
 
     
 
      unset($_SESSION["cart"]); 
 
\t \t \t 
 
\t } 
 
\t  
 
\t  
 
?> 
 
<html> 
 
<head> 
 
</head> 
 
<body> 
 
<form method="POST" action="cart.php"><div align="center"><button type="remove" name="empty" >empty</button></div> 
 
<a href="ppage.php">back </a> 
 
<table width="100%" cellpadding="6" cellspacing="0"><thead><tr><th>Quantity</th><th>Name</th><th>Price</th><th>code</th><th>colour</th><th>remove</th></tr></thead> 
 
    <tbody> 
 

 
<?php 
 

 

 

 
foreach($_SESSION['cart'] as $itm=>$val) 
 
     \t echo "<thead>"; 
 
\t \t echo"<tr>"; 
 
\t \t \t echo '<td>'.$val['$qnty'].'</td>'; 
 
\t \t \t echo '<td>'.$val['name'].'</td>'; 
 
\t \t \t echo '<td>'.$val['price'].'</td>'; 
 
\t \t \t echo '<td>'.$val['product_code'].'</td>'; 
 
\t \t \t echo '<td>'.$val['color'].'</td>'; 
 
\t \t \t /*echo '<td>'<a href="?remove=<?php echo $itm; ?>">remove</a>'</td>' 
 
\t \t \t \t */ 
 
\t \t \t \t 
 
\t \t \t \t if (isset($_POST['remove'])){ 
 
\t 
 
\t \t \t \t unset($_SESSION['cart'][$_POST['remove']]);} 
 
\t \t \t /*echo \t <div align="right"><button type="remove" name="remove" >remove</button></div> */ 
 
\t \t \t echo '</tr>'; 
 
\t \t \t echo "<thead>"; 
 

 
\t 
 
\t \t \t 
 
\t \t ?> \t 
 
echo \t <div align="right"><button type="remove" name="remove" >remove</button></div> 
 
\t \t \t \t echo '<td>'<a href="?remove=<?php echo $itm; ?>">remove</a>'</td>' 
 
\t \t \t \t \t 
 
</form> 
 
</tbody> 
 
</body> 
 
</html>
項目

+0

請您說明使用標點符號。當每一句話都在一個句子中時,真的很難讀懂。 –

+0

現在我不能編輯的問題 –

回答

1

那裏沒有必要創建任何種類的form,只是c reate於各行的超鏈接和數組索引值追加到它,這樣的:

echo '<td><a href="?remove=' . $itm . '">remove</a></td>'; 

和加工過程中,簡單地使用$_GET超全局趕上數組索引值,然後從$_SESSION['cart']刪除的項目陣列,像這樣:

if(isset($_GET['remove']) && (!empty($_GET['remove'] || $_GET['remove'] == 0))){ 
    unset($_SESSION['cart'][$_GET['remove']]); 
} 

所以,你的代碼應該是這樣的:

<?php 
    // Start session 
    session_start(); 

    // Declare $_SESSION['cart'] as an empty array 
    if(empty($_SESSION['cart'])){ 
     $_SESSION['cart'] = array(); 
    } 

    // Store all product details in $_SESSION['cart'] array 
    if(isset($_POST['product_code'])){ 
     $product_code = $_POST["product_code"]; 
     $product_name = $_POST["product_name"]; 
     $product_qty = $_POST["product_qty"]; 
     $product_price = $_POST["price"]; 
     $product_color = $_POST["product_color"]; 
     $_SESSION['cart'][] = array('name' => $product_name, 'product_code' => $product_code, 'qnty' => $product_qty, 'color' =>$product_color, 'price' => $product_price); 
    } 

    // Check whether the URL parameter 'remove' is set or not 
    // If it's set and not empty, then delete that item array from the $_SESSION['cart'] 
    if(isset($_GET['remove']) && (!empty($_GET['remove'] || $_GET['remove'] == 0))){ 
     unset($_SESSION['cart'][$_GET['remove']]); 
    } 

?> 
<html> 
    <head> 

    </head> 
    <body> 

     <table width="100%" cellpadding="6" cellspacing="0" border="1" style="text-align:center;"> 
     <thead> 
      <tr> 
       <th>Quantity</th> 
       <th>Name</th> 
       <th>Price</th> 
       <th>code</th> 
       <th>colour</th> 
       <th>remove</th> 
      </tr> 
     </thead> 
     <tbody> 
     <?php 
     // Check whether the cart is empty or not 
     if(count($_SESSION['cart'])){ 
      // Loop through the $_SESSION['cart'] array 
      // and display the item details and 'remove' hyperlink 
      // for each row 
      foreach($_SESSION['cart'] as $itm=>$val){ 
       echo '<tr>'; 
       echo '<td>'.$val['qnty'].'</td>'; 
       echo '<td>'.$val['name'].'</td>'; 
       echo '<td>'.$val['price'].'</td>'; 
       echo '<td>'.$val['product_code'].'</td>'; 
       echo '<td>'.$val['color'].'</td>'; 
       echo '<td><a href="?remove=' . $itm . '">remove</a></td>'; 
       echo '</tr>'; 
      } 
     }else{ 
      echo '<tr><td colspan="6">No item in the cart</td></tr>'; 
     } 
     ?>        
     </tbody> 
     </table> 

    </body> 
</html> 
+0

謝謝....其很大的幫助,但你可以解釋一下代碼 –

+0

@SaurabhSharma當然!我已經更新了我的答案,並在必要時添加了評論。 –

+0

是啊,謝謝你也是@rajdeeppaul –

相關問題