voting logo Results for $electionname"); echo("

This election is still open. Official results can only be viewed upon closure of the election.

"); } else { if ($electionstatus == "Closed" and $released == 0) { // they must wait for it to be released to see it echo("

This election has been closed but results have yet to be released.
Please check again later...

"); } elseif ($electionstatus == "Closed" and $released == 1) { //the election is closed, so results can be viewed if ($admin == true) { // print the administrator-only content // show link to go back echo("

Back to Election Administration

"); // let's print voter turnout echo("

Results for Voter Turnout for $electionname:

"); echo("

Total Turnout

"); // total voters created $query = "SELECT * FROM Voters WHERE ElectionID = $electionid;"; $result = mysql_query($query) or die("Query Error 54:" . mysql_error()); $totalvoters = mysql_num_rows($result); // total voters who voted $query = "SELECT * FROM Voters WHERE ElectionID = $electionid AND HasVoted = 1;"; $result = mysql_query($query) or die("Query Error 54:" . mysql_error()); $totalturnout = mysql_num_rows($result); // total writeins $query = "SELECT * FROM Voters WHERE ElectionID = $electionid AND HasVoted = 1 AND WriteIn = 1;"; $result = mysql_query($query) or die("Query Error 54:" . mysql_error()); $totalwriteins = mysql_num_rows($result); // get the turnout; account for divide-by-zero if ($totalvoters == 0) { $turnoutpercent = 0; } else { $turnoutpercent = ($totalturnout / $totalvoters) * 100; } // get the write-in percentage; account for divide-by-zero if ($totalturnout == 0) { $writeinpercent == 0; } else { $writeinpercent = ($totalwriteins / $totalturnout) * 100; } // print the stats echo("

" . toPercentage($turnoutpercent) . " total voted ($totalturnout/$totalvoters)
"); echo("Of those, " . toPercentage($writeinpercent) . " were write-in ballots ($totalwriteins/$totalturnout)"); // get the stats for each department // we'll need to loop several times (it's inefficient, but it does the job) $query = "SELECT * FROM Voters WHERE ElectionID = $electionid;"; $result = mysql_query($query) or die("Query Error 54:" . mysql_error()); while ($row = mysql_fetch_array($result)) { // stats for total possible viewers $depttotal[$row['Department']]['Total']++; // total voters in this department $depttotal[$row['Department']][$row['Year']]++; // total voters in this department and this year $yeartotal[$row['Year']]++; // total voters in this year // stats for actual turnout if ($row['HasVoted'] == 1) { $deptvoted[$row['Department']]['Total']++; // total voters who voted in this department $deptvoted[$row['Department']][$row['Year']]++; // total voters who voted in this department and year $yearvoted[$row['Year']]++; // total voters in this year if ($row['WriteIn'] == 1) { $deptwriteins[$row['Department']]++; //total writeins for this departments } } } // print out the year totals // sort the year (U0, U1, etc.) ksort($yeartotal); // loop through each of the year total values (keys in the dictionary of year to total for that year foreach ($yeartotal as $year => $total) { // turn no-data into a 0 $tempcount = $yearvoted[$year]; if ($tempcount == "") { $tempcount = 0; } // account for divide by zero if ($total == 0) { $yearpercent == 0; } else { $yearpercent = ($tempcount / $total) * 100; } // print the stat echo("
" . toPercentage($yearpercent) . " of U$year voted ($tempcount/$total)"); } echo("

"); // print out departmental information // loop through each departmental total in the departmental totals list (dictionary of dept to totals for that dept) foreach ($depttotal as $dept => $totals) { echo("

Turnout for $dept

