2016-06-20 3 views
2

Ich möchte Warenkorb mit einfachen PHP und MySQL-Code hier ist ein Code, um den Warenkorb anzuzeigen, und ich möchte das Element in einer Tabelle und mit einer Schaltfläche zum Entfernen für sehen jeder hier habe ich einen Wagen und seesion ist dort möchte ich ein einfaches Warenkorb so kann mir jemand sagen, wie ich eine Entfernen-Taste auf jedes Element anzeigen und auch den Artikel in den Warenkorb zeigenWarenkorb, die Artikel mit der Schaltfläche zum Entfernen für jede von ihnen zeigen

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

Bitte verwenden Sie Interpunktion in Ihrer Beschreibung. Es ist wirklich schwer zu lesen, wenn alles in einem Satz ist. –

+0

jetzt kann ich die Frage bearbeiten –

Antwort

1

Es gibt keine Notwendigkeit, irgendeine Art von form dort zu erstellen, nur c einen Hyperlink für jede Zeile reate und den Arrayindexwert, um es, wie diese anfügen:

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

Und während der Verarbeitung einfach den Arrayindexwert fangen $_GET superglobalen und anschließend aus den $_SESSION['cart'], wie dies der Element-Array löschen :

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

So sollte der Code wie folgt sein:

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

Dank Mann .... es ist eine große Hilfe, aber können Sie den Code ein wenig bitte erklären –

+0

@SaurabhSharma Sicher! Ich habe meine Antwort aktualisiert und bei Bedarf Kommentare hinzugefügt. –

+0

ja danke auch dafür @rajdeeppaul –

Verwandte Themen