I'm having a problem with a poll script here. It's supposed to show the current results along with the radio button to allow the user to vote. However, if there are no votes in the database, it outputs: Couldn't view poll. You must vote first.
What am I missing here?
Here's the code:
PHP Code:
function ShowPoll()
{
/* Allows user to choose, shows current results */
global $recent_poll;
$query = "SELECT * FROM poll_questions pq INNER JOIN poll_answers pa ON pq.id = pa.poll_id WHERE pq.id = $recent_poll";
$answertally[0] = 0;
$answertally[1] = 0;
$answertally[2] = 0;
$answertally[3] = 0;
$answertally[4] = 0;
$answertotal = 0;
if($result = mysql_query($query))
{
while($row = mysql_fetch_array($result))
{
switch($row["answer"])
{
case $row["answer1"]:
$answertally[0]++;
break;
case $row["answer2"]:
$answertally[1]++;
break;
case $row["answer3"]:
$answertally[2]++;
break;
case $row["answer4"]:
$answertally[3]++;
break;
case $row["answer5"]:
$answertally[4]++;
break;
}
}
// Get the total number of votes
for($i = 0; $i < sizeof($answertally); $i++)
$answertotal += $answertally[$i];
@mysql_data_seek($result, 0);
$row = mysql_fetch_array($result) or die("Couldn't view poll. You must vote first.");
}
else
{
die("An error occured while trying to display this poll's results.");
}
?>
<table width="100%" border="0" bgcolor="#C0C0C0" cellpadding="2" cellspacing="0">
<form name="vote" action="poll.php?poll_id=<?php echo $row["poll_id"]; ?>" method="post">
<tr>
<td colspan="3">
<span class="bigboldblue"><?php echo $row["title"]; ?></span>
</td>
</tr>
<tr>
<td colspan="3">
<?php echo $row["description"]; ?>
</td>
</tr>
<tr>
<td colspan="3">
<span class="midboldblue"><?php echo $row["question"]; ?></span>
</td>
</tr>
<?php
for($i = 1; $i <= 5; $i++)
{
if($row["answer$i"] != "")
{
?>
<tr>
<td width="10%"><input type="radio" name="answer" value="<?php echo $row["answer$i"]; ?>" onClick="this.form.submit(); return true;"></td>
<td valign="middle"><?php echo $row["answer" . $i]; ?></td>
<td align="right"><?php echo number_format(($answertally[$i-1] / $answertotal) * 100, 0, ".", '') . "%<br>"; ?></td>
</tr>
<?php
}
}
?>
<tr>
<td colspan="3">
<?php echo "<span class=\"smallbold\">Result based on " . $answertotal . " response" . ($answertotal == 1 ? "" : "s") . ".</span>"; ?><br><br>
<a href="/pollarchives.php"><div style="text-align:center">Poll Archives</div></a><br>
</td>
</tr>
</form>
</table>
<?php
}
?>
Bookmarks