home - php help - faq - download
amazing little poll - code help
This page will explain the php source code of Littlepoll. It is a very simple script, so this should be easy to follow. I hope it helps you in further customizing the script, and learn some more PHP.
The screenshots here refer to version 1.3, the latest version is 1.4. The differences are very minor, so you should be able to follow just fine.
lp_source.php
All the main code is contained in the lp_source.php file. Your own page calls this file, loads and executes the code. Lp_source.php then calls lp_settings.php and reads the parameters you set when setting-up the poll
This explanation will only go trough the code of lp_source.php
background info
The idea (or voting process) is simple. The user visits the page, and sees the poll as a question and a couple of options. He selects an option and clicks the vote button. The vote is logged, and he sees the results. When revisiting the site, or refreshing, he will still just see the results, and no new vote should be counted.
In order to track in which 'step' of the process the user is, a variable $votingstep is kept in a cookie on the user's computer. It has no value if the user has never visited the site, and has value 1 if the user has visited the site but not voted.
Once he votes, the variable $votingstep gets value 2. The script will read the variable, and store the vote. Then, the value 3 is stored in the cookie on the computer of the user. This indicates the script that it should just show the results.
the code

In this piece of code, the settings are read from the lp_settings.php file, and the variables from the form (POST variables) and the cookie are read.
The if statement checks whether the variable $c_votingstep has a value. The 'c_' means that it comes from the cookie - thus, I'm actually checking here whether a cookie exists. If not: the user has not visited the site before, he is in voting step 1. That is why the $votingstep variable is set to 1 in the next line.
The next part is a function declaration. I needed a function to sum all values in an array. No need to go into detail, but this function will return exactly that.

The next part contains another three function declarations. The first is a general function which simply reads the IP adress of the user, which we can later use for logging. The second function simply writes a supplied text to a log file. This function is called on several occasions, just to keep a log on what users are doing - very useful for finding out if someone is cheating!
The ReadElements function will be called to read information about the current poll. The file contains first the question, and then the options, followed by the number of votes on that option. The elements are separated by a : character. This function opens the file, reads the entire contents, and then splits the contents into an array, splitting by the ":" character. The array is called $elements. The first item in $elements, indicated by $elements[0] will now contain the question, $elements[1] will contain the first option, etc. So the script first reads the question, and then loops trough the $elements array and reads all voting options. They're stored in $items (the options) and $itemvoted (the number of votes).

This part starts by calling the ReadElements function, the file containing the voting scores is read, and the results are stored in $question, $items (the options) and $itemvoted (the number of votes).
The next if statement checks whether the cookie on the users' computer contains the same question as the question of the current poll. If not, it means he has a cookie for a previous poll, therefore, we must reset the poll ($votingstep = 1 - back to step 1 in the voting process).
The next part I am not very proud of. It's a bit silly. It is a function declaration (of the function ShowTheStuff) within an if statement. This means that the function will only be available if we are in step 2 or 3 of the voting process. Because well, we only need to show the results (that's what the function does) in step 2 and 3. Computationally (?) it makes no sense though, it just complicates the code. I won't do it again, I promise.
What the function ShowTheStuff does, is generate a line of html code which will show the poll. How does this work? The script will append piece by piece html code into a variable called $stepstr (Step String, as in the String that contains the HTML code for this step). In the ShowTheStuff function, you'll see a while loop, that will loop trough all the voting options and add (using the . operator to concatenate text in PHP) pieces of HTML. Carefully read those lines. First a piece is added with a voting percentage, then two images are created, with a length depending on the number of votes - these are the little voting bars! And this is repeated for each option in the poll.

Here's where the real processing starts. The first if statement checks that if for some reason, we don't know the voting step yet (this normally shouldn't happen, just to be sure), we'll default to step 1.
The next if statement checks that if we are in step 2 (indicated by the cookie), but we have no information about what is voted (the $p_radios variable contains the value of the radio button that was selected) - we will conclude that the user didn't choose something to vote on; he just clicked the button. Back to step 1.
The third statement is the piece of code for voting step 1 - showing the form with radio buttons, one radio button for each option. It is similar to the ShowTheStuff function, in that it will generate again one of those variables containing a string of HTML code. In this case, the variable is $step1str - Step 1 String. You'll see that first the beginning of the form is added, and then in a loop, per option, an input is added. Finally, the code for a button is added.

In the final part, the processing for step 2 and 3 is defined. Step 2 starts with registering the vote and writing the new score to the file. It then calls ShowTheStuff to... show the stuff.
Step 3 is even shorter, it just calls ShowTheStuff.
finally
Hope this little walktrough was interesting for you. Let me know if you have any further questions, and I'll try to clear them up.