基于Php会话的购物车

时间:2015-03-28 03:58:35

标签: php cart

我试图创建一个基于php会话的购物车,我能够做到但我现在的问题是如何记录我的电子商店中订购的每个产品的数量,我不知道知道如何做到这一点。所以,我希望你们能帮助我,谢谢......这就是我到目前为止所做的一切。

my products.php

<?php 
session_start();
$cart = $_SESSION['cart'];
$cart = count($cart);

include 'connection.php' ; 
$prod = mysqli_query($conn,'SELECT * FROM products');
$name = $_GET['name'];
$id = $_GET['id'];

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <script src="//use.typekit.net/aqc7mal.js"></script>
    <script>try{Typekit.load();}catch(e){}</script>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" href="style.css">
    <meta charset="UTF-8">
    <title><?php echo $header; ?></title>
</head>
<body>
    <nav>
        <ul>
            <li><a href="">Home</a></li>
            <li><a href="products.php">Products</a></li>
            <li><a href="">Blogs</a></li>
            <li><a href="cart.php">Cart <span class="badge"><?php  echo $cart ;?></span></a></li>
        </ul>
    </nav>
    <div class="content">
        <h2>PRODUCTS</h2>
        <hr>
            <?php
            $action = $_GET['action'];
            if ($action=='exists')
            {
                echo "<div class='alert alert-danger' id='alerts'>";
                echo "<span>" .$name. "&nbspalready in the cart  </span>";
                echo "</div>";
            }
            elseif ($action =='added') 
            {
                echo "<div class='alert alert-info' id='alerts'>";
                echo "<span>" .$name. "&nbspAdded to Cart  </span>";
                echo "</div>";
            }

        echo "<div class='table'>";
                echo "<table class='table table-hover table-responsive table-bordered'>";
                echo "<thead>";
                echo "<th>Product Name</th>";
                echo "<th>Price</th>";
                echo "<th>Description</th>";
                echo "<th>Quantity</th>";   
                echo "<th>Action</th>";
                echo "</thead>";
            while ($prods = $prod->fetch_array(MYSQL_ASSOC)) 
            {   
                $id = $prods['id'];
                $name = $prods['prod_name'];
                echo "<tr>";
                echo "<td>"  . $prods['prod_name'] . "</td>";
                echo "<td> $"   . $prods['price'] . "</td>";
                echo "<td>"  . $prods['description'] . "</td>";
                echo "<td></td>";
                echo "<td>"  .'<a class="btn btn-primary" href="addtocart.php?id='.$id.'&name='.$name.'">
                    <span class="glyphicon glyphicon-shopping-cart"></span>Add To Cart</a>'. "</td>";
                echo "</tr>";
            }
        echo "</table>";
            ?>
        </div>
    </div>
</body>
</html>

和我的add_to_cart.php

<?php

session_start();
$cart = $_SESSION['cart'];

$id = $_GET['id'];
$name = $_GET['name'];
$qty = $_GET['qty'];

var_dump($qty);

if (array_key_exists($id, $_SESSION['cart'])) {

    header('location:products.php?action=exists&id' . $id .'&name=' . $name);
}
else
{
    if (empty($_SESSION['cart'])) {
        $_SESSION['cart'] = array();
    } 
    array_push($_SESSION['cart'], $id);
    header('location:products.php?action=added&id' . $id . '&name=' . $name);
}
?>

谢谢你的家伙

0 个答案:

没有答案