0

我遵循数据库结构。

id      email     lat      long      point      balance    date
1       33        1.00     2.00      0          empty      date 
2       34        8.00     3.00      -1         empty      date
3       33        7.00     4.00      2          empty      date
4       33        6.00     5.00      0          empty      date
5       33        6.33     5.43      -1         empty      date

所以我想显示所有记录哪个电子邮件 ID 是 33,但它必须显示每一行的余额,例如。

In first row it's balance is 0
second row it's balance is   2
third row it's balance is    2
four row it's balance is     1 

所以我的 php 代码看起来像这样,但无法获得正确的平衡:

echo "<table width='100%' cellpadding='5' cellspacing='0' border='1'>";
echo "<tr>";
echo "<td class='tdhead' valign='top' width='100'><b>Date</b></td>";    
echo "<td class='tdhead' valign='top' width='100'><b>Lattitude</b></td>";
echo "<td class='tdhead' valign='top' width='50'><b>Longitude</b>
</td>";                     
echo "<td class='tdhead' valign='top' width='50'><b>Point</b>
</td>";                     
echo "<td class='tdhead' valign='top' width='50'><b>Balance</b>
</td>";                     
echo "</tr>";

while($res =  mysql_fetch_array($park_history))
{
    $lat = $res['lat'];
    $long = $res['long'];
    $point =  $res['point'];
    $date = $res['date'];           
    $balance = 0;

$sum = mysql_query("SELECT SUM(point) AS points FROM balance WHERE email = 
'".$_SESSION['SESS_ID']."'");
    $sum_res = mysql_fetch_array($sum);
    $sum = $sum_res['points'];

    echo "<tr>";                
            echo "<td class='tdhead2' valign='top'>$date</td>";
            echo "<td class='tdhead2' valign='top'>$lat</td>";
            echo "<td class='tdhead2' valign='top'>$long</td>";
            echo "<td class='tdhead2' valign='top'>$point</td>";
            echo "<td class='tdhead2'    
valign='top'>$sum</td>";                        
    echo "</tr>";
}

我相信可以使用 mysql sum 函数来完成。你能给我解决方案或建议吗?谢谢你。

4

2 回答 2

1

MySQLsum函数不会做你想做的事——但它不是必须的——有一种更简单的方法可以使用你已经获取的结果来完成你的任务。

由于您已经拥有$point要操作的行,只需将其添加到计数器并从那里继续。实际上,您每行都进行了多余的 db 调用。

利用:

$sum = 0;

while ( $res = mysql_fetch_array($park_history) ) {

    /* yada yada */
    $point =  $res['point'];
    $sum += $point;

    echo /* your table here */

}

您可以完全删除这些行:

$sum = mysql_query( ... );
$sum_res = mysql_fetch_array($sum);
$sum = $sum_res['points'];

$total将按照您的描述保存运行点数,并且不会在每个循环中查询您的数据库。

于 2013-10-26T05:37:59.460 回答
0

这是php:

$park_history = mysql_query("
SELECT * 
FROM balance 
WHERE email ='".$_SESSION['SESS_ID']."'");
$i = 0;
$balance = 0; // the first sum of balance will 0 + first point
while($res =  mysql_fetch_array($park_history))
{
    $i++;
    $lat    = $res['lat'];
    $long   = $res['long'];
    $point  = $res['point'];
    $date   = $res['date'];           
    $balance= $balance + $point; // balance will be updated in every loops
    echo "<tr>";                
            echo "<td class='tdhead2' valign='top'>$date</td>";
            echo "<td class='tdhead2' valign='top'>$lat</td>";
            echo "<td class='tdhead2' valign='top'>$long</td>";
            echo "<td class='tdhead2' valign='top'>$point</td>";
            echo "<td class='tdhead2' valign='top'>$balance</td>";                        
    echo "</tr>";
}

我删除此代码:

$sum = mysql_query("SELECT SUM(point) AS points FROM balance WHERE email = 
'".$_SESSION['SESS_ID']."'");
    $sum_res = mysql_fetch_array($sum);
    $sum = $sum_res['points'];

并重新定义$park_history

$park_history = mysql_query("
SELECT * 
FROM balance 
WHERE email ='".$_SESSION['SESS_ID']."'");

看看吧!如果您发现一些错误,请告诉我(评论).. 可能会有所帮助:D

于 2013-10-26T05:41:04.980 回答