php代码获取总和值并显示总和值的最大值

时间:2014-10-08 13:08:25

标签: php mysql

  

表名---选民

     

字段名称选民

     

输入整数

我从这个表中选择不同的值,得到选民字段的总和,并在循环中回显这个字段。

我需要在变量中取上述求和值,并在此求和字段中显示最大值。我怎么能做到这一点?

这是我的代码:

<?php       
        $party = $rowco['partyname'];   
        $district = $rowco['district']; 
        $constituency = $rowco['constituency'];     
        $sqlvotes = "SELECT SUM(votes) AS vote,constituency FROM voter_count where state = '$state' AND partyname = '$party' AND district = '$district' AND constituency = '$constituency'";    
        $resultvotes = mysql_query($sqlvotes);  
        while($rowvotes = mysql_fetch_array($resultvotes,MYSQL_ASSOC))
        {               
        ?>   
         <th><?php echo $rowvotes['vote']; ?></th>  
          <th><?php echo $rowvotes['constituency']; ?></th>       
    </tr>
    <?php }  ?>

1 个答案:

答案 0 :(得分:1)

简单:

$max = -1;
while($rowvotes = mysql_fetch_array($resultvotes,MYSQL_ASSOC))
{               
    if ($rowvotes['vote'] > $max) $max = $rowvotes['vote'];
    ?>   
    <th><?php echo $rowvotes['vote']; ?></th>  
    <th><?php echo $rowvotes['constituency']; ?></th>       
    </tr>
    <?php 
} 

echo "Max value : $max";
?>