在PHP中显示SQL表并比较值

时间:2013-04-30 18:45:57

标签: php mysql sql

我有这张桌子:

+----+---------------------+----------+---------+---------+---------+
| id | date                | client1  | client2 | client1 | client1 |
+----+---------------------+----------+---------+---------+---------+
|  1 | 2013-04-17 16:15:46 |     8592 |    9786 |    6471 |       0 | 
|  2 | 2013-04-27 16:15:46 |     8617 |    9876 |    7130 |   40243 | 
|  3 | 2013-04-28 17:57:30 |     8617 |    9884 |    7211 |   41657 | 
|  4 | 2013-04-29 13:28:11 |     8616 |    9886 |    7270 |   42516 | 
+----+---------------------+----------+---------+---------+---------+

我在这样的PHP中显示它:

  <table width="800" border="0" cellspacing="3" cellpadding="3" style="margin-left:auto; margin-right:auto">
  <tr>
    <th scope="col" style="text-align:center; font-weight:bold">Date</th>
    <th scope="col" style="text-align:center; font-weight:bold">Client 1</th>
    <th scope="col" style="text-align:center; font-weight:bold">Client 2</th>
    <th scope="col" style="text-align:center; font-weight:bold">Client 3</th>
    <th scope="col" style="text-align:center; font-weight:bold">Client 4</th>
  </tr>
  <?

$queryuv = mysql_query("SELECT * FROM com_information ORDER BY date ASC");
while ($resultuv=mysql_fetch_assoc($queryuv)) {
        $date=$resultuv['date'];
        $client1=$resultuv['client1'];
        $client2=$resultuv['client2'];
        $client3=$resultuv['client3'];
        $client4=$resultuv['client4'];          
        ?>            
  <tr>
    <th scope="row" style="text-align:left"><?=$date?></th>
    <td style="text-align:center"><?=number_format($client1,0,'.',',');?></td>
    <td style="text-align:center"><?=number_format($client2,0,'.',',');?></td>
    <td style="text-align:center"><?=number_format($client3,0,'.',',');?></td>
    <td style="text-align:center"><?=number_format($client4,0,'.',',');?></td>
  </tr>

<? } ?>
</table>

我想要做的是,在表格中,我显示信息,将值与来自同一客户的上一个日期进行比较,并显示它增长或减少了多少。

2 个答案:

答案 0 :(得分:1)

设置一些变量来跟踪之前的值

$lastclient1 = 0 ;
$lastclient2 = 0 ;
$lastclient3 = 0 ;
$lastclient4 = 0 ;

像以前一样继续

$queryuv = mysql_query("SELECT * FROM com_information ORDER BY date ASC");

while ($resultuv=mysql_fetch_assoc($queryuv)) {
        $date=$resultuv['date'];
        $client1=$resultuv['client1'];
        $client2=$resultuv['client2'];
        $client3=$resultuv['client3'];
        $client4=$resultuv['client4'];          
        ?>   

像以前一样输出您的数据 - 对于您可以使用的移动数据:

<?=number_format($client1-$lastclient1,0,'.',',');?>

在循环结束之前设置最后一行数据

<?
        $lastclient1=$client1;
        $lastclient2=$client2;
        $lastclient3=$client3;
        $lastclient4=$client4;
      }
?>

答案 1 :(得分:0)

你的意思是这样的:

<table width="800" border="0" cellspacing="3" cellpadding="3" style="margin-left:auto; margin-right:auto">
  <tr>
    <th scope="col" style="text-align:center; font-weight:bold">Date</th>
    <th scope="col" style="text-align:center; font-weight:bold">Client 1</th>
    <th scope="col" style="text-align:center; font-weight:bold">Client 2</th>
    <th scope="col" style="text-align:center; font-weight:bold">Client 3</th>
    <th scope="col" style="text-align:center; font-weight:bold">Client 4</th>
  </tr>
  <?

$queryuv = mysql_query("SELECT * FROM com_information ORDER BY date ASC");

$client1 = false;
$client2 = false;
$client3 = false;
$client4 = false;

while ($resultuv=mysql_fetch_assoc($queryuv)) { ?>            
  <tr>
    <th scope="row" style="text-align:left"><?=$resultuv['date']?></th>
    <td style="text-align:center"><?=number_format($resultuv['client1'],0,'.',',');?><?if($client1 !== false) echo ($client1-$resultuv['client1'] > 0 ? '+' : '-') . number_format($client1-$resultuv['client1'], 0, '.', ',')?> </td>
    <td style="text-align:center"><?=number_format($resultuv['client2'],0,'.',',');?><?if($client2 !== false) echo ($client2-$resultuv['client2'] > 0 ? '+' : '-') . number_format($client2-$resultuv['client2'], 0, '.', ',')?></td>
    <td style="text-align:center"><?=number_format($resultuv['client3'],0,'.',',');?><?if($client3 !== false) echo ($client3-$resultuv['client3'] > 0 ? '+' : '-') . number_format($client3-$resultuv['client3'], 0, '.', ',')?></td>
    <td style="text-align:center"><?=number_format($resultuv['client4'],0,'.',',');?><?if($client4 !== false) echo ($client4-$resultuv['client4'] > 0 ? '+' : '-') . number_format($client4-$resultuv['client4'], 0, '.', ',')?></td>
  </tr>
<?
    $client1 = $resultuv['client1'];
    $client2 = $resultuv['client2'];
    $client3 = $resultuv['client3'];
    $client4 = $resultuv['client4'];
?>
<? } ?>
</table>

???

我强烈建议在PDO函数被弃用时切换到 mysqli 或至少 mysql_* ... < / p>