我正忙于创建订单系统。
我在尝试创建只需要一个订单按钮的表单时遇到问题。
目前,表单适用于每个产品结果。用户能够输入特定产品所需的数量,然后可以根据所选数量订购产品。然后将选择的项目ID和数量添加到会话中。
问题在于,如果有5个项目,则有5个订单按钮。
我正在努力简化这一点,对于5种产品,只有一个订单按钮控制整个产品选择。
以下是与此相关的代码:
//Now we search for our search term, in the field the user specified
$dataQuery = "SELECT * FROM `products` WHERE upper(`desc`) LIKE '%"
. implode("%' AND upper(`desc`) LIKE '%", $keywords_array)
. "%' ORDER BY `desc`";
$data = mysql_query($dataQuery) or die(mysql_error());
$tempVar = 0;
//And we display the results
while ($result = mysql_fetch_array($data)) {
$prId = $result['id'];
$prRefCode = $result['refCode'];
$prDesc = $result['desc'];
$prPack = $result['pack'];
$prMeasure = $result['measure'];
$prQuantity = $result['quantity'];
$prDeptCode = $result['deptCode'];
$prTaxable = $result['taxable'];
$prPrice1 = $result['price1'];
$prPrice2 = $result['price2'];
$prCrdCode = $result['crdCode'];
$prCost1 = $result['cost1'];
$prCost2 = $result['cost2'];
if ($tempVar == 0) {
$pageContent .= '
<p><u>All prices are inclusive of VAT</u></p>
<table class="searchResults" border="1" align="center" width="90%">
<thead>
<tr>
<th>Stock Code</th>
<th>Description</th>
<th>Packsize</th>
<th>Price</th>
<th>In-Stock?</th>
<th>Quantity</th>
<th>Submit</th>
</tr>
</thead>
<tbody>
';
}
$pageContent .= '
<tr>
<td>' . $prId . '</td>
<td>' . $prDesc . '</td>
<td>' . $prPack . 'x' . $prSize . ' ' . $prMeasure . '</td>
<td>R' . $prPrice1 . '</td>
';
if (empty($prQuantity)) {
$pageContent .= '
<td>No</td>
';
} else {
$pageContent .= '
<td>Yes</td>
';
}
$pageContent .= '
<form id="validated" action="" method="post">
<td>
<div>
<input type="text"
onkeydown="return ( event.ctrlKey || event.altKey
|| (47<event.keyCode && event.keyCode<58 && event.shiftKey==false)
|| (95<event.keyCode && event.keyCode<106)
|| (event.keyCode==8) || (event.keyCode==9)
|| (event.keyCode>34 && event.keyCode<40)
|| (event.keyCode==46) )"
name="quantity" size ="2"
value ="1"
style="background: #F4F4F4;
font-family: Monaco, monospace;" />
</div>
</td>
<td>
<div>
<input type="hidden" name="id" value="' . $prId . '" />
<input type="submit" name="action" value="Order" />
</div>
</td>
</form>
</tr>
';
$tempVar ++;
}
//This counts the number of results - and if there wasn't any it gives them a little message explaining that
$anymatches = mysql_num_rows($data);
if ($anymatches == 0) {
$pageContent .= '
<p>Sorry, but we can not find an entry to match your query</p>
<!-- end .aliLeft --></div>
';
}
if ($anymatches > 0 and count($tempVar) == count($anymatches)) {
$pageContent .= '
</tbody>
</table>
<!-- end .aliLeft --></div>
';
}
如您所见,当用户搜索产品时,将查询mysql表,如果找到结果,则返回它们。
在此过程中,将为每个结果创建表单。
我想要做的是,扩展此表单以涵盖整个结果集,然后只有一个“订单”输入,所以基本上,用户可以在各种产品上输入数量,然后拥有这些产品和数量加入了他们的订单。
有没有人对如何完成这项任务有任何建议?
我很感激任何输入,谢谢!
OP编辑:
以下是控制用户点击订单按钮实例的代码:
if (!isset($_SESSION['order']))
{
$_SESSION['order'] = array();
}
if (!isset($_SESSION['quantity']))
{
$_SESSION['quantity'] = array();
}
if (isset($_POST['action']) and $_POST['action'] == 'Order' and $_POST['quantity'] > 0)
{
// Add item to the end of the $_SESSION['order'] array
$_SESSION['order'][$_POST['id']] = $_POST['id'];
$_SESSION['quantity'][$_POST['id']] = $_POST['quantity'];
header('Location: .');
exit();
}
以下是配置订购总额的数量值的代码:
$total = 0;
if (count($order) > 0)
{
foreach ($order as $product)
{
mysql_data_seek( $totalsSql, 0); //<- this line, to reset the pointer for every EACH.
while($row = mysql_fetch_assoc($totalsSql))
{
$prodId = $row['id'];
$prodPrice1 = $row['price1'];
$prodQuantity = $quantity[$prodId];
if ($product == $prodId)
{
$total += ($prodPrice1*$prodQuantity);
break;
}
}
}
}
OP编辑:
请参阅修订后的控制器代码:
$productQuantities=$_POST['quantity'];
if (isset($_POST['action']) and $_POST['action'] == 'Order' and $productQuantities > 0)
{
foreach($productQuantities as $productId=>$quantityS)
{
if ($quantityS > 0)
{
$_SESSION['order']["$productId"] = $productId;
$_SESSION['quantity']["$productId"] = $quantityS;
header('Location: .');
exit();
}
}
}
答案 0 :(得分:2)
对您的代码进行以下更改:
..................
...................
<form id="validated" action="" method="post">
while ($result = mysql_fetch_array($data))
{
..........................
.................
......................
<td>
<div>
<input type="text"
onkeydown="return ( event.ctrlKey || event.altKey
|| (47<event.keyCode && event.keyCode<58 && event.shiftKey==false)
|| (95<event.keyCode && event.keyCode<106)
|| (event.keyCode==8) || (event.keyCode==9)
|| (event.keyCode>34 && event.keyCode<40)
|| (event.keyCode==46) )"
// Notice that I have changed name="quantity" to name="quantity[$prId]"
// VVVVVVVVVVVVVVV
name="quantity[$prId]" size ="2"
// ^^^^^^^^^^^^^^^
value ="1"
style="background: #F4F4F4;
font-family: Monaco, monospace;" />
</div>
</td>
...............
.................
................
...............
</tbody>
</table>
<!-- end .aliLeft --></div>
</form>';
}
现在在表格提交的php文件中获取产品ID的数量: -
<?php
$productQuantities=$_POST['quantity'];
if (!isset($_SESSION['order']))
{
$_SESSION['order'] = array();
}
if (!isset($_SESSION['quantity']))
{
$_SESSION['quantity'] = array();
}
foreach($productQuantites as $productId=>$quantity)
{
if ($quantity>0)
{
// Add item to the end of the $_SESSION['order'] array
$_SESSION['order'][$productId] = $productId;
$_SESSION['quantity'][$productId] = $quantity;
header('Location: .');
exit();
}
}
?>