...

View Full Version : So close to the solution please HELP with php POLL



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

MattF
11-18-2010, 04:20 AM
Only had a quick peek, bit your main problem seems to be here:



while ($row = $result->fetch_object())
{
$total_votes += $row->num_votes;
}


That should be something like:



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

fjanos
11-18-2010, 04:32 AM
Thanx for the quick peek.
It gives me this error:
"Fatal error: Call to undefined method mysqli_result::num_rows() in"

MattF
11-18-2010, 04:36 AM
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:



foreach ($total as $name => $numvotes)

fjanos
11-18-2010, 04:39 AM
A solution must be found so it gives the output

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.

MattF
11-18-2010, 04:41 AM
A solution must be found so it gives the output

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:



foreach ($total as $name => $numvotes)
{
print('Name: '.$name.' Votes: '.(($total_votes/$numvotes)*100));
}

fjanos
11-18-2010, 05:13 PM
I tried it
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
/*******************************************
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>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum