Jump to content

So close to the solution please HELP with php object oriented POLL


pahunrepublic

Recommended Posts

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

 

It's a radio button poll:

<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
/*******************************************
  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:

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

Please help me with this I'd like to have an object oriented solution the point is that I don't want to use  "If else" statements.

Thanks any help a lot in advance

Link to comment
Share on other sites

after

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

add in

 

$table_html = "";
while ($row = $result->fetch_object())
{
   $percent = 100*($row->num_votes/$total_votes);
    $table_html .= '<tr><td><img src="poll.gif" width="'. intval($percent) . '" height="20">' . $percent . '%</td></tr>';
}

 

then for your html:

<table><?=$table_html?></table>

Link to comment
Share on other sites

just spotted a mistake. your first column shows the name of the candidate, so the line in the loop will have to reflect that. I don't know your column names, so change it accordingly

 

$table_html .= '<tr><td>' . $row->candidate_name. '</td><td><img src="poll.gif" width="'. intval($percent) . '" height="20">' . $percent . '%</td></tr>';

 

and yes, avoid short tags ;P

Link to comment
Share on other sites

just spotted a mistake. your first column shows the name of the candidate, so the line in the loop will have to reflect that. I don't know your column names, so change it accordingly

 

$table_html .= '<tr><td>' . $row->candidate_name. '</td><td><img src="poll.gif" width="'. intval($percent) . '" height="20">' . $percent . '%</td></tr>';

 

and yes, avoid short tags ;P

Thank you seanlim. It works!!. Yes After you gave me the first post I added the candidate name also. Man I love PHP but I don't have that programmer way of thinking that you have guys. I wish I had. I was so close to the solution but I couldn't find it.

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.