我怎么说“party1和party2有相同数量的选票”否则party1赢得了选举

时间:2012-03-23 21:45:30

标签: php mysql

修改

好吧我已经解决了我现在遇到的任何错误,但我唯一能够理解的是为什么我不知道现在出现在屏幕上的派对的名字!

<?php 
$id = $_GET['election'];

$result = mysql_query(
    sprintf("
        SELECT votes.party, COUNT(votes.vote_id)
        FROM votes
        WHERE election_id = %d
        GROUP BY election_id, votes.party
        ORDER BY COUNT(votes.vote_id) DESC",
        mysql_real_escape_string($id)
    )
);

// change is here

$votes = false;
$winners = array();

while ( ($row = mysql_fetch_row($result) ) && ( ($votes==false) || ($row['vote_count']===$votes) ) ) {
  $winners[] = $row['party'];
  $votes = $row['vote_count'];
}
echo '<hr><h3>'.'Results</h3><hr>'.'<h4>'.implode(' and ', $winners).' won with '.$votes.'</h4>';

?>

我在屏幕上的所有内容都是'和赢得'。我没有得到各方的名字出现!有人知道吗?

1 个答案:

答案 0 :(得分:0)

他们可能是2名获胜者,但也有3名,4名......谁知道。

以下是显示所有真正赢家的通用代码:

// no change here

$id = $_GET['election'];

$result = mysql_query(
    sprintf("
        SELECT votes.party, COUNT(votes.vote_id) AS vote_count
        FROM votes
        WHERE election_id = %d
        GROUP BY election_id, votes.party
        ORDER BY COUNT(votes.vote_id) DESC",
        mysql_real_escape_string($id)
    )
);

// change is here

$votes = false;
$winners = array();
while ( ($row = mysql_fetch_assoc($result) ) && ( ($votes==false) || ($row['vote_count']===$votes) ) ) {
  $winners[] = $row['party'];// was missing the $ before row['party'];
  $votes = $row['vote_count'];
}

echo '<hr><h3>'.'Results</h3><hr>'.'<h4>'.implode(' and ', $winners).' won with '.$votes.'</h4>';