SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: Poll Problem

  1. #1
    SitePoint Zealot
    Join Date
    Nov 2002
    Posts
    142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Poll Problem

    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) * 1000"."'') . "%<br>"?></td>
        </tr>
        <?php
                   
    }
         }
        
    ?>
        <tr>
         <td colspan="3">
          <?php echo "<span class=\"smallbold\">Result based on " $answertotal " response" . ($answertotal == "" "s") . ".</span>"?><br><br>
                      <a href="/pollarchives.php"><div style="text-align:center">Poll Archives</div></a><br>
         </td>
        </tr>
       </form>     
       </table>
      <?php
     
    }
    ?>

  2. #2
    SitePoint Wizard Sillysoft's Avatar
    Join Date
    May 2002
    Location
    United States :)
    Posts
    1,691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try taking out the or die part where the message you get comes up. Then see if that works. For testing purposes of course. I had a similiar issue and it was the or die part

    Silly

  3. #3
    SitePoint Zealot
    Join Date
    Nov 2002
    Posts
    142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you want to go see yourself, I took out the or die. All I get is the Result based on 0 responses. Nothing else shows up. Any ideas?

  4. #4
    SitePoint Zealot
    Join Date
    Nov 2002
    Posts
    142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    **UPDATE**

    I manually added a response to the database. When it shows one response there, that's what you're supposed to see before you vote. How can I make it show that when there are zero votes in the database?

  5. #5
    SitePoint Zealot
    Join Date
    Nov 2002
    Posts
    142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    *bump to the top*

    I'm still having the problems here. I was thinking that it had to do with dividing by 0, but I am not sure of that.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •