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.

To allow users to vote on our poll we need to setup a form. This script will be much like the script to output the results, except this time, instead of having the for loop echo the results for each option, we will have it echo a radio button for each one.


<?php

$self 
$_SERVER['PHP_SELF'];

// 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);
?>
<form action="<?php echo $self?>" method="post">
<?php

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

for(
$x=0$x<$total_options$x++) {
    echo 
'<input type="radio" name="option" value="'.$x.'"';
    if(
$x == 0) echo ' checked';
    echo 
' /> '.$options[$x]."<br />\n";
}

?>
<br /><input type="submit" name="vote" value="Vote" />
<input type="submit" name="vote" value="View" />
</form>

Ok, that's the script to setup the form. Basically, it just grabs all the information we need first, like in results.php. After we have that information, we output a form, with the action as $self, which holds the path to the file it is called from, and we give it a post method.

Then we just loop through the options and echo out the option and a radio button. Next we write out some submit buttons, one to submit the vote, and another to view the results.

If you test that script, you'll see that clicking either submit button doesn't do anything, we still have to setup that part. To accomplish this we'll use the isset() function to see if either submit button has been clicked. Below is what the final vote.php script looks like.

<?php

$self 
$_SERVER['PHP_SELF'];

// 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);

// if the vote form has not been submitted
if(!isset($_POST['vote'])) {
?>
<form action="<?php echo $self?>" method="post">
<?php

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

for(
$x=0$x<$total_options$x++) {
    echo 
'<input type="radio" name="option" value="'.$x.'"';
    if(
$x == 0) echo ' checked';
    echo 
' /> '.$options[$x]."<br />\n";
}

?>
<br />
<input type="submit" name="vote" value="Vote" />
<input type="submit" name="vote" value="View" />
</form>
<?php
} else {
    
// if the vote form has been submitted
    
    
if($_POST['vote'] == 'Vote') {
        
// if the user clicked 'Vote'
        
$poll_data['votes'][$_POST['option']]++;
        
$handle fopen($dat_file,'w');
        
fwrite($handle,serialize($poll_data));
        
fclose($handle);
        require(
'results.php');
    } else {
        
// else, the user clicked 'View'
        
require('results.php');
    }
}
?>

Line 19 checks whether or not either submit button has been clicked. If not, it outputs the form. If so, lines 40-52 are executed. Line 42 checks if the submit button the user clicked was the 'Vote' button. If so, the script increments the vote count for that option by one, writes the poll data to the file, then displays the results by included the results.php file with require(). Otherwise, the the 'View' button has been clicked, so the script includes the results.php file without incrementing the vote count.

Thats it, the poll is complete. The users can now view the results and vote in the poll. To create a new poll, all you have to do is edit the $poll array in the setup.php file, then run the script again.

With a little more effort you can setup the script to handle multiple polls and maybe even comments, like the ones here at ronsguide.com. That's all for this tutorial, hope it was helpful to you. Smile

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