【问题标题】:Iterate over database table values迭代数据库表值
【发布时间】:2013-05-25 06:41:55
【问题描述】:

我在尝试迭代查询中返回的结果数量时遇到问题,我有一个名为 Cart 的数据库表,其中包含以下字段:

项目代码 //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 中包含的所有内容的总价。

我想要这样的东西:

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>

【问题讨论】:

  • 你需要ItemUnitPrice * Quantity by row 还是所有行的总和?`
  • 表中所有行的总和
  • 在您的代码中将session_start() 放在$_SESSION['totalCost']=... 之前,并在您的输入值中使用$_SESSION['totalCost'] 如果所有代码都在同一个文件中,您不必使用会话它应该以它的方式工作除非有查询结果问题,否则请尝试var_dump($result); 来查看结果。
  • 不,结果仍然返回 0,如果您显示代码会很有用
  • 不要破坏您的帖子。这是您唯一的警告。

标签: php mysql loops store


【解决方案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();

【讨论】:

  • 嘿,感谢您的补充,但我展示的 PHP 代码并没有尝试做我所要求的,它只是为表中的每个字段设置变量,以便我以后可以使用它们用于显示它所显示的......所以我的代码并没有完全错误你只是把它误认为是别的东西
  • 当然可以。如果上面的代码在函数 IE 中:function someFunction(){ /* the above code */ },则 $cartTotalPrice 值仅在该函数中可用。但是,您可能希望将其存储在会话对象中。我会用如何做到这一点来更新我的答案。简而言之,是的。
  • 它仍然返回 0,因为 $_SESSION['totalCost'] = $cartTotalPrice; 在循环外拾取 $cartTotalPrice,它首先被初始化为 0,所以它给了我一个 0 的结果。
  • 我只是想在&lt;form&gt;的循环外回显它
  • 它仍然返回 0,即使使用 session_start();
【解决方案2】:

试试下面的代码我希望它有效,我做了更正并在 cmets 中提到它们:

$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="info@asianweddingservices.org" />
<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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-28
    • 2020-01-22
    相关资源
    最近更新 更多