Adding a Poll to Your Web Site - The Complete showvotes.php
(Page 4 of 6 )
Below is the complete code to showvotes.php. The other two files, poll.php and bargraph.php, have already appeared previously in their entirety.
<?php $host = 'localhost'; $user = 'user'; $pass = 'pass'; $db = 'polls'; print "<html>\n"; print "<head>\n"; print "<title>Vote totals</title>\n"; print "</head>\n"; print "<body>\n"; $dbcon = mysql_connect($host, $user, $pass) or die('Unable to connect to server ' . $host); mysql_select_db($db) or die('Unable to find database ' . $db); if(isset($_POST['vote']) && ctype_digit($_POST['vote'])) { $query = 'UPDATE poll_answers SET votes=votes+1 WHERE choice=' . $_POST['vote']; $result = mysql_query($query); } $num_votes_query = 'SELECT SUM(votes) AS sumvotes FROM poll_answers'; $num_votes_query .= ' WHERE poll_num=1'; if ($result = mysql_query($num_votes_query)) { $row = mysql_fetch_array($result); $sum = $row['sumvotes']; } $totals_query = 'SELECT activity, votes FROM poll_answers ';
if ($result = mysql_query($totals_query)) { print "<table>\n"; print "<tr><th>Activity</th><th>Votes</th></tr>\n"; while($row = mysql_fetch_array($result)) { print "<tr><td>" . $row['activity'] . "</td>\n"; print "<td align=\”right\”>" . $row['votes'] . "</td></tr>\n"; if($sum) { $percent = round($row['votes'] * 100 / $sum); print "<tr><td><img src=\”bargraph.php?pct=$percent\”></td>\n"; print "<td align=\”right\”>" . $percent . "%</td></tr>\n"; }
} print "</table>\n"; } print "</body>\n"; print "</html>\n"; ?> |
Next: Expanding the Poll >>
More Database Articles Articles
More By Codewalkers
blog comments powered by