pahunrepublic Posted November 18, 2010 Share Posted November 18, 2010 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 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 Quote Link to comment https://forums.phpfreaks.com/topic/219032-so-close-to-the-solution-please-help-with-php-object-oriented-poll/ Share on other sites More sharing options...
trq Posted November 18, 2010 Share Posted November 18, 2010 A little off topic but there is nothing object oriented about your code. Quote Link to comment https://forums.phpfreaks.com/topic/219032-so-close-to-the-solution-please-help-with-php-object-oriented-poll/#findComment-1135902 Share on other sites More sharing options...
seanlim Posted November 18, 2010 Share Posted November 18, 2010 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> Quote Link to comment https://forums.phpfreaks.com/topic/219032-so-close-to-the-solution-please-help-with-php-object-oriented-poll/#findComment-1135957 Share on other sites More sharing options...
Pikachu2000 Posted November 18, 2010 Share Posted November 18, 2010 Just to answer the "why won't it show up" question before it gets asked, I'd recommend not using the short <?= "quick echo" syntax, and opt for the full <?php echo instead. Quote Link to comment https://forums.phpfreaks.com/topic/219032-so-close-to-the-solution-please-help-with-php-object-oriented-poll/#findComment-1136070 Share on other sites More sharing options...
seanlim Posted November 18, 2010 Share Posted November 18, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/219032-so-close-to-the-solution-please-help-with-php-object-oriented-poll/#findComment-1136074 Share on other sites More sharing options...
pahunrepublic Posted November 18, 2010 Author Share Posted November 18, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/219032-so-close-to-the-solution-please-help-with-php-object-oriented-poll/#findComment-1136165 Share on other sites More sharing options...
pahunrepublic Posted November 18, 2010 Author Share Posted November 18, 2010 Just to answer the "why won't it show up" question before it gets asked, I'd recommend not using the short <?= "quick echo" syntax, and opt for the full <?php echo instead. Why don't you recommend the quick echo syntax? I heard that one before it shouldn't be used but I don't know why? Quote Link to comment https://forums.phpfreaks.com/topic/219032-so-close-to-the-solution-please-help-with-php-object-oriented-poll/#findComment-1136166 Share on other sites More sharing options...
Pikachu2000 Posted November 18, 2010 Share Posted November 18, 2010 The short open tag syntax can be disabled in php.ini. If you always use the proper full tags, short tags being disabled on a server can't come back to haunt you later. Quote Link to comment https://forums.phpfreaks.com/topic/219032-so-close-to-the-solution-please-help-with-php-object-oriented-poll/#findComment-1136179 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.