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
Share on other sites

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
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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.