CSV in Flash Lite

Recently I’d been working on an application that required Flash Lite to import data from a CSV file format.

CSV stands for “Comma Separated Value”. It is a standard means of exchanging data, and virtually all spreadsheets and databases can import data presented in this fashion. This is what a CSV spreadsheet will look like when opened in Excel.

For Flash Lite to import data from a CSV file format, you can make use of a middle language – PHP. Written below is a basic code showing interaction between Flash Lite and PHP for reading CSV files.

PHP File

$file = $_POST['link'];
$row = 1;
$handle = fopen($file, "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
$row++;
for ($c=0; $c < $num; $c++) {
echo
"&data$row$c".$data[$c];
}
}
fclose($handle);

The code above is accepting the CSV file URL from the ‘link’ variable sent from Flash, and tossing it into the ‘fopen’ command. It is then parsing the result using ‘fgetcsv’, and echoing the returned data value back into Flash.

Flash File

varSender = new LoadVars();
varReceiver = new LoadVars();
varSender.link = "http://myurl.com/test.csv";
varSender.sendAndLoad(url, varReceiver, "POST");
varReceiver.onLoad = function(){
trace(varReceiver.data10);
};
stop();

The Flash file is sending the ‘link’ variable to the PHP file and receiving the ‘echoed’ values from PHP in the ‘varReceiver’ Object. The received values can then be used within the application.

Flash Lite interaction with CSV can be very useful in reading data for several games and applications. You can use this code and explore the possibilities of adding much more interaction to your content.

About Mariam Dholkawala 188 Articles
Part geeky part quirky, I love writing games, travelling the world, trying new food and learning new languages. I am sometimes the designer, programmer and artist for some of my own games.

6 Comments Posted

  1. I assume, you are doing parsing on server-side to save some cpu cycles on client? Though CSV is not complex format and can be done on client-side as well (provided it doesn’t have thousands of rows and cols) 🙂

    Other thing I noticed, you are using two LoadVars instances. I think, you can get things done with one only..

    Following should just work fine:-

    varSender = new LoadVars();
    varSender.link = “http://myurl.com/test.csv”;
    varSender.sendAndLoad(url, varSender, “POST”);
    varSender.onLoad = function(){
    trace(varSender.data10);
    };
    stop();

  2. Some more modification:-

    varSender = new LoadVars();
    varSender.link = “http://myurl.com/test.csv”;
    varSender.sendAndLoad(url, varSender, “POST”);
    varSender.onLoad = function(){
    trace(this.data10);
    };
    stop();

  3. @abdul – The CSV code is a part of a much extensive script that the client would absolutely not be able to handle, so the server side 🙂

    However would try your suggestion with LoadVars to suit this application.

    @cisnky – I too am comfortable with keeping the objects separate for sending and receiving data..especially with regular updates within the application.

  4. It’s totally ok and legal to use two LoadVars, that’s why it’s method take another instance…

    My response was more in context of performance, on one side you talking about devices and other side you are happy to create two instance of an Object (which would persist in memory as long as it’s alive?)

    To keep things clean (for development purpose), it’s alright to have two different objects.

    Different thoughts and perspectives, we all got different ways of doing things 🙂

  5. Hi Mariam,

    1. Could you please give the detail instruction or maybe tutorial step by step to do the databased-like flash lite. I’ve been searching for this about 10 month and still no one give exact explanation like you are.

    2. I heard flash have the limit for the data transfer, how do u face that?

    3. Can we store the .csv file at a server and how to link them all .swf , .php and .csv ?

Leave a Reply

Your email address will not be published.


*


%d bloggers like this: