EDIT
ok i have fixed any errors i have had now but the only thing im struggling to understand is why i dont get the name of the parties appearing on screen now!
<?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>';
?>
all i am getting on screen is 'and won with'. i am not getting the names of the parties to appear! anyone know?
They may be 2 winners but also 3, 4 ... who knows.
Here is the generic code that will display all the real winners:
// 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>';