我在尝试迭代查询中返回的结果量时遇到问题,我有一个Cart
使用以下字段调用的数据库表:
项目代码 //Unique Code of Item
项目描述 //Description / Name ofItem
商品单价 //Unit Price for Item
项目类别 //Category of Item e.g. Books, CD, DVD etc...
数量 //Quantity of Item(s) in cart
我想遍历显示在我的所有记录display.php
(它只是打印Cart
表中的所有数据),然后将每个项目乘以ItemUnitPrice
并将Quantity
其存储在一个变量中以存储display.php
.
我想要这样的东西:
LOOP
$Total= $ItemUnitPrice * $Quantity;
END LOOP
我正在使用 MySQL,但我不太确定应该如何循环获取每个项目的总数。
所以简而言之,我想找到数据库表中每个项目的总数( ItemUnitPrice
* )并将其存储在一个变量中。Quantity
编辑:
$query="SELECT * FROM Cart";
$result=mysql_query($query);
$num=mysql_num_rows($result);
$cartTotalPrice = 0;
while($row = mysql_fetch_assoc($result))
{
$cartTotalPrice += ($row['itemUnitPrice']*$row['Quantity']);
}
$_SESSION['totalCost'] = $cartTotalPrice;
mysql_close();
session_start();
echo "<b><center> Islamic Book Store - Your Shopping Cart </b><br/><br/>";
$i=0;
echo "<table border=1><tr><th>Item Code</th><th>Item Desc,</th>";
echo "<th> Item Unit Price</th><th>Item Category</th><th>Quantity</th><th>Image</th> <th>Update Quantity</th></tr>";
while ($i < $num)
{
$ItemCode = mysql_result($result,$i,"ItemCode");
$ItemDesc = mysql_result($result,$i,"ItemDesc");
$ItemUnitPrice = mysql_result($result,$i,"ItemUnitPrice");
$ItemCategory = mysql_result($result,$i,"ItemCategory");
$Quantity = mysql_result($result,$i,"Quantity");
echo "<tr><td align=center>$ItemCode</td><td align=center>$ItemDesc</td>";
echo "<td align=center>£$ItemUnitPrice</td>";
echo "<td align=center>$ItemCategory</td><td align=center>$Quantity</td>";
$i++;
}
echo "</table><center>";
echo "$num Item(s) found.";
echo "<br/><br/><center><form action = 'clear.php'><input type='submit' value='Clear'> </form></center>";
?>
<html>
<form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_BLANK">
<input type="hidden" name="cmd" value="_xclick" />
<input type="hidden" name="business" value="email@example.com" />
<input type="hidden" name="item_name" value="<? echo $ItemDesc ?>" />
<input type="hidden" name="item_number" value="TEST ITEM NUMBER" />
<input type="hidden" name="amount" value="<? echo $cartTotalPrice ?>" />
<input type="hidden" name="currency_code" value="GBP" />
<input type="hidden" name="lc" value="GB" />
<input type="hidden" name="bn" value="PP-BuyNowBF" />
<input src="paypal/purchase.png" name="Submit" type="image" value="purchase" alt="Purchase" />
</form>
</html>