如何计算PHP中while循环内的乘法值?

时间:2012-02-08 06:56:55

标签: php mysql

我有一个while循环显示来自数据库的数据。现在我想在一行中将两个值相乘并在同一行中显示结果,以相同的方式将值相乘并在每一行中显示结果。我没有得到结果。谁能帮我?我是PHP的新手。

while($row = mysql_fetch_array($result))
{
   echo "<tr>";
   echo "<td class='alt'>" . $row['id'] . "</td>";
   echo "<td>" . $row['item'] . "</td>";
   echo "<td>" . $row['amount'] . "</td>";
   $ss=$row['amount'];
   echo '<td >'.'<input type="checkbox"  name="status" value="" >'.'</td>';
   echo '<td >'.'<input type="text" name="qty">'.'</td>';
   echo "<td>" . $rr1 . "</td>";
   echo "</tr>";
 }

4 个答案:

答案 0 :(得分:0)

从哪里获得$rr1 ??

你可以拥有

echo "<td>" . $row['item'] * $row['amount'] . "</td>";

希望这就是你想要的......

答案 1 :(得分:0)

你可以尝试

printf('amount is %d',$row['item'] * $row['amount']);

reference

答案 2 :(得分:0)

尝试以下:

 while($row = mysql_fetch_array($result))
    {
       echo "<tr>";
       echo "<td class='alt'>" . $row['id'] . "</td>";
       echo "<td>" . $row['item'] . "</td>";
       echo "<td>" . $row['amount'] . "</td>";
       echo "<td>" .($row['item'] * $row['amount']). "</td>";
       echo "</tr>";
     }

答案 3 :(得分:0)

将查询更改为

$result= mysql_query("Select id, item, amount, (item * amount)  total
 from table_name");

然后在while循环中

while($row = mysql_fetch_array($result))
    {
       echo "<tr>";
       echo "<td >" . $row['id'] . "</td>";
       echo "<td>" . $row['item'] . "</td>";
       echo "<td>" . $row['amount'] . "</td>";
       echo "<td>" .($row['total']  "</td>";
       echo "</tr>";
     }