xgd Posted July 20, 2009 Share Posted July 20, 2009 Helo, 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: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <?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 } } ?> </body> </html> Link to comment https://forums.phpfreaks.com/topic/166694-little-script-problem/ Share on other sites More sharing options...
gijew Posted July 20, 2009 Share Posted July 20, 2009 You answered your own question kind of. The "No votes yet" is IN the loop. If you only want it once do something like... <?php if (mysql_num_records($sql) == 0) { echo 'no votes'; } else { while ($row = mysql_fetch_array($sql)) { ... } } ?> Link to comment https://forums.phpfreaks.com/topic/166694-little-script-problem/#findComment-878984 Share on other sites More sharing options...
xgd Posted July 21, 2009 Author Share Posted July 21, 2009 Hey man, 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 Link to comment https://forums.phpfreaks.com/topic/166694-little-script-problem/#findComment-879353 Share on other sites More sharing options...
xgd Posted July 21, 2009 Author Share Posted July 21, 2009 ? Link to comment https://forums.phpfreaks.com/topic/166694-little-script-problem/#findComment-879478 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.