Navigation
Poll
Would you be an active poster if Ron's Guide had a message board?


Total Votes: 62
Comments: 18 — View
Past pollsPoll idea?
Rate Ron's Guide
Rate our resource at Bigwebmaster.com
Flat File Poll
Learn how to create your own poll script without a MySql database using PHP and flat files databases.

Now that we have our poll data stored, we can make a script to read this data and output the results of the poll.

Create a new file, I'm going to call mine results.php. Type or Copy/Paste the following code into results.php:

<?php

// get the poll data
$dat_file 'poll.dat';
$poll_data unserialize(file_get_contents($dat_file));

// define question, options, and votes
$question $poll_data['question'];
$options $poll_data['options'];
$votes $poll_data['votes'];

// count the number of options and votes
$total_options count($options);
$total_votes array_sum($votes);

// echo out the poll question
echo $question."<br /><br />\n";

// loop through each option
for($x=0$x<$total_options$x++) {
    
// calculate percentage if the number of votes
    // is NOT zero otherwise, the percentage is zero
    
$percentage = ($total_votes != 0) ? 
    
number_format($votes[$x] * 100 $total_votes) : 0;

    
// echo out the option
    
echo $options[$x].'<br />';

    
// echo out the number of votes and percentage
    
echo $votes[$x].' votes - '.$percentage."%<br /><br />\n";
}

echo 
'Total votes: '.$total_votes.'<br />';

?>

It may look like a lot, but the script above really isn't that hard to understand. I've tried commenting in it, so that should give you a better idea what's going on. I'll go through and explain each bit.

Lines 4 and 5 get our poll data from the file and unserialize it. This allows us to use the information just like a normal array.

Lines 8, 9 and 10 just define some variables that will be used later in the script. Pretty much the same for lines 13 and 14, they count the number of options our poll has and how many votes have been made on it.

Line 17 echos the poll question. Finally, we get to the most important part of the script, lines 20-33. This is a for loop, the loop starts at zero and ends when it has looped the number of option we have in our poll. Each time through the loop the script calculates the percentage of votes each option has received. Then it just does a couple of echos, it echos the option itself and the number of votes and the percentage of votes for that option.

Finally, line 35 echos the total number of votes the poll has recorded.
If you go ahead and run the results.php file, you should come up with somthing like this:


This is a poll question?

Yes
0 votes - 0%

No
0 votes - 0%

Huh?
0 votes - 0%

Total votes: 0

Great! Our results are being displayed, now all we have to do is the voting part of the poll. Go on to the next page to see how this is done.

« Previous [ 1 2 3 ] Next »
Discuss Tutorial: Flat File Poll 93 Comments
Comment by Ron on Aug 15, 2004, 8:21 am
Please post questions or comments about this tutorial below. Smile
Comment by David on Aug 15, 2004, 9:51 am
This is an excellent flatfile database tutorial. You did well Ron, great work! Once I managed to debug my script I got it working pretty well, the bugs were from me typing it up by hand, not from the script it self. Once againt, excellent work Ron!
Comment by Flash on Aug 15, 2004, 2:27 pm
Wassat Gotta love the way you've set it out, Ron. Smile
Comment by jordan on Aug 20, 2004, 2:52 am
can i use this on my site lol
also the to codes on the last page what page do u add them into or do u add the mwhere u wont the poll to be placed
Comment by Ron on Aug 20, 2004, 12:09 pm
Thanks for the comments, David, and Flash, glad you like the tutorial Smile

Jordan, you are free to use the script for personal use. The very last code is to be saved as vote.php. When you have all of the files in the same directory, just put somthing like:

<?php
require('/path/to/poll/vote.php');
?>

To display the poll. Good luck Smile
Comment by jordan on Aug 20, 2004, 6:57 pm
thanks dude Smile im happy now Tongue it will be on my portfolio is that ok
Comment by Ron on Aug 21, 2004, 4:08 am
Yep, that's fine. Smile
Enjoy Grin
Comment by Syko Nachoman on Aug 26, 2004, 9:16 pm
Great script! Cool I just have a small problem...the poll shows up twice! Wassat How do I get it to show up only once? Sad
Comment by Ron on Aug 28, 2004, 2:19 am
Are you sure you copied the script correctly? Maybe you put two requires()? Wassat
Comment by manne on Sep 2, 2004, 3:22 pm
Can you add a tutorial on this poll that makes the user to only vote one time?

« Previous [ 1 2 ... 8 9 10 ] Next »
Post a comment
Sorry, you must be a registered member to post comments.

If you would like to register, you can do so here.
If you already have an account, please login.