2017-05-22 2 views
-1

Ich bin derzeit Aufbau einer Online-Shopping für meine Aufgabe. Ich habe alles ziemlich in Betrieb, aber ich möchte meinen Warenkorb funktional verbessern. Jetzt kann es nur ein Element zum Warenkorb hinzufügen und wenn ich versuche, das gleiche Element hinzuzufügen, wird es sagen, dass bereits existiert, möchte ich fragen, ob jemand weiß, wie ich diesen Code von diesem Original ändern, stattdessen mehr in den Warenkorb hinzufügen zu sagen, dass der Gegenstand bereits existiert. Vielen Dank im Voraus und schätzen wirklich alle Kommentare ^^“ PS. Unter meinem Code.Update Warenkorb mit PHP + MYSQL (PDO-Verbindung)

<?php require '../ppuyakul/php/userIndex.php'; require 
'../ppuyakul/php/db_conn.php'; 

$msg ='PLEASE SELECT ITEM'; 

if(isset($_POST["add_to_cart"])): 
    if(isset($_SESSION["shopping_cart"])): 
     $item_array_id = array_column($_SESSION["shopping_cart"], "item_id"); 
     if(!in_array($_GET["id"], $item_array_id)):    
       $count = count($_SESSION["shopping_cart"]); 
       $item_array = array( 
        'item_id'=>$_GET["id"], 
        'item_name'=>$_POST["hidden_name"], 
        'item_price'=>$_POST["hidden_price"], 
        'item_quantity'=>$_POST["quantity"] 
      ); 
       $_SESSION["shopping_cart"][$count] = $item_array; 
       $msg = 'PRODUCT ADDED';  
     else: 
       $msg = 'PRODUCT ALREADY EXISTS'; 

     endif; 
    else: 
     $item_array = array( 
       'item_id'=>$_GET["id"], 
       'item_name'=>$_POST["hidden_name"], 
       'item_price'=>$_POST["hidden_price"], 
       'item_quantity'=>$_POST["quantity"] 
     ); 
     $_SESSION["shopping_cart"][0] = $item_array; 
    endif; 
endif; 

if(isset($_GET["action"])): 
    if($_GET["action"] == "delete"):   
     foreach($_SESSION["shopping_cart"] as $keys => $values):    
       if($values["item_id"] == $_GET["id"]):     
        unset($_SESSION["shopping_cart"][$keys]); 
        $msg = 'PRODUCT REMOVED'; 
       endif; 
     endforeach; 
    endif; 
endif; ?> 

Antwort

1

Statt $count als Ihren Warenkorb des Arrays verwenden Verwenden Sie die Produkt-IDs. Ich würde Ihren Code auf diese Weise wiederholen:

if (isset($_POST["add_to_cart"])): 
    $new_item_id = $_GET["id"]; 
    $item_array = [ 
     'item_id' => $new_item_id, 
     'item_name' => $_POST["hidden_name"], 
     'item_price' => $_POST["hidden_price"], 
     'item_quantity' => $_POST["quantity"] 
    ]; 
    if (isset($_SESSION["shopping_cart"]) && isset($_SESSION["shopping_cart"][$new_item_id])): 
     $_SESSION["shopping_cart"][$new_item_id]['item_quantity'] += $item_array['item_quantity']; 
     $msg = 'PRODUCT ALREADY IN CART, QTY INCREASED'; 
    else: 
     $_SESSION["shopping_cart"][$new_item_id] = $item_array; 
     $msg = 'PRODUCT ADDED'; 
    endif; 
endif; 

if (isset($_GET["action"]) && $_GET["action"] == "delete"): 
    foreach ($_SESSION["shopping_cart"] as $item_id => $item): 
     if ($item_id == $_GET["id"]): 
      unset($_SESSION["shopping_cart"][$item_id]); 
      $msg = 'PRODUCT REMOVED'; 
     endif; 
    endforeach; 
endif; 

Schließlich, wenn Sie durch alle Grundlagen bekommen, verwandeln Sie dies in OOP wie erwähnt.

+0

Vielen Dank für Sie, es wird mir helfen, mehr aus dem bereitgestellten Code lernen beantworten =) –

+0