"); // let's sort them so the years look in order ksort($totals, SORT_STRING); //, SORT_NUMERIC); if($deptvoted[$dept]) { ksort($deptvoted[$dept], SORT_STRING); //, SORT_NUMERIC); } // loop through each year in the departmental totals (dictionary of departmental year to count for that year) foreach ($totals as $year => $count) { // convert no-data to 0 $tempcount = $deptvoted[$dept][$year]; if ($tempcount == "") { $tempcount = 0; } // account for divide-by-zero if ($count == 0) { $deptturnoutpercent = 0; } else { $deptturnoutpercent = ($tempcount / $count) * 100; } // conert 0, 1, .. to U0, U1, ... if (is_numeric($year)) { $year = "    U" . $year; } // print the stat echo("$year: " . toPercentage($deptturnoutpercent) . " voted ($tempcount/$count)
"); } echo("

"); } } // now, let's loop through all positions and their candidates to show the results // first we'll need all of the positions $query = "SELECT * FROM Positions WHERE ElectionID = $electionid ORDER BY PositionID;"; $result = mysql_query($query) or die("Query Error 54:" . mysql_error()); // go through each position and list the candidates running for it! if (mysql_num_rows($result) > 0) { echo("

Results for Position Elections for $electionname:

"); } while ($row = mysql_fetch_array($result)) { echo("

" . strtoupper($row['PositionTitle']) . "

"); $winners[$row['PositionTitle']] = ""; // let's get the votes for this position $totalquery = "SELECT * FROM Results WHERE ElectionID = $electionid AND PositionID = " . $row['PositionID'] . ";"; $totalresult = mysql_query($totalquery) or die("Query Error 62:" . mysql_error()); $totalvotes = mysql_num_rows($totalresult); // get the candidates for this position $innerquery = "SELECT * FROM Candidates WHERE PositionID = " . $row['PositionID'] . ";"; $innerresult = mysql_query($innerquery) or die("Query Error 67:" . mysql_error()); $candidatecount = mysql_num_rows($innerresult); if ($candidatecount == 1) { // there is only one candidate (ie it's yes or no) $candidate = mysql_fetch_array($innerresult); echo("
" . $candidate['FullName'] . "
"); // get the yes counts $votequery = "SELECT * FROM Results WHERE ElectionID=$electionid AND CandidateID=" . $candidate['CandidateID'] . ";"; $voteresult = mysql_query($votequery) or die("Query Error 75:" . mysql_error()); $yescount = mysql_num_rows($voteresult); // get the no counts (the negative value of the candidate id) $votequery = "SELECT * FROM Results WHERE ElectionID=$electionid AND CandidateID=-" . $candidate['CandidateID'] . ";"; $voteresult = mysql_query($votequery) or die("Query Error 80:" . mysql_error()); $nocount = mysql_num_rows($voteresult); // store the winning side $candidate = mysql_fetch_array($innerresult); if ($yescount > $nocount) { // he/she won $winners[$row['PositionTitle']] = $candidate['FullName']; } elseif ($yescount == $nocount) { // a draw! $winners[$row['PositionTitle']] = "Draw"; } else { // he/she lost $winners[$row['PositionTitle']] = "No one elected"; } // print out the yes versus no // print the results if ($totalvotes == 0) { // account for divide-by-zero $yespercent = 0; $nopercent = 0; } else { $yespercent = ($yescount/$totalvotes) * 100; $nopercent = ($nocount/$totalvotes) * 100; } echo("
Yes: " . toPercentage($yespercent) . " ($yescount/$totalvotes)
"); echo("
No: " . toPercentage($nopercent) . " ($nocount/$totalvotes)
"); } else { // there are multiple candidates // loop through each found candidate //determine the number of people who can win the position $countquery = "SELECT VoteCount FROM Positions WHERE PositionID=-" . $row['PositionID'] . ";"; $countresult = mysql_query($countquery) or die("Query Error 112:" . mysql_error()); $count = mysql_fetch_array($countresult); $winnercount = $count['VoteCount']; // loop through each candidate for the position while ($candidate = mysql_fetch_array($innerresult)) { // get the actual data of him/her and print it echo("
" . $candidate['FullName'] . "
"); // get the votes for this person $votequery = "SELECT * FROM Results WHERE ElectionID=$electionid AND CandidateID=" . $candidate['CandidateID'] . ";"; $voteresult = mysql_query($votequery) or die("Query Error 124:" . mysql_error()); $votecount = mysql_num_rows($voteresult); // print the results if ($totalvotes == 0) { // account for divide-by-zero $percent = 0; } else { $percent = ($votecount/$totalvotes) * 100; } echo("
" . toPercentage($percent) . " ($votecount/$totalvotes)
"); // store the max person in each category // if ($votecount > $max) { // $winners[$row['PositionTitle']] =$candidate['FirstName'] . " " . $candidate['LastName']; // $max = $votecount; // } elseif ($votecount == $max) { // //a tie! so concatanate the winners // $winners[$row['PositionTitle']] .= "," . $candidate['FirstName'] . " " . $candidate['LastName']; // $max = $votecount; // } $candidatevotecount[$votecount] = $row['PositionTitle']; //with this, we store the winners and their vote counts in sort of an opposite style //we can no use krsort() to sort the array by key in reverse order, and then use the vote count to get the first n positions (i.e. the winners) } } // print the spoiled ones $votequery = "SELECT * FROM Results WHERE ElectionID=$electionid AND PositionID=" . $row['PositionID'] . " AND CandidateID=-1;"; $voteresult = mysql_query($votequery) or die("Query Error 149:" . mysql_error()); $votecount = mysql_num_rows($voteresult); // print the results if ($totalvotes == 0) { // account for divide-by-zero $percent = 0; } else { $percent = ($votecount/$totalvotes) * 100; } echo("
Abstained
"); echo("
" . toPercentage($percent) . " ($votecount/$totalvotes)
"); echo("
"); } // now, let's loop through all referendums to show the results // first we'll need all of the positions $query = "SELECT * FROM Referendums WHERE ElectionID = $electionid;"; $result = mysql_query($query) or die("Query Error 165:" . mysql_error()); //go through each position and list the candidates running for it! if (mysql_num_rows($result) > 0) { echo("

