2016-06-20 16 views
2

シンプルなphpとmysqlコードでショッピングカートを作成したいのですが、カートを表示するコードが1つあります。ここで、各 iは、カートを作成し、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

あなたの説明に句読点を使用してください:

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

だからあなたのコードは次のようにする必要があります。すべてのものが1つの文に含まれているときに読むのは本当に難しいです。 –

+0

今質問を編集することはできません –

答えて

1

そこにはformの種類を作成する必要はありません。ちょうどc各列のハイパーリンクをreateし、それに配列インデックス値を追加し、このように:

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

、処理中に、単にこのように、$_SESSION['cart']からアイテム配列を削除し、その後$_GETスーパーグローバルを使用して、配列のインデックス値をキャッチし:

<?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 –

関連する問題