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.

I've seen a few tutorials online that show you how to create a poll using PHP, but none that did it without a mysql database. This tutorial will try to do just that.

First of all, if you haven't read my Flat File Databases tutorial, or you do not have any prior experience with storing/retrieving information from flat files, go ahead and read that tutorial before you continue.

We're going need a place to work, so go ahead and create a new directory named poll, or whatever you want, so that we can store all of the files necessary for this tutorial in one place.

Ok, now that we've got our directory, let's get started. We need to setup an array that will store the information for our poll(s). This array will need three items. The first item will be the poll question. The second will be another array of the possible options of the poll. The final item will be another array of the votes.

What we're going to have is what is called a multidimensional array. Below is the code for our poll array. Create a new file named 'setup.php' and type or copy/paste the following:

<?php

$poll 
= array(
     
'question' => 'This is a poll question?',
     
'options' => array('Yes''No''Huh?'),
     
'votes' => array(000),
);

Each item in the array of options will correspond to each item in the array of votes. This is how we will calculate how many votes have been made for each option.

Now that we have our array setup, we need to serialize it and stick it into a file, so that we can use is later for adding to our poll data and calculating the poll results. Go ahead and create a new file, I'm going to call mine 'poll.dat'. Stick it in your poll directory that you made earlier.

Add this to your setup.php file then run it:

$dat_file 'poll.dat';
$poll_data serialize($poll);
$handle fopen($dat_file,'w+');
fwrite($handle,$poll_data);
fclose($handle);

?>

After you run setup.php you should see a new file in your poll directory, named 'poll.dat'. If you get errors when you run the script, try CHMODing your poll directory to 777 or 755.

Go on to the next page, where we'll learn how to calculate the results.

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