简体   繁体   English

如何在从MySQL检索的表行中添加值?

[英]How to add up values in the table row which is retrieved from MySQL?

What I want to is add values in a table row retrieved from MySQL. 我要在从MySQL检索的表行中添加值。

This is my PHP file: 这是我的PHP文件:

<?php

mysql_connect('localhost','root','');
mysql_select_db('accounts');
$sql= "SELECT * FROM users1";
$list=mysql_query($sql);

?>
<html>
<head>
<title>Welcome to ScanNShop</title>
</head>
<h1>Welcome to ScanNShop</h1>

<body>

<table width="600" border="1" cellpadding="1" cellspacing="1">
<tr>

<th>No.</th>
<th>Name</th>
<th>Price</th>
<tr>

<?php

while ($users1=mysql_fetch_assoc($list)) {

echo "<tr>";

echo "<td>".$users1['pid']."</td>";

echo "<td>".$users1['name']."</td>";

echo "<td>".$users1['price']."</td>";

}

?>

</table>
</body>
</html  >

This is the output from the PHP file. 这是PHP文件的输出。

No. Name    Price
1   bread   2.00
2   milk    2.00 
3   janabab 6797994.00
4   jajajsh 846494.00

I want to add up all the price and display an echo "Total:" thetotal 我想将所有价格加起来并显示echo "Total:" thetotal

I kept your code easy and add the change's I could give you an other code but this is easy to understand if your learning php 我让您的代码简单易行,并添加了更改,我可以给您其他代码,但是如果您学习的是php,这很容易理解

I added a comment by the change's 我通过更改添加了评论

mysql_connect('localhost','root','');
mysql_select_db('accounts');
$sql= ('SELECT * FROM users1');
$list=mysql_query($sql);

?>
<html>
<head>
<title>Welcome to ScanNShop</title>
</head>
<h1>Welcome to ScanNShop</h1>

<body>

<table width="600" border="1" cellpadding="1" cellspacing="1">
<tr>
<th>No.</th>
<th>Name</th>
<th>Price</th>
</tr>

<?php
 //set total to zero
$total=0;
while ($users1=mysql_fetch_assoc($list)) {

echo "<tr>";

echo "<td>".$users1['pid']."</td>";

echo "<td>".$users1['name']."</td>";

echo "<td>".$users1['price']."</td>";
//set total to the price + the previous total
$total = $users1['price']+$total;

echo "</tr>";

}
//display total
echo '<tr><td></td>';
echo '<td>Total:</td>';
echo '<td>'.$total.'</td></tr>';

?>

</table>
</body>
</html  >

Another way to sum data rows across is within the SQL query itself by using GROUP BY .. WITH ROLLUP (doc) . 汇总数据行的另一种方法是在SQL查询本身中使用GROUP BY .. WITH ROLLUP(doc)

Example: 例:

SELECT Number, Min(Name) AS Name, SUM(amount) AS Price
  FROM grocery_list
 GROUP BY Number WITH ROLLUP

Will produce the output: 将产生输出:

Number  Name      Price
1       bread     2.00
2       milk      2.00 
3       janabab   6797994.00
4       jajajsh   846494.00
NULL    bread     7644492.00

The Line with NULL is the MIN(name), SUM(Price) across ALL records for the group 'Number'. 带有NULL的行是组“ Number”的所有记录中的MIN(名称),SUM(价格)。 You can then filter your output based on which field is NULL to determine how to display. 然后,您可以根据哪个字段为NULL过滤输出,以确定如何显示。

May be you need just to sum results in special variable? 可能只需要对特殊变量中的结果求和即可?

$sum = 0;
while ($users1=mysql_fetch_assoc($list)) {
    $sum += $users1['price'];

    echo '<tr>';
    echo '<td>'.$users1['pid'].'</td>';
    echo '<td>'.$users1['name'].'</td>';
    echo '<td>'.$users1['price'].'</td>';
    echo '</tr>';
}

echo '<tr>';
echo '<td></td>';
echo '<td>Total</td>';
echo '<td>'.$sum.'</td>';
echo '<tr>';

Do not use double quotes (") for strings. Use single quotes ('); And do not use mysql_* functions, they are deprecated. 不要在字符串中使用双引号(“)。使用单引号(');并且不要使用mysql_ *函数,因为它们已被弃用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM