遍历数据库表值 [英] Iterate over database table values
问题描述
我尝试遍历查询中返回的结果量时遇到问题,我有一个名为Cart
的数据库表,其中包含以下字段:
I have a problem with trying to iterate over the amount of results returned in a query,I have a database table called Cart
with the following fields:
商品代码 //Unique Code of Item
ItemDesc //Description / Name ofItem
ItemUnitPrice //Unit Price for Item
ItemCategory (产品类别) //Category of Item e.g. Books, CD, DVD etc...
数量 //Quantity of Item(s) in cart
我想遍历display.php
中显示的所有记录(该记录仅打印Cart
表中的所有数据),然后将ItemUnitPrice
与Quantity
相乘,并将其存储在一个变量,用于存储display.php
中包含的所有内容的总价格.
I want to loop over all the records displayed in my display.php
(which simply prints all the data in the Cart
table) and then multiply the ItemUnitPrice
by the Quantity
for every item and store it in a variable to store the total price for everything contained in display.php
.
我想要这样的东西:
LOOP
$Total= $ItemUnitPrice * $Quantity;
END LOOP
我正在使用MySQL,但我不太确定如何循环获取每个项目的总数.
I am using MySQL and I'm not too sure how I should loop to get the total for each and every item.
因此,简而言之,我想找到数据库表中每个项目的总计(ItemUnitPrice
* Quantity
),并将其存储在变量中.
So in a nutshell I want to find the total (ItemUnitPrice
* Quantity
) for each and every item in the database table and store it in a variable.
$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>
推荐答案
首先,摆脱使用mysql_*
函数的习惯!使用PDO
或mysqli
.
First of all, get out of the habit of using mysql_*
functions! Use PDO
or mysqli
.
其次,我很伤心地告诉你,你的代码是完全错误的!
Secondly, I'm sad to tell you, your code is completely wrong!
session_start();
$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']);
echo "<tr><td align=center>{$row['itemCode']}</td><td align=center>{$row['ItemDesc']}</td>";
echo "<td align=center>{$row['ItemUnitPrice']}</td>";
echo "<td align=center>{$row['$ItemCategory']}</td><td align=center>{$row['$Quantity']}</td></tr>";
}
$_SESSION['totalCost'] = $cartTotalPrice;
// $_SESSION['totalCost'] is now available on every page (as long as you use start_session() before any output)
mysql_close();
这篇关于遍历数据库表值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!