【发布时间】:2016-06-05 06:51:14
【问题描述】:
我想创建一个计费系统。该系统应在他们的个人资料上显示他们订阅的服务的 20 美元定期付款。
如果他们在帐户中添加新金额,则报表必须根据他们选择的服务更新新金额(例如 30 美元)。
我已经在 PHP 中创建了一个购物车,我知道如何将商品添加到我的购物车并进行结帐。我只是有点困惑如何让每个用户都独一无二。 添加到购物车。
<?php
session_start();
// Get the product id
$id = isset($_GET['id']) ? $_GET['id'] : "";
$name = isset($_GET['name']) ? $_GET['name'] : "";
$quantity = isset($_GET['quantity']) ? $_GET['quantity'] : "";
//Check if the cart array was created
//If it isn't, create the cart array
if(!isset($_SESSION['cart_items'])){
$_SESSION['cart_items'] = array();
}
//Check if the item is in the array, if it is, do not add
if(array_key_exists($id, $_SESSION['cart_items'])){
// redirect to product list and tell the user it was added to cart
header('Location: products.php?action=exists&id' . $id . '&name=' . $name);
}
//If not, then add the item to the array
else{
$_SESSION['cart_items'][$id]=$name;
//Redirects to product list
header('Location: products.php?action=added&id' . $id . '&name=' . $name);
}
?>
<?php
session_start();
$page_title="Cart";
include 'layout_head.php';
$action = isset($_GET['action']) ? $_GET['action'] : "";
$name = isset($_GET['name']) ? $_GET['name'] : "";
if($action=='removed'){
echo "<div class='Wow danger'>";
echo "<strong>{$name}</strong> was removed from your cart.";
echo "</div>";
}
else if($action=='quantity_updated'){
echo "<div class='Wow danger'>";
echo "<strong>{$name}</strong> quantity was updated.";
echo "</div>";
}
if(count($_SESSION['cart_items'])>0){
//Gets the Product Id's
$ids = "";
foreach($_SESSION['cart_items'] as $id=>$value){
$ids = $ids . $id . ",";
}
//Removes the comma
$ids = rtrim($ids, ',');
//Starts Table
echo "<table class='table table-hover table-responsive table-bordered'>";
// Table heading
echo "<tr>";
echo "<th class='textAlignLeft'>Product Name</th>";
echo "<th>Price (USD)</th>";
echo "<th>Action</th>";
echo "</tr>";
$query = "SELECT id, name, price FROM products WHERE id IN ({$ids}) ORDER BY name";
$stmt = $con->prepare( $query );
$stmt->execute();
$total_price=0;
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
extract($row);
echo "<tr>";
echo "<td>{$name}</td>";
echo "<td>${$price}</td>";
echo "<td>";
echo "<a href='remove_from_cart.php?id={$id}&name={$name}' class='btn btn-danger'>";
echo "<span class='shopping cart-remove'></span> Remove from cart";
echo "</a>";
echo "</td>";
echo "</tr>";
$total_price+=$price;
}
echo "<tr>";
echo "<td><b>Total</b></td>";
echo "<td>${$total_price}</td>";
echo "<td>";
echo "<a href='#' class='success'>";
echo "<span class='shopping-cart'></span> Checkout";
echo "</a>";
echo "</td>";
echo "</tr>";
echo "</table>";
}
else{
echo "<div class='Wow danger'>";
echo "<strong>No products found</strong> in your cart!";
echo "</div>";
}
include 'layout_foot.php';
?>
【问题讨论】: