fjanos
11-18-2010, 04:09 AM
I'm trying to create PHP poll. I know I'm close but I got stuck. If anyone can help me with this.
<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 the point is that I don't want to use "If else" statements.
Thanks any help a lot in advance
<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 the point is that I don't want to use "If else" statements.
Thanks any help a lot in advance