Democratic
'; $colhead_2 = '
Republican
'; $colhead_0 = '
Open Primary
'; $colsub_all = '
ALL VOTERS
'; $colsub_eligible = '
ELLIGIBLE VOTERS
'; $colspacer = '
 
'; $colend = '
'; $tableend = '
'; // database query functions function getVoteTable($p, $r, $e) { // $p = party, $r = rank, $e = elligible if ($p == 0) { $p1 = " "; $p2 = " "; } else { $p1 = "AND c.party_id = " . $p; $p2 = "WHERE `party_id` = " . $p; } if ($e != '0') { $e = " AND `voter_id` IN ( SELECT `id` FROM `voters` WHERE `eligible` = 1 )"; } else $e = ' '; $q = "SELECT cname, letter, cvotes, round(cvotes / tot_votes * 100) AS pct, tot_votes FROM ( SELECT p.`letter`, CONCAT(c.`fname`, ' ', c.`lname`) AS cname, c.lname, v.votes AS cvotes FROM `candidates` c , `parties` p , ( SELECT `candidate_id`, COUNT(*) AS votes FROM `votes` WHERE rank = " . $r . $e . " GROUP by `candidate_id` ) v WHERE c.`id` = v.`candidate_id` AND c.`party_id` = p.`id` ". $p1 . " ) AS s1, ( SELECT COUNT(*) AS tot_votes FROM `votes` WHERE rank = " . $r . $e . "AND `candidate_id` IN ( SELECT `id` FROM `candidates` " . $p2 . " ) ) AS s2 ORDER BY cvotes DESC, lname"; echo mysql_error(); $rtn = mysql_query($q); return $rtn; }; function buildTable($q) { $table = ''; $tot_votes = mysql_result($q, 0, "tot_votes"); $table = $table . '
' . number_format($tot_votes) . ' votes
'; $table = $table . '
  CANDIDATE
VOTES 
%   
'; $ii=0; $numrows = mysql_numrows($q); while ($ii < $numrows) { $cname = mysql_result($q,$ii,"cname"); $votes = mysql_result($q,$ii,"cvotes"); $pct = explode(".", mysql_result($q,$ii,"pct")); $bc = $ii % 2; $table = $table . ' ' . ' ' . ' ' . ' ' . ' ' . ' '; $ii++; } $table = $table . '
' . '' . '' . $cname . '' . number_format($votes) . '' . $pct[0] . '
'; return $table; } function writeFile ($filename, $content) { $file = "/home/18852/domains/onlineprimary.us/html/" . $filename; $fp = fopen ("$file", "wb"); fwrite($fp, $content); fclose($fp); echo "Wrote to ($file) "; } function setLastCalcTime() { $q = "UPDATE `votecount` SET `results_updated_at` = now() WHERE `rowid` = 1"; mysql_query($q); } $maxrank = 5; $parties = array(1, 2, 0); $link = mysql_connect($server,$username,$password); mysql_select_db($database) or die( "Unable to select database"); // ranks - loop through 1st choice through 5th choice for ( $r = 1; $r <= $maxrank; $r++ ) { $content = ''; // outer loop - ranks foreach ($parties as $p) { // inner loop - parties switch ($p) { case "1" : $colhead = $colhead_1; break; case "2" : $colhead = $colhead_2; break; default : $colhead = $colhead_0; } $colbody_a = buildTable( getVoteTable($p, $r, '0') ); $colbody_e = buildTable( getVoteTable($p, $r, '1') ); $content = $content . $colhead . $colsub_all . $colbody_a . $colspacer . $colsub_eligible . $colbody_e . $colend; } // parties $content = $content . $tableend; $filename = "results-t" .$r . ".php"; writeFile($filename, $content); } // ranks // anybody-but $content = ''; $r = -1; foreach ($parties as $p) { // inner loop - parties switch ($p) { case "1" : $colhead = $colhead_1; break; case "2" : $colhead = $colhead_2; break; default : $colhead = $colhead_0; } $colbody_a = buildTable( getVoteTable($p, $r, '') ); $colbody_e = buildTable( getVoteTable($p, $r, '1') ); $content = $content . $colhead . $colsub_all . $colbody_a . $colspacer . $colsub_eligible . $colbody_e . $colend; } // parties $content = $content . $tableend; $filename = "results-t6.php"; writeFile($filename, $content); setLastCalcTime(); mysql_close(); ?>