Results for Referendum Questions:

"); } while ($row = mysql_fetch_array($result)) { echo("

" . strtoupper($row['ReferendumName']) . "

"); $winners[$row['ReferendumName']] = ""; // let's get the votes for this question $totalquery = "SELECT * FROM ReferendumResults WHERE ElectionID = $electionid AND ReferendumID = " . $row['ReferendumID'] . ";"; $totalresult = mysql_query($totalquery) or die("Query Error 173:" . mysql_error()); $totalvotes = mysql_num_rows($totalresult); // loop through each possible result (1 = yes, 0 = no, -1 = spoiled) $max = 0; // for the winners tracker for ($i = 1; $i >= -1; $i--) { // branch based on the result we are currently getting if ($i == 1 ) { echo("
" . $row['ReferendumQuestion'] . "
"); echo("
Yes: "); } elseif ($i == 0) { echo("
No: "); } else { echo("
"); echo("
Abstained
"); } // get the votes for this result $votequery = "SELECT * FROM ReferendumResults WHERE ElectionID=$electionid AND ReferendumID=" . $row['ReferendumID'] . " AND YesNoToQuestion=$i;"; $voteresult = mysql_query($votequery) or die("Query Error 190:" . mysql_error()); $votecount = mysql_num_rows($voteresult); //print the results if ($totalvotes == 0) { // account for divide-by-zero $percent = 0; } else { $percent = ($votecount/$totalvotes) * 100; } echo(toPercentage($percent) . " ($votecount/$totalvotes)
"); // add a clearing if we are not on the spoiled result if ($i <= 0) { echo("
"); } // store the max answer in each question if ($votecount > $max) { $winners[$row['ReferendumName']] = $i; $max = $votecount; } } echo("
"); } } } ?>