Php 创建订单购物车-单击;查看购物车“;是否清除会话并重新加载页面?
我遇到了一个简单的脚本问题,该脚本允许用户浏览从MySQL表中提取的产品 该脚本有一个“购物车”功能,用户可以在其中向订单添加特定项目。然后,用户可以查看订单,该订单生成已订购的特定项目、价格和总金额的列表。(稍后,用户将能够通过电子邮件将输出发送给网站创建者) 这是控制器:Php 创建订单购物车-单击;查看购物车“;是否清除会话并重新加载页面?,php,mysql,session,compare,Php,Mysql,Session,Compare,我遇到了一个简单的脚本问题,该脚本允许用户浏览从MySQL表中提取的产品 该脚本有一个“购物车”功能,用户可以在其中向订单添加特定项目。然后,用户可以查看订单,该订单生成已订购的特定项目、价格和总金额的列表。(稍后,用户将能够通过电子邮件将输出发送给网站创建者) 这是控制器: <?php require_once $_SERVER['DOCUMENT_ROOT'].'/includes/config.php'; require_once($docRoot . '/includes/layo
<?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;
?>
既然您说这不是粘贴到stackoverflow时引入的打字错误,请参见以下内容:
foreach ($_SESSION['order'] as $id)
{
foreach ($prId as $product)
{
if ($product == $id)
{
$order[] = $product;
$total += $prPrice1;
break;
}
}
include($docRoot . '/orders/orders-finalize.php'); **//why this is included here? you are not yet looped to include all the orders from session inside order array**
exit();
}
看到我说的为什么包括在这里的那一行了吗?您尚未循环以将来自会话的所有订单包含在订单数组中。您将该行放在foreach中,该行循环通过实际生成订单数组的会话,但在包含的订单最终确定中,您尝试循环通过未完成的订单
不确定这是否能解决您的问题,但请尝试检查此问题并让我们知道。既然您说这不是粘贴到stackoverflow时引入的拼写错误,请参见以下内容:
foreach ($_SESSION['order'] as $id)
{
foreach ($prId as $product)
{
if ($product == $id)
{
$order[] = $product;
$total += $prPrice1;
break;
}
}
include($docRoot . '/orders/orders-finalize.php'); **//why this is included here? you are not yet looped to include all the orders from session inside order array**
exit();
}
看到我说的为什么包括在这里的那一行了吗?您尚未循环以将来自会话的所有订单包含在订单数组中。您将该行放在foreach中,该行循环通过实际生成订单数组的会话,但在包含的订单最终确定中,您尝试循环通过未完成的订单
不确定这是否能解决您的问题,但请尝试检查这一点并让我们知道。在foreach会话中包含order finalize是一个输入错误,还是实际代码中是这样的?相关重定向如下:标题('Location:?order')代码>是不合法的,尽管它们在很多浏览器中都可以工作。但特别是这一个页面不是偶数的页面可能不被推荐。在任何输出之前,您需要调用session\u start
——也许可以将其移动到文件顶部(在任何包含之前)以确保?@恐怕这不是一个打字错误,查看从sitepoint手册中取出的原始脚本的“我的编辑”。@Ariel什么是不合法的?在foreach会话中包含订单最终确定是一个输入错误,还是实际代码中是这样的?相关重定向如下:标题('Location:?order')代码>是不合法的,尽管它们在很多浏览器中都可以工作。但特别是这一个页面不是偶数的页面可能不被推荐。在任何输出之前,您需要调用session\u start
——也许可以将其移动到文件顶部(在任何包含之前)以确保?@恐怕这不是一个打字错误,有关从sitepoint手册中取出的原始脚本,请参见“我的编辑”。@Ariel什么是非法?对于其他问题,请尝试添加session_write_close();在标题(“位置:”)之前,在您将订单添加到会话之后,我的话!!!好吧,你是对的!但现在我看到一个页面,上面显示“你的购物车是空的”。在原始页面上,我向订单中添加了一个项目,然后单击“查看订单”,但它似乎没有完成会话?是否尝试添加会话_write_close()?请看我的上述评论。是的,我已经添加了这个。我认为可能没有向$order数组中添加任何内容,或者可能没有在finalize页面上添加foreach和if,但我不太确定是什么。请尝试使用例如var_dump然后在可疑会话后逐个放置die()来调试代码,并查看哪一个未正常工作以确定。有很多可能性。或者,您可以创建一个函数,在文件中记录日志,并尝试在每个步骤中记录输出。对于其他问题,请尝试添加会话_write_close();在标题(“位置:”)之前,在您将订单添加到会话之后,我的话!!!好吧,你是对的!但现在我看到一个页面,上面显示“你的购物车是空的”。在原始页面上,我向订单中添加了一个项目,然后单击“查看订单”,但它似乎没有完成会话?是否尝试添加会话_write_close()?请看我的上述评论。是的,我已经添加了这个。我认为可能没有向$order数组中添加任何内容,或者可能没有在finalize页面上添加foreach和if,但我不太确定是什么。请尝试使用例如var_dump然后在可疑会话后逐个放置die()来调试代码,并查看哪一个未正常工作以确定。有很多可能性。或者,您可以创建一个函数,在文件中记录日志,并尝试在每个步骤中记录输出
<?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 ($_SESSION['order'] as $id)
{
foreach ($prId as $product)
{
if ($product == $id)
{
$order[] = $product;
$total += $prPrice1;
break;
}
}
include($docRoot . '/orders/orders-finalize.php'); **//why this is included here? you are not yet looped to include all the orders from session inside order array**
exit();
}