我遇到了一個簡單的腳本問題,它允許用戶瀏覽從MySQL表中繪製的產品。創建訂單購物車 - 點擊「查看購物車」清除會話並重新加載頁面?
該腳本具有「購物車」功能,用戶可以在訂單中添加特定物品。然後,用戶可以查看訂單,該訂單可生成已訂購的特定物料清單,其價格和總金額。 (後來的用戶將能夠以電子郵件輸出到網站的創建者)
這裏是控制器:
<?php
require_once $_SERVER['DOCUMENT_ROOT'].'/includes/config.php';
require_once($docRoot . '/includes/layout.php');
require_once($docRoot . '/includes/magicquotes.inc.php');
$productsQuery = 'SELECT `id`, `refCode`, `desc`, `pack`, `measure`, `quantity`, `deptCode`, `taxable`, `price1`, `price2`, `crdCode`, `cost1`, `cost2` FROM `products` ORDER BY `desc` ';
$productsSql = mysql_query($productsQuery) or die(mysql_error());
session_start();
if (!isset($_SESSION['order']))
{
$_SESSION['order'] = array();
}
if (isset($_POST['action']) and $_POST['action'] == 'Order')
{
// Add item to the end of the $_SESSION['order'] array
$_SESSION['order'][] = $_POST['id'];
header('Location: .');
exit();
}
if (isset($_POST['action']) and $_POST['action'] == 'Clear order')
{
// Empty the $_SESSION['order'] array
unset($_SESSION['order']);
header('Location: ?order');
exit();
}
if (mysql_num_rows($productsSql) == 0)
{
die('No results.');
} else
{
$orderContent = '';
while($row = mysql_fetch_assoc($productsSql))
{
$prId = $row['id'];
$prRefCode = $row['refCode'];
$prDesc = $row['desc'];
$prPack = $row['pack'];
$prMeasure = $row['measure'];
$prQuantity = $row['quantity'];
$prDeptCode = $row['deptCode'];
$prTaxable = $row['taxable'];
$prPrice1 = $row['price1'];
$prPrice2 = $row['price2'];
$prCrdCode = $row['crdCode'];
$prCost1 = $row['cost1'];
$prCost2 = $row['cost2'];
$orderContent .= '
<tr>
<td>'.$prId.'</td>
<td>'.$prDesc.'</td>
<td>'.$prPack.'x'.$prSize.' '.$prMeasure.'</td>
<td>R'.$prPrice1.'</td>
<td>
<form action="" method="post">
<div>
<input type="hidden" name="id" value="'.$prId.'" />
<input type="submit" name="action" value="Order" />
</div>
</form>
</td>
</tr>
';
if (isset($_GET['order']))
{
$order = array();
$total = 0;
foreach ($_SESSION['order'] as $id)
{
foreach ($prId as $product)
{
if ($product == $id)
{
$order[] = $product;
$total += $prPrice1;
break;
}
}
include($docRoot . '/orders/orders-finalize.php');
exit();
}
}
}}
include 'orders-layout.php';
?>
這是包括訂單封邊:
<?php
ob_start();
<meta name="keywords" content="'.$keyWords.'" />
';
$belowMenu = '
<p>Orders Page</p>
';
$pageContent = '
<h2>Your order</h2>
';
if (count($order) > 0)
{
$pageContent .= '
<table>
<thead>
<tr>
<th>Item Description</th>
<th>Price</th>
</tr>
</thead>
<tfoot>
<tr>
<td>Total:</td>
<td>R'.number_format($total, 2).'</td>
</tr>
</tfoot>
<tbody>
';
foreach ($order as $item)
{
if ($item == $prId)
{
$pageContent .= '
<tr>
<td>'.$prDesc.'</td>
<td>
R'.number_format($prPrice1, 2).'
</td>
</tr>
';
}
}
$pageContent .= '
</tbody>
</table>
';
} else
{
$pageContent .= '
<p>Your cart is empty!</p>
';
}
$pageContent .= '
<form action="?" method="post">
<p>
<a href="?">Continue shopping</a>
or
<input type="submit" name="action" value="Clear order" />
</p>
</form>
';
echo $head1 . $pageDetails . $head2 . $header . $menu . $belowMenu . $content . $pageContent . $footer . $pageScripts;
exit;
?>
這是訂單佈局包括:
<?php
ob_start();
$belowMenu = '
<p>Orders Page</p>
';
$pageContent = '
<h2>Place your order</h2>
<p>Your order contains '.count($_SESSION['order']).' items.</p>
<p><a href="?order">View your order</a></p>
<table border="1">
<thead>
<tr>
<th>Stock Code</th>
<th>Description</th>
<th>Packsize</th>
<th>Price</th>
<th>Quantity</th>
</tr>
</thead>
<tbody>
';
$pageContentEnd = '
</tbody>
</table>
<br />
<p>All prices are inclusive of VAT</p>
';
echo $head1 . $pageDetails . $head2 . $header . $menu . $belowMenu . $content . $pageContent . $orderContent . $pageContentEnd . $footer . $pageScripts;
exit;
?>
這產生一個頁面,其中列出了每個結果。
如果用戶點擊「查看訂單」,它應該導致一個頁面,用戶可以看到所有已訂購的項目。
它的實際操作是清空會話並刷新頁面。
如果任何人都可以發現我在這裏出錯,請注意,如果您可以傳遞一些信息!
這裏是控制器是基於代碼:
<?php
include_once $_SERVER['DOCUMENT_ROOT'] .
'/includes/magicquotes.inc.php';
$items = array(
array('id' => '1', 'desc' => 'Canadian-Australian Dictionary',
'price' => 24.95),
array('id' => '2', 'desc' => 'As-new parachute (never opened)',
'price' => 1000),
array('id' => '3', 'desc' => 'Songs of the Goldfish (2CD set)',
'price' => 19.99),
array('id' => '4', 'desc' => 'Simply JavaScript (SitePoint)',
'price' => 39.95));
session_start();
if (!isset($_SESSION['cart']))
{
$_SESSION['cart'] = array();
}
if (isset($_POST['action']) and $_POST['action'] == 'Buy')
{
// Add item to the end of the $_SESSION['cart'] array
$_SESSION['cart'][] = $_POST['id'];
header('Location: .');
exit();
}
if (isset($_POST['action']) and $_POST['action'] == 'Empty cart')
{
// Empty the $_SESSION['cart'] array
unset($_SESSION['cart']);
header('Location: ?cart');
exit();
}
if (isset($_GET['cart']))
{
$cart = array();
$total = 0;
foreach ($_SESSION['cart'] as $id)
{
foreach ($items as $product)
{
if ($product['id'] == $id)
{
$cart[] = $product;
$total += $product['price'];
break;
}
}
}
include 'cart.html.php';
exit();
}
include 'catalog.html.php';
?>
這裏是cart.html.php代碼:
<?php include_once $_SERVER['DOCUMENT_ROOT'] .
'/includes/helpers.inc.php'; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Shopping cart</title>
<meta http-equiv="content-type"
content="text/html; charset=utf-8" />
<style type="text/css">
table {
border-collapse: collapse;
}
td, th {
border: 1px solid black;
}
</style>
</head>
<body>
<h1>Your Shopping Cart</h1>
<?php if (count($cart) > 0): ?>
<table>
<thead>
<tr>
<th>Item Description</th>
<th>Price</th>
</tr>
</thead>
<tfoot>
<tr>
<td>Total:</td>
<td>$<?php echo number_format($total, 2); ?></td>
</tr>
</tfoot>
<tbody>
<?php foreach ($cart as $item): ?>
<tr>
<td><?php htmlout($item['desc']); ?></td>
<td>
$<?php echo number_format($item['price'], 2); ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php else: ?>
<p>Your cart is empty!</p>
<?php endif; ?>
<form action="?" method="post">
<p>
<a href="?">Continue shopping</a> or
<input type="submit" name="action" value="Empty cart"/>
</p>
</form>
</body>
是否包括命令在foreach會話中敲定錯字還是它在實際代碼中的方式? –
相對重定向像這樣:'header('Location:?order');'不合法,儘管它們在很多瀏覽器中都有效。但特別是這個甚至不是頁面的頁面可能不被推薦。 – Ariel
你需要在任何輸出之前調用'session_start' - 可以將它移動到文件的頂部(在任何包含之前)以確保? – Skilldrick