Hello,
I am trying to write this voting poll script, and it works fine so far, but i have this small problem: if i click to view results, and there haven't been any votes cast, then it repetas "no votes yet" as many times as there are choices.
currently i have 4 choices and so it prints the message 4 times instead of just once. Why does that happen ?
Here's the script:
<?php
require_once('connect.php');
$totalvotes = 0;
$ans = "SELECT *FROM P_choices";
$ans2 = mysqli_query($dbc, $ans) or die ("couldnt select from choices");
while ($ans3 = mysqli_fetch_array($ans2)) {
@$total = $total + $ans3['votes'];
}
// now display results
echo "total votes $total<br />";
$ans4 = "SELECT * FROM P_choices";
$ans5 = mysqli_query($dbc, $ans4) or die ("couldnt select chooiuces 2");
while ($ans6 = mysqli_fetch_array($ans5)) {
if ($total > 0) {
$imagewidth = (100*$ans6['votes']) / $total;
$imagewidth = round($imagewidth);
print "$ans6[answer]($imagewidth%)<br>";
} else {
echo "No votes yet<br />";// it repeats this 4 times because there are 4 choices
}
}
?>
i tried editing it like this
<?php
if (mysqli_num_rows($ans5) == 0) {
echo "No votes yet";
}
else {
while ($ans6 = mysqli_fetch_array($ans5)) {
if ($total > 0) {
$imagewidth = (100*$ans6['votes']) / $total;
$imagewidth = round($imagewidth);
print "$ans6[answer]($imagewidth%)<br>";
}
}
}
?>
but now it doesn't show display "no votes yet" at all.
what should i do ? thanks