Menu:

Sponsor

Discover Master of Alchemy, our first iPad/iPhone and iPod touch game!

Follow Me

 

Forum's topics

Latest Files

Archives

Top Rated

Categories

Photo Gallery


Flash/PHP poll with a text file

3.3 Third frame: the chart component

Before continue you should install the Chart Component extension from the macromedia exchange site.

Drag and drop the bar chart component on the stage of your third frame

From the component panel drag 'n drop the BarChart element on your stage in the 3rd frame and give to the movieclip istance name: results.

3.4 Third frame: the script

In the 3rd frame we put this code at first, in order to remove all duplicate movieclips of the second frame:


stop();
// -------------------
// remove radio button
// -------------------
function removeRadio()
{
    var a = 2;
    while(_root['myRadio' + a] != undefined)
    {
        _root['myRadio' + a].removeMovieClip();
        a++
    }
}
removeRadio();

Then setup the script to add the items to the bar component:


// -----------
// build chart 
// -----------
results.removeAll();
a = 1
while (myVar['choice' + a] != undefined)
{
    var value = myVar['votes' + a];
    var label = myVar['choice'+a];
    results.addItem({label:label, value:value});
    results.setChartTitle(myVar.title);
    results.setYAxisTitle('Number of votes');
    results.setXAxisTitle('Team name');
    a++
}
results.sortItemsBy("value","DESC");

First we remove all the data inside the bar component (removeAll), and launch the while loop which reads each label and value from the myVar object (the one we created in the first frame) and passes it to the component in this way:
results.addItem({label:label, value:value});

What the component does is to get the value and create the bar chart in according to the values passed. So it's not necessary for use to calculate for each element it's percentage value, since it is autometically done by the component.

The last step:
Create a new button (also a component button), with goBack istance name and prevFrame as clickHandler.
In the frame actions, below the code you already added insert these lines:


// ----------------------------
// if cookie value is FALSE
// set the back button disabled
// ----------------------------
if(myVar.allow=='false')
{
    goBack.setEnabled(false);
}

Do you remember that in the previous Frame we have defined the myVar.allow variable to false once user has submit his vote? In the frist frame also this variable is returned by PHP is user has already voted. If this var is false then disable the back button.

Note: It's better to use a poll with a database data source also because read and write text files is slower that read and write from databases. But if you cannot use a db...

 

Pay attention: in the PHP code I used inside the eregi_replace to check newlines this string "\r\n" but I was testing my application on a Windows server. If your script will run under UNIX server machine you must replace all the "\r\n" occurences into "\n"

 

4 Files download

download .zip file with files used in this tutorial