Tutorial: How to build a poll system with PHP and mySQL

Pages: 1 2 3 4 5 6 7 8 9

At this point you will have a working poll system. But what if you want to add your own questions and make new polls? Simple. All you need to do is edit the contents of the database in two places.

8. Firstly open up phpmyadmin. Then click on the database you made and then on the table name poll. Here is where all your polls are stored. Click on the button Insert. Simply make a title for your poll and click go. Next click browse. You will see your entry there, along with an id number. Remember this id number and click on the table name questions.

Do the same with insert, however type in the poll id (pid) which is the number you just remembered, and then the question title. Repeat this process with the question titles and the same pollid until you have all the possible responses you want!

And thats it! You have now set up a new poll. Remember at the start of the tutorial we said we would only show the newest poll, well since your new poll has a greater id number than the previous, it is considered the newest now.

*UPDATE*
The poll bars did not extend to the entire width of the area, that is because we were using strict percentages, in comparison to the total amount of votes. In order to make the bars extend to the full width, and in doing so making the option with the most votes equal 100% we must compare all other votes with that of the most voted option. So to do so open up poll.php and:

After:

  1. if($rows > 0){
  2.     $poll = mysql_fetch_array($query);
  3.     $title = $poll['name'];
  4.   } else {
  5.     $title = 'No Poll Yet';
  6.   }

Add:

  1. $query = mysql_query("SELECT COUNT(`id`) as hits FROM `responses` GROUP BY `qid`");
  2.   while($row = mysql_fetch_array($query)){
  3.     $me[] = $row['hits'];
  4.   }
  5.   $max = max($me);

Next Find:

  1. $percentage = round(($responses['total'] / $total) * 100);

And replace with:

  1. $percentage = round(($responses['total'] / $max) * 100);

Done! Now your bars are constructed in comparison to the most popular option!

Pages: 1 2 3 4 5 6 7 8 9

Tags
, , , , , , , , , , , , , , ,

Post a Comment