1. Introduction
1.1 What we will do in this tutorial
The goal of this tutorial is to show how to build a simple and fast poll in Flash without using a database to store your data, but a simple text file. You can see the text file here, and the final flash poll here.
2. Start Up
2.1 Create the text file (text_poll.txt)
At first we have to create the text file which will contain our poll informations such as the poll question and the answers. Open your preferrend text editor (ie. notepad) and write this:
title=Which team will win the World Championship in Corea/Japan ?
choice1=Brazil
choice2=France
choice3=England
choice4=Italy
choice5=Argentina
choice6=Spain
choice7=Portugal
choice8=Denmark
choice9=Germany
votes1=4
votes2=1
votes3=4
votes4=2
votes5=1
votes6=1
votes7=0
votes8=1
votes9=1
As you can see I've create 3 set of variables:
- title: the poll question
- choice1, choice2, choicen: The possible answers.
- votes1, votes2, votesn: The value for each answer.
Now, once you have saved this document as text_poll.txt you have to upload it to your remote server and set the CHMOD permissions to 777.
2.2 The PHP code options
The first thing to do in the PHP code is to add the header variable to prevend browser cache, so open you PHP editor and create a new document (text_poll.php) and add this code:
<?
// ---------------------
// prevent browser cache
// ---------------------
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
?>
Now we must define the variable options for the poll script. Then in the same tex_poll.php file add these lines:
<?
// ---------------
// define options
// ---------------
$file_data = 'text_poll.txt'; // text file name (CHMOD 777);
$cookie_duration = 3600; // how long cookie remains (3600 = 1 hour)
$cookie_title='text_poll_1';
?>
where $file_data is the variable which point to the text files we have created first, $cookie_duration is the life time defined for the cookie and $cookie_title is the name of the cookie (this is needed is you decide to change the poll, then you must change also the cookie_title value)
2.3 PHP functions and the switch command.
The rest of the php code inside our text_poll.php is based on two functions. One reads data from the text file and send the output to flash, the other one saves data into the text files according to the variables sent by flash.
The switch command will activate one of those function based on the value of another variables ($action).
This is the struture of the next step (this is not the final code for the functions, which we will analyze later)
<?
function readData()
{
...
// this is the function
// which reads the txt file
// and send the output to flash
}
function saveData($num)
{
...
// this function saves
// data to the txt file
}
// -----------------
// Decisional switch
// -----------------
switch($_GET['action'])
{
case 'saveData':
saveData($_GET['num']);
break;
default:
readData();
break;
}
?>
Each function is called by the code inside the switch, which controls the value of a variable ($_GET['action']) and if this is equal to 'saveData' (case 'saveData') it calls the saveData function and then break the script. The default value for the switch command is instead readData().
