2013-05-29 132 views
0

我有试图遍历在查询返回的结果量的问题,我有一个数据库表叫Cart具有以下字段:遍历数据库表中的值

ItemCode//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

我要循环(在Cart表,简单的打印所有数据)显示在我的display.php的所有记录,然后乘以ItemUnitPriceQuantity,并将其存储在一个变量中,以存储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 protected]" /> 
<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> 
+0

你需要'ItemUnitPrice * Quantity'' –

+0

代码中表 –

+0

中所有行的总和,在'$ _SESSION ['totalCost'] = ...之前放置'session_start()',并且在您的输入值中使用'$ _SESSION ['totalCost']'if all代码是在同一个文件中,你不必使用Session它应该以它的方式工作,除非有查询结果问题尝试'var_dump($ result);'来查看结果。 –

回答

1

首先,摆脱使用mysql_*功能的习惯!使用PDOmysqli

Why shouldn't I use mysql_* functions in PHP?

http://php.net/pdo

其次,我很伤心地告诉你,你的代码是完全错误的!

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(); 
+0

嘿,谢谢你的补充,但我展示的PHP代码并不是试图去做我所要求的简单的设置表中每个字段的变量,所以我可以稍后使用它们来显示它。所以我的代码并不完全错误,你只是把它误认为是别的东西 –

+0

当然可以。如果你的代码在函数IE:'function someFunction(){/ *上面的代码* /}'内,那么'$ cartTotalPrice'值只在该函数中可用。但是,您可能希望将其存储在会话对象中。我会更新我的答案和如何做到这一点。总之,是的。 –

+0

它仍然返回0作为'$ _SESSION ['totalCost'] = $ cartTotalPrice;'正在拾取'$ cartTotalPrice'外循环首先初始化为0,所以它给我回0的结果。 –

0

尝试下面的代码,我希望它的作品,我做了更正,并在评论中提到他们:按行或所有行的总和

$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_start(); 
$_SESSION['totalCost'] = $cartTotalPrice; 
    // mysql_close(); // here you are closing your mysql connection before using mysql_result refer to the example in http://php.net/manual/en/function.mysql-result.php 
    // session_start(); // session start should go above you dont need session if the whole script is in the same page. no need to save the variable to session 
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>"; 


mysql_close(); 
?> 



<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 protected]" /> 
<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>