Dynamic shopping cart in PHP

 Creating a dynamic shopping cart in PHP involves several steps. In this example, I'll outline a basic implementation of a shopping cart system using PHP and MySQL. This example assumes you have a basic understanding of PHP, MySQL, HTML, and CSS.

Step 1: Set Up Your Database

Create a MySQL database to store information about products and the shopping cart. Here's a simple database schema:

sql code

CREATE TABLE products (

    id INT AUTO_INCREMENT PRIMARY KEY,

    name VARCHAR(255) NOT NULL,

    price DECIMAL(10, 2) NOT NULL

);


CREATE TABLE cart (

    id INT AUTO_INCREMENT PRIMARY KEY,

    product_id INT NOT NULL,

    quantity INT NOT NULL,

    FOREIGN KEY (product_id) REFERENCES products(id)

);

Step 2: Create a Product Listing Page

Create a PHP page that lists your products. This page will display a list of products with an "Add to Cart" button for each product.

php code

<?php

// Connect to your MySQL database here

// Query to fetch products from the database

$query = "SELECT * FROM products";

$result = mysqli_query($connection, $query);

if (!$result) {

    die("Database query failed.");

}

?>

<!DOCTYPE html>

<html>

<head>

    <title>Product List</title>

</head>

<body>

    <h1>Product List</h1>

    <table>

        <tr>

            <th>Name</th>

            <th>Price</th>

            <th>Action</th>

        </tr>

        <?php while ($row = mysqli_fetch_assoc($result)) { ?>

            <tr>

                <td><?= $row['name'] ?></td>

                <td><?= $row['price'] ?></td>

                <td>

                    <form method="post" action="add_to_cart.php">

                        <input type="hidden" name="product_id" value="<?= $row['id'] ?>">

                        <input type="number" name="quantity" value="1" min="1">

                        <input type="submit" value="Add to Cart">

                    </form>

                </td>

            </tr>

        <?php } ?>

    </table>

</body>

</html>

Step 3: Create the "Add to Cart" Logic

Create a PHP script (add_to_cart.php) that handles adding products to the shopping cart.

php code

<?php

// Connect to your MySQL database here

if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    $product_id = $_POST['product_id'];

    $quantity = $_POST['quantity'];

    // Check if the product is already in the cart

    $cart_query = "SELECT * FROM cart WHERE product_id = $product_id";

    $cart_result = mysqli_query($connection, $cart_query);

    if (mysqli_num_rows($cart_result) > 0) {

        // Product is already in the cart, update the quantity

        $cart_row = mysqli_fetch_assoc($cart_result);

        $new_quantity = $cart_row['quantity'] + $quantity;

        $update_query = "UPDATE cart SET quantity = $new_quantity WHERE product_id = $product_id";

        mysqli_query($connection, $update_query);

    } else {

        // Product is not in the cart, insert a new record

        $insert_query = "INSERT INTO cart (product_id, quantity) VALUES ($product_id, $quantity)";

        mysqli_query($connection, $insert_query);

    }

}

header("Location: product_list.php");

exit();

Step 4: Create a Shopping Cart Page

Create a PHP page (cart.php) that displays the contents of the shopping cart and allows users to update or remove items.

php code

<?php

// Connect to your MySQL database here

// Fetch items from the cart

$cart_query = "SELECT products.id, products.name, products.price, cart.quantity

               FROM products

               INNER JOIN cart ON products.id = cart.product_id";

$cart_result = mysqli_query($connection, $cart_query);

if (!$cart_result) {

    die("Database query failed.");

}

?>

<!DOCTYPE html>

<html>

<head>

    <title>Shopping Cart</title>

</head>

<body>

    <h1>Shopping Cart</h1>

    <table>

        <tr>

            <th>Name</th>

            <th>Price</th>

            <th>Quantity</th>

            <th>Total</th>

            <th>Action</th>

        </tr>

        <?php while ($cart_row = mysqli_fetch_assoc($cart_result)) { ?>

            <tr>

                <td><?= $cart_row['name'] ?></td>

                <td><?= $cart_row['price'] ?></td>

                <td>

                    <form method="post" action="update_cart.php">

                        <input type="hidden" name="product_id" value="<?= $cart_row['id'] ?>">

                        <input type="number" name="quantity" value="<?= $cart_row['quantity'] ?>" min="1">

                        <input type="submit" value="Update">

                    </form>

                </td>

                <td><?= $cart_row['price'] * $cart_row['quantity'] ?></td>

                <td>

                    <form method="post" action="remove_from_cart.php">

                        <input type="hidden" name="product_id" value="<?= $cart_row['id'] ?>">

                        <input type="submit" value="Remove">

                    </form>

                </td>

            </tr>

        <?php } ?>

    </table>

</body>

</html>

Step 5: Create Logic for Updating and Removing Items

Create PHP scripts (update_cart.php and remove_from_cart.php) to handle updating the cart and removing items from the cart. These scripts are similar to the add_to_cart.php script but with appropriate logic for updating and removing items.

This is a basic example of creating a dynamic shopping cart in PHP. You can expand and enhance this system with features like user authentication, checkout, and payment processing to build a complete e-commerce solution. Additionally, consider using frameworks and libraries to simplify the development process and enhance security.

Comments

Popular posts from this blog

Explain the purpose of the MYSQL CASE statement

How to connect to a URL in PHP?

Does PHP support multiple inheritances? Explain with Examples

Master-slave replication in MYSQL