在php中如何在单独的选项卡中显示购物车页面?
这是我的index.php:在php中如何在单独的选项卡中显示购物车页面?,php,Php,这是我的index.php: <?php session_start(); require_once("dbcontroller.php"); $db_handle = new DBController(); if(!empty($_GET["action"])) { switch($_GET["action"]) { case "add": if(!empty($_POST["quantity"])) { $productByCode =
<?php
session_start();
require_once("dbcontroller.php");
$db_handle = new DBController();
if(!empty($_GET["action"])) {
switch($_GET["action"]) {
case "add":
if(!empty($_POST["quantity"])) {
$productByCode = $db_handle->runQuery("SELECT * FROM tblproduct WHERE code='" . $_GET["code"] . "'");
$itemArray = array($productByCode[0]["code"]=>array('name'=>$productByCode[0]["name"], 'code'=>$productByCode[0]["code"], 'quantity'=>$_POST["quantity"], 'price'=>$productByCode[0]["price"]));
if(!empty($_SESSION["cart_item"])) {
if(in_array($productByCode[0]["code"],$_SESSION["cart_item"])) {
foreach($_SESSION["cart_item"] as $k => $v) {
if($productByCode[0]["code"] == $k)
$_SESSION["cart_item"][$k]["quantity"] = $_POST["quantity"];
}
} else {
$_SESSION["cart_item"] = array_merge($_SESSION["cart_item"],$itemArray);
}
} else {
$_SESSION["cart_item"] = $itemArray;
}
}
break;
case "remove":
if(!empty($_SESSION["cart_item"])) {
foreach($_SESSION["cart_item"] as $k => $v) {
if($_GET["code"] == $k)
unset($_SESSION["cart_item"][$k]);
if(empty($_SESSION["cart_item"]))
unset($_SESSION["cart_item"]);
}
}
break;
case "empty":
unset($_SESSION["cart_item"]);
break;
}
}
?>
<HTML>
<HEAD>
<TITLE>Simple PHP Shopping Cart</TITLE>
<link href="style.css" type="text/css" rel="stylesheet" />
</HEAD>
<BODY>
<div id="shopping-cart">
<div class="txt-heading">Shopping Cart <a id="btnEmpty" href="index.php?action=empty">Empty Cart</a></div>
<?php
if(isset($_SESSION["cart_item"])){
$item_total = 0;
?>
<table cellpadding="10" cellspacing="1">
<tbody>
<tr>
<th><strong>Name</strong></th>
<th><strong>Code</strong></th>
<th><strong>Quantity</strong></th>
<th><strong>Price</strong></th>
<th><strong>Action</strong></th>
</tr>
<?php
foreach ($_SESSION["cart_item"] as $item){
?>
<tr>
<td><strong><?php echo $item["name"]; ?></strong></td>
<td><?php echo $item["code"]; ?></td>
<td><input type="number" value="<?php echo $item["quantity"]; ?>"></td>
<td align=right><?php echo "$".$item["price"]; ?></td>
<td><a href="index.php?action=remove&code=<?php echo $item["code"]; ?>" class="btnRemoveAction">Remove Item</a></td>
</tr>
<?php
$item_total += ($item["price"]*$item["quantity"]);
}
?>
<tr>
<td colspan="5" align=right><strong>Total:</strong> <?php echo "$".$item_total; ?></td>
</tr>
</tbody>
</table>
<?php
}
?>
<div class="checkout"> <a href="checkout.php?action=empty">Checkout</a></div>
</div>
<div id="product-grid">
<div class="txt-heading">Products</div>
<?php
$product_array = $db_handle->runQuery("SELECT * FROM tblproduct ORDER BY id ASC");
if (!empty($product_array)) {
foreach($product_array as $key=>$value){
?>
<div class="product-item">
<form method="post" action="index.php?action=add&code=<?php echo $product_array[$key]["code"]; ?>">
<div class="product-image"><img src="<?php echo $product_array[$key]["image"]; ?>"></div>
<div><strong><?php echo $product_array[$key]["name"]; ?></strong></div>
<div class="product-price"><?php echo "$".$product_array[$key]["price"]; ?></div>
<div><input type="text" name="quantity" value="1" size="2" /><input type="submit" value="Add to cart" class="btnAddAction" /></div>
</form>
</div>
<?php
}
}
?>
</div>
</BODY>
</HTML>
如果我正确阅读了您的说明,但我不能100%确定我是否正确,我想您只想在添加时显示购物车。要做到这一点,我想我会把你的页面分成几个部分,这样每个部分都有一个任务。然后,您可以更轻松地将购物车与所有产品显示区分开。您还可以更轻松地在站点的其他部分使用购物车,因为它是一个类:
class.ShoppingCart.php
<?php
class ShoppingCart
{
protected $db;
public function __construct($db)
{
$this->db = $db;
}
public function execute()
{
if(!empty($_GET["action"])) {
switch($_GET["action"]) {
case "add":
$this->AddToCart();
break;
case "remove":
$this->RemoveFromCart();
break;
case "empty":
$this->EmptyCart();
break;
}
}
}
public function AddToCart()
{
// I don't know if code is supposed to be numeric or not and I also
// don't know if you sanitize your input, but from just looking at this
// it looks dangerous (sql injection-prone)
if(!empty($_POST["quantity"]) && is_numeric($_GET["code"])) {
$productByCode = $this->db->runQuery("SELECT * FROM tblproduct WHERE code='" . $_GET["code"] . "'");
$itemArray = array($productByCode[0]["code"]=>array('name'=>$productByCode[0]["name"], 'code'=>$productByCode[0]["code"], 'quantity'=>$_POST["quantity"], 'price'=>$productByCode[0]["price"]));
if(!empty($_SESSION["cart_item"])) {
if(in_array($productByCode[0]["code"],$_SESSION["cart_item"])) {
foreach($_SESSION["cart_item"] as $k => $v) {
if($productByCode[0]["code"] == $k)
$_SESSION["cart_item"][$k]["quantity"] = $_POST["quantity"];
}
} else {
$_SESSION["cart_item"] = array_merge($_SESSION["cart_item"],$itemArray);
}
} else {
$_SESSION["cart_item"] = $itemArray;
}
}
}
public function RemoveFromCart()
{
if(!empty($_SESSION["cart_item"])) {
foreach($_SESSION["cart_item"] as $k => $v) {
if($_GET["code"] == $k)
unset($_SESSION["cart_item"][$k]);
if(empty($_SESSION["cart_item"]))
unset($_SESSION["cart_item"]);
}
}
}
public function EmptyCart()
{
unset($_SESSION["cart_item"]);
}
public function DisplayCart()
{
if(isset($_SESSION["cart_item"]) && !empty($_SESSION["cart_item"])) {
$item_total = 0; ?>
<div id="shopping-cart">
<div class="txt-heading">Shopping Cart <a id="btnEmpty" href="index.php?action=empty">Empty Cart</a></div>
<table cellpadding="10" cellspacing="1">
<tbody>
<tr>
<th><strong>Name</strong></th>
<th><strong>Code</strong></th>
<th><strong>Quantity</strong></th>
<th><strong>Price</strong></th>
<th><strong>Action</strong></th>
</tr><?php
foreach ($_SESSION["cart_item"] as $item) { ?>
<tr>
<td><strong><?php echo $item["name"]; ?></strong></td>
<td><?php echo $item["code"]; ?></td>
<td><input type="number" value="<?php echo $item["quantity"]; ?>"></td>
<td align=right><?php echo "$".$item["price"]; ?></td>
<td><a href="index.php?action=remove&code=<?php echo $item["code"]; ?>" class="btnRemoveAction">Remove Item</a></td>
</tr>
<?php
$item_total += ($item["price"]*$item["quantity"]);
} ?>
<tr>
<td colspan="5" align=right><strong>Total:</strong> <?php echo "$".$item_total; ?></td>
</tr>
</tbody>
</table>
<div class="checkout"> <a href="checkout.php?action=empty">Checkout</a></div>
</div>
<?php }
}
} ?>
<?php
// Exit if the database is not set
// Stops direct access to this file
if(!isset($db_handle)) return; ?>
<div id="product-grid">
<div class="txt-heading">Products</div>
<?php
$product_array = $db_handle->runQuery("SELECT * FROM tblproduct ORDER BY id ASC");
if (!empty($product_array)) {
foreach($product_array as $key=>$value){
?>
<div class="product-item">
<form method="post" action="index.php?action=add&code=<?php echo $product_array[$key]["code"]; ?>">
<div class="product-image"><img src="<?php echo $product_array[$key]["image"]; ?>"></div>
<div><strong><?php echo $product_array[$key]["name"]; ?></strong></div>
<div class="product-price"><?php echo "$".$product_array[$key]["price"]; ?></div>
<div><input type="text" name="quantity" value="1" size="2" /><input type="submit" value="Add to cart" class="btnAddAction" /></div>
</form>
</div>
<?php
}
}
?>
</div>
<?php
session_start();
require_once("dbcontroller.php");
// Include shopping cart class
require_once("class.ShoppingCart.php");
// Initiate db instance
$db_handle = new DBController();
// Initiate shopping cart engine
$ShoppingCart = new ShoppingCart($db_handle);
// Execute the engine portion that handles adding/removing/emptying
$ShoppingCart->execute(); ?><!DOCTYPE html>
<HTML>
<HEAD>
<TITLE>Simple PHP Shopping Cart</TITLE>
<link href="style.css" type="text/css" rel="stylesheet" />
</HEAD>
<BODY>
<?php
// If adding to the cart, show the cart
if(isset($_GET['action']) && $_GET['action'] == 'add')
$ShoppingCart->DisplayCart();
// Only show product page if action add not available
else
include('includes/products/index.php'); ?>
</BODY>
</HTML>
您只是想添加另一个项目行吗?或者你想要一个完整的其他显示部分,比如你圈出的部分?@Rasclatt:我只需要显示索引页中的所有产品。。当我点击“添加到购物车”按钮时,它会链接到另一个选项卡,即“显示购物车”页面。谢谢。你说的帐单是什么意思?您是否希望单击以重新加载页面,而该页面将仅显示购物车,而不是所有其他产品?如果我单击产品下的“添加到购物车”按钮,它需要显示该购物车的单独页面。。谢谢,你的回答不起作用,当我点击“添加到购物车”按钮时,它没有为购物车部分添加产品。而且它不适用于单独的页面。。谢谢。代码与您的代码相同,只是1)它的显示方式有点不同,2)注意到的地方有额外的检查。如果不需要这些额外的检查,您可以删除它们。我把它们放进去是因为在一些地方没有输入的验证。另外,根据我收集到的描述,它应该基于add操作工作。如果操作是add,它将显示购物车,否则将显示其余的产品。那部分很直截了当。