You'll need to know your way around fairly well to decipher this I suppose, but here is the code that I use on a page with the votes. To change the color of the bars, you just use different image files.
This is what I use at the top of the code, above everything else to use a cookie so that people can't vote multiple times within a month:
<?php
if (!headers_sent()) {
$pollid = 2;
$cookie_expire = 720; // hours
$endtime = time() + 3600 * $cookie_expire;
$check = $_POST;
if (!isset($_COOKIE['EBCPoll'][$pollid]) && ($check['pollselections'] != '')) {
$endtime = time() + 3600 * $cookie_expire;
setcookie("EBCPoll[$pollid]", "1", $endtime);
$ok = "yes";
header('Location:'.$_SERVER['PHP_SELF'].'?refresh=1');
}
if (isset($_COOKIE['EBCPoll'][$pollid])) {
$show_results = "block";
$show_poll = "none";
} else {
$show_results = "none";
$show_poll = "block";
}
}
?>
This is the code I use for the voting system php:
<?php
include_once 'database.php';
$db = new Database('main');
if ($_POST) {
if ($ok == "yes") {
$f = $_POST;
$query = "INSERT INTO poll_results (pollid, answerid) VALUES ('$pollid', '$f[pollselections]')";
//perform query
$db = new Database('main');
if ($db->query($query)) {
}
}
}
// Store the Question in a Variable
$db->query("SELECT * FROM poll_questions WHERE pollid='$pollid'");
while($db->fetch()) {
$question = $db->row['question'];
}
// Store the Answer Text in a Variable - Find the Total Number of Answers
$db->query("SELECT * FROM poll_answers WHERE pollid='$pollid'");
$i = 1;
while($db->fetch()) {
${'answer'.$i} = $db->row['answer'];
$i = $i + 1;
$answercount = $answercount + 1;
}
// Count the Number of Votes for each Answer
$db->query("SELECT * FROM poll_results WHERE pollid='$pollid'");
while($db->fetch()) {
$total = $total + 1;
for ($i = 1; $i <= $answercount; $i++) {
if ($db->row['answerid'] == $i) {
${'results'.$i} = ${'results'.$i} + 1;
}
}
}
// Calculate Percentages and Bar Length
for ($i = 1; $i <= $answercount; $i++) {
if (${'results'.$i} == 0) {
${'percent'.$i} = 0;
${'bar'.$i} = 0;
} else {
${'percent'.$i} = number_format(round((${'results'.$i} / $total), 2), 2, '', '');
${'bar'.$i} = (${'percent'.$i} * 1.5);
}
}
?>
And here is the form code where the voting and displaying results takes place:
<table style="margin: 0 auto 0 auto; border: 1px solid #555555; width: 180px; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 9px; display: <?= $show_results ?>" cellpadding="0" cellspacing="0" border="0">
<?php
echo '<tr><td style="background-color: #1B4778; width: 180px; height: 18px; color: #FFF; font-size: 11px"><center><strong>EBC Polls</strong></center></td></tr>'."\n\r";
echo '<tr>'."\n\r";
echo '<td><center><br /><strong>'.$question.'</strong></center><br /></td></tr>'."\n\r";
for ($i = 1; $i <= $answercount; $i++) {
echo '<tr><td><table style="margin: 0 auto 0 auto; width: 160px">';
echo '<tr><td>'.${'percent'.$i}.'% - '.${'answer'.$i}.'</td></tr>';
echo '<tr><td style="border: 1px solid #888888; width: 150px"><img src="/_2/g/pollbar_blue.jpg" height="8px" width='.${'bar'.$i}.'"px" /></td></tr>'."\n\r";
echo '</td></tr></table>';
echo '<tr><td> </td></tr>'."\n\r";
}
echo '<tr><td><center><font color="#888888">Total Votes - </font><font color="#CC0000">'.$total.'</font><br /><br /></center></td></tr>';
?>
</table>
<table style="margin: 0 auto 0 auto; border: 1px solid #555555; width: 180px; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 9px; display: <?= $show_poll ?>" cellpadding="0" cellspacing="0" border="0">
<tr>
<td style="background-color: #1B4778; width: 180px; height: 18px; color: #FFF; font-size: 11px"><center><strong>EBC Polls</strong></center></td>
</tr>
<form name="poll" method="post" action="<?=htmlentities($_SERVER['REQUEST_URI'])?>" enctype="multipart/form-data">
<tr style="width: 150px">
<td style="padding-left: 2px"><center><br /><strong><?= $question ?></strong></center></td>
</tr>
<tr>
<td> </td>
</tr>
<?php
for ($i = 1; $i <= $answercount; $i++) {
echo '<tr style="width: 150px">'."\n\r";
echo '<td style="width: 150px"><input name="pollselections" type="radio" value="'.$i.'" />'.${'answer'.$i}.'</td>'."\n\r";
echo '</tr>'."\n\r";
}
?>
<tr>
<td> </td>
</tr>
<tr>
<td><center><input type="submit" name="Submit" value="Submit" /></center><br /></td>
</tr>
</form>
</table>
Good luck!