PDA

View Full Version : read from .csv file


thedark_master
11-25-2009, 05:17 PM
So lets say I created a simple game with all of the parameters are stored in variables. At any time of the game the user can press save, and these variables will be sent to .csv file using PHP. Each time the user presses save, it will add a new line in the file. This I know how to do.

Here is what I don't know how to do. How would I make it so that when the game is started, I wanted it to load the last line of variables from the .csv file into the game? So basically the last save needs to be loaded when the game is started.

Slowburn
11-25-2009, 06:49 PM
var savedDataLoader:URLLoader = new URLLoader();
savedDataLoader.addEventListener( Event.COMPLETE, handleSavedDataLoaded, false, 0, true );
savedDataLoader.load( new URLRequest( "file.csv" ) );

function handleSavedDataLoaded( event:Event ):void
{
event.target.removeEventListener( Event.COMPLETE, handleSavedDataLoaded, false );
var savedData:String = String( event.target.data );

// split the file into lines
var lines:Array = savedData.split( "\n" ); // you may need \r in aswell, depending on how the line feeds were created.
// get the last element in the array, this should be our last line....
var lastSaved:String = lines[lines.length-1];
trace( lastSaved ); // should output the last line of the file...if this is an EOF character, then make sure to go back one more line
}

thedark_master
11-25-2009, 07:09 PM
that works perfectly but I need to separate this line. Each cell in the line represents its own variable. How would I do that?

Also I wasn't aware that this can be done without PHP. Here was my code for saving the variables.



var request:URLRequest = new URLRequest( "http://marinpetkov.com/excel.php" );

var variables:URLVariables = new URLVariables();


variables.mc1_category = mc1_category;
variables.mc1_type = mc1_type;
variables.mc1scale1 = mc1scale1;
variables.mc1scale2 = mc1scale2;
variables.mc1scale3 = mc1scale3;


request.data = variables;

request.method = URLRequestMethod.POST;

sendToURL(request);


<?php

$file=fopen("file.csv","a+");




$mc1_category = $_POST['mc1_category'];
$mc1_type = $_POST['mc1_type'];
$mc1scale1 = $_POST['mc1scale1'];
$mc1scale2 = $_POST['mc1scale2'];
$mc1scale3 = $_POST['mc1scale3'];
$mc1content.= $mc1_category.','.$mc1_type.','.$mc1scale1.','.$mc 1scale2.','.$mc1scale3."\n";

fwrite($file, $mc1content);

;

fclose($file);

?>

thedark_master
11-25-2009, 07:45 PM
I actually just got how to separate it:


var myData:Array = lastSaved.split(",");

trace(myData[i]);


Thank you so much for your help.
Btw do you know of a better way of writing the data than what I did without using PHP?

Slowburn
11-25-2009, 08:44 PM
The only thing I could think of is reduce the amount of data your sending to the php.
( please note, I'm not that good at php.....but here is my attempt ).


// here I'm placing the vars in an array for easy joining to a string.
var contentString:Array = [mc1_type, mc1scale1, mc1scale2, mc1scale3];

var request:URLRequest = new URLRequest( "http://marinpetkov.com/excel.php" );
var variables:URLVariables = new URLVariables();
// join the vars to create the string to send to php.
variables.new_content = contentString.join( "," );
request.data = variables;
request.method = URLRequestMethod.POST;
sendToURL(request);


<?php
$file=fopen("file.csv","a+");

$new_content = $_POST['new_content'];
$mc1content .= $new_content . "\n";

fwrite($file, $mc1content);

fclose($file);
?>

thedark_master
11-25-2009, 08:46 PM
thank you so much for your help!