@CodeGoide haben Dieser Fehler "Schwerwiegender Fehler: Isset() kann nicht auf das Ergebnis eines Ausdrucks (verwenden Sie stattdessen" null! == Ausdruck ") in C: \ xampp \ htdocs \ ppuyakul \ php \ cart.php Zeile 24 " –

+0

Ah ja, ich hatte eine Klammer in der delete' if' Anweisung. Ich habe meine Antwort aktualisiert. Schau es dir an, wenn du kannst. Wie ist die OOP (Klassen/Funktionen) gelaufen, hast du sie bekommen? Ich würde Ich habe dir auch die OOP-Antwort gegeben, aber ich denke, du musst das Konzept erst richtig machen. (Revision: https://stackoverflow.com/posts/441048 99/Revisionen) – CodeGodie

1

Ich persönlich würde den id Wert den Schlüsselnamen machen, es wäre einfacher, abzurufen und zu bearbeiten. I würde auch ein paar Klassen zu helfen Ihr Skript mehr Menschen lesbaren machen die addToCart() Methode Siehe zu zeigen, wie in den Warenkorb hinzufügen, wo das Element bereits gesetzt ist.

# I would create a general class to help fetch some basic elements 
class App 
    { 
     # Easily extract $_POST array and values 
     public function getPost($key=false,$default=false) 
      { 
       if(!empty($key)) 
        return (isset($_POST[$key]))? $_POST[$key] : $default; 

       return $_POST; 
      } 
     # Easily extract $_GET array and values 
     public function getGet($key=false,$default=false) 
      { 
       if(!empty($key)) 
        return (isset($_GET[$key]))? $_GET[$key] : $default; 

       return $_GET; 
      } 
     # Easily extract $_SESSION array and values 
     public function getSession($key=false,$default=false) 
      { 
       if(!empty($key)) 
        return (isset($_SESSION[$key]))? $_SESSION[$key] : $default; 

       return $_SESSION; 
      } 
    } 
# This is your shopping cart class that extends the basic class 
class ShoppingCart extends App 
    { 
     # Fetches an item in cart, if item is actually in the cart 
     public function getItem($id) 
      { 
       return (isset($_SESSION['shopping_cart'][$id]))? $_SESSION['shopping_cart'][$id] : false; 
      } 
     # Adds to the cart 
     public function addToCart($id,$name,$price,$qty=1) 
      { 
       if(isset($_SESSION['shopping_cart'][$id])) 
        $_SESSION['shopping_cart'][$id]['item_quantity'] += $qty; 
       else { 
        $_SESSION['shopping_cart'][$id]['item_name']  = $name; 
        $_SESSION['shopping_cart'][$id]['item_price']  = $price; 
        $_SESSION['shopping_cart'][$id]['item_quantity'] = $qty; 
       } 
      } 
     # Removes from the cart 
     public function removeFromCart($id) 
      { 
       if(isset($_SESSION['shopping_cart'][$id])) 
        unset($_SESSION['shopping_cart'][$id]); 
      } 
     # Checks if the action is set to add 
     public function isAdding() 
      { 
       return (isset($_POST["add_to_cart"])); 
      } 
     # Checks if the action is set to remove 
     public function isRemoving() 
      { 
       return ($this->getGet("action") == 'delete'); 
      } 
     # Just start the cart if not already started 
     public function startCart() 
      { 
       if(!isset($_SESSION['shopping_cart'])) 
        $_SESSION['shopping_cart'] = array(); 
      } 
    } 
# Messaging 
$msg = ''; 
# Create the cart instance 
$Cart = new ShoppingCart(); 
# If adding to cart 
if($Cart->isAdding()) { 
    # Start the cart array 
    $Cart->startCart(); 
    # Add to the cart 
    $Cart->addToCart(
     $Cart->getGet("id"), 
     $Cart->getPost("hidden_name"), 
     $Cart->getPost("hidden_price"), 
     $Cart->getPost("quantity",1) 
    ); 
    # Set messaging 
    $msg = 'PRODUCT ADDED'; 
} 
# Check if the action is deleting 
elseif($Cart->isRemoving()) { 
    # Remove the id from the cart 
    $Cart->removeFromCart($Cart->getGet("id")); 
    # Create messaging 
    $msg = 'PRODUCT REMOVED'; 
} 
+0

Dank für Sie so viel helfen, Code und Kommentar einen Code innerhalb ich es zu folgen versuchen ^^“ –