Jump to content

Little script problem


xgd

Recommended Posts

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.