Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Jul 2010
    Location
    Pahun Republic
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Unhappy So close to the solution please HELP with php POLL

    I'm trying to create PHP poll. I know I'm close but I got stuck. If anyone can help me with this.

    Code:
    <html>
    <head>
      <title>Polling</title>
    <head>
    <body>
    <h1>Pop Poll</h1>
    <p>Who will you vote for in the election?</p>
    <form method=post action="show_poll.php">
    <input type="radio" name="vote" value="John Smith">John Smith<br />
    <input type="radio" name="vote" value="Mary Jones">Mary Jones<br />
    <input type="radio" name="vote" value="Fred Bloggs">Fred Bloggs<br /><br />
    <input type=submit value="Show results">
    </form>
    </body>
    Here is the show_poll.php

    PHP Code:
    <?php
    /*******************************************
      Database query to get poll info
    *******************************************/

    // get vote from form
    $vote=$_REQUEST['vote'];

    // log in to database
    if (!$db_conn = new mysqli('localhost''poll''poll''poll'))
    {
      echo 
    'Could not connect to db<br />';
      exit;
    }

    if (!empty(
    $vote))  // if they filled the form out, add their vote
    {
      
    $vote addslashes($vote);
      
    $query "update poll_results
                set num_votes = num_votes + 1
                where candidate = '$vote'"
    ;
      if(!(
    $result = @$db_conn->query($query)))//'@' means not showing error message.
      
    {
        echo 
    'Could not connect to db<br />';
        exit;
      }
    } else {

        

    echo 
    'You did not vote!<br />';

        

    exit;
    }
    // get current results of poll, regardless of whether they voted
    $query 'select * from poll_results';
    if(!(
    $result = @$db_conn->query($query)))
    {
      echo 
    'Could not connect to db<br />';
      exit;
    }
    $num_candidates $result->num_rows;//how many candidates are =  the number of rows
    // calculate total number of votes so far
    $total_votes=0;
    while (
    $row $result->fetch_object())
    {
        
    $total_votes +=  $row->num_votes;

        


    }
    $result->data_seek(0);  // reset result pointer



    ?>
    <h2>Result:</h2>
    <table>
    <tr>
    <td>John Smith:</td>
    <td>
    <img src="poll.gif"
    width='<?php echo(100*($num_candidates/$total_votes)); ?>'
    height='20'>
    <?php echo(100*($num_candidates/$total_votes)); ?>%
    </td>
    </tr>
    <tr>
    <td>Mary Jones:</td>
    <td>
    <img src="poll.gif"
    width='<?php echo(100*($num_candidates/$total_votes)); ?>'height='20'>
    <?php echo(100*($num_candidates/$total_votes)); ?>%
    </td>
    </tr>
    <tr>
    <td>Fred Bloggs:</td>
    <td>
    <img src="poll.gif"
    width='<?php echo(100*($num_candidates/$total_votes)); ?>'height='20'>
    <?php echo(100*($num_candidates/$total_votes)); ?>%
    </td>
    </tr>
    </table>
    the Problem:
    When I click a candidate it increments the value in the database it gets recorded well but I have problem with showing the result. It doesn't show the result right. I know that the bug lays here:
    Code:
    100*($num_candidates/$total_votes))
    Instead of $num_candidates variable I need to come up with another variable that is the number of votes each candidate gets. I don't know how to come up with that. Any help??
    Please help me with this the point is that I don't want to use "If else" statements.
    Thanks any help a lot in advance

  • #2
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Only had a quick peek, bit your main problem seems to be here:

    Code:
    while ($row = $result->fetch_object())
    {
        $total_votes +=  $row->num_votes;
    }
    That should be something like:

    Code:
    $total = array();
    $total_votes = 0;
    
    while ($row = $result->fetch_object())
    {
        $total[$row['candidate']] = $row['num_votes'];
        $total_votes = ($total_votes+$row['num_votes']);
    }
    $num_candidates = count($total);

    Totally untested of course.
    Last edited by MattF; 11-18-2010 at 04:29 AM.

  • #3
    New Coder
    Join Date
    Jul 2010
    Location
    Pahun Republic
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanx for the quick peek.
    It gives me this error:
    "Fatal error: Call to undefined method mysqli_result::num_rows() in"

  • #4
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Quote Originally Posted by fjanos View Post
    Thanx for the quick peek.
    It gives me this error:
    "Fatal error: Call to undefined method mysqli_result::num_rows() in"
    No idea what the function names are in your DB class, (or even if you have certain functions available). That's example code which you'll need to adapt to your specific situation. It's not expected to be perfect as is.


    Edit: I think I've already updated that bit of the code anyhows though. Try the code in my first post as it is now. I've made several revisions since first posting it.

    You'll also end up with the $total array which should contain the name => num_votes key/value pair, so just loop through the $total array with a foreach such as:

    Code:
    foreach ($total as $name => $numvotes)
    Last edited by MattF; 11-18-2010 at 04:40 AM.

  • #5
    New Coder
    Join Date
    Jul 2010
    Location
    Pahun Republic
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    vote x candidate gets/total votes

    A solution must be found so it gives the output
    PHP Code:
    echo(100*($vote_x_candidate_gets/$total_votes)) 
    I don't know how to create a variable that has the vote number that one candidate gets.

  • #6
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Quote Originally Posted by fjanos View Post
    A solution must be found so it gives the output
    PHP Code:
    100*(echo($vote_x_candidate_gets/$total_votes)) 
    I don't know how to create a variable that has the vote number that one candidate gets.
    I've added info on how you access that in my post above. Using your existing math, it should be along the lines of:

    Code:
    foreach ($total as $name => $numvotes)
    {
        print('Name: '.$name.' Votes: '.(($total_votes/$numvotes)*100));
    }
    Last edited by MattF; 11-18-2010 at 05:37 AM.

  • #7
    New Coder
    Join Date
    Jul 2010
    Location
    Pahun Republic
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I tried it
    Code:
    foreach ($total as $name => $numvotes)
    {
        print('Name: '.$name.' Votes: '.(($total_votes/$numvotes)*100));
    }
    it doesn't work. I changed properties database clas objects and everything according to my script.
    what I found as a solution and it works:
    PHP Code:
    <?php
    /*******************************************
      Database query to get poll info
    *******************************************/

    // get vote from form
    $vote=$_REQUEST['vote'];

    // log in to database
    if (!$db_conn = new mysqli('localhost''poll''poll''poll'))
    {
      echo 
    'Could not connect to db<br />';
      exit;
    }

    if (!empty(
    $vote))  // if they filled the form out, add their vote
    {
      
    $vote addslashes($vote);
      
    $query "update poll_results
                set num_votes = num_votes + 1
                where candidate = '$vote'"
    ;
      if(!(
    $result = @$db_conn->query($query)))//'@' means not showing error message.
      
    {
        echo 
    'Could not connect to db<br />';
        exit;
      }
    } else {
        echo 
    'You did not vote!<br />';
        exit;
    }
    // get current results of poll, regardless of whether they voted
    $query 'select * from poll_results';
    if(!(
    $result = @$db_conn->query($query)))
    {
      echo 
    'Could not connect to db<br />';
      exit;
    }
    $num_candidates $result->num_rows;//how many candidates are =  the number of rows
    // calculate total number of votes so far
    $total_votes=0;

    while (
    $row $result->fetch_object())
    {
        
    $total_votes +=  $row->num_votes;
        
    }
    $result->data_seek(0);  // reset result pointer
    $table_html "";
    #fetch_object() will return the current row result set as an object where 
    #the attributes of the object represent the names of the fields found within the result set. 
    while ($row $result->fetch_object()) //kiiratás. Outputs candidates list with the number of votes they get
    {
       
    $percent 100*round($row->num_votes/$total_votes,4);
       
    $table_html .= '<tr><td><img src="poll.gif" width="'$percent '" height="20">' $percent '%</td><td>'.$row->candidate.'</td></tr>';
    }
    ?>

    <h2>Result:</h2>
    <table><?php echo $table_html?></table>


  •  

    Posting Permissions

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