Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 11-25-2009, 05:17 PM   #1
thedark_master
Senior Member
 
Join Date: Feb 2008
Posts: 152
Default read from .csv file

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.
thedark_master is offline   Reply With Quote
Old 11-25-2009, 06:49 PM   #2
Slowburn
[Invent:Design:Create]
 
Join Date: Sep 2002
Location: Toronto, ON
Posts: 2,068
Default

ActionScript Code:
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 }
__________________
o Please do a search before you post
o Please use the [ as][ /as] tag for your code (so we can read it clearly)
Slowburn is offline   Reply With Quote
Old 11-25-2009, 07:09 PM   #3
thedark_master
Senior Member
 
Join Date: Feb 2008
Posts: 152
Default

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.

ActionScript Code:
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 Code:
<?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.','.$mc1scale2.','.$mc1scale3."\n";

    
fwrite($file$mc1content);

;

    
fclose($file);

?>
thedark_master is offline   Reply With Quote
Old 11-25-2009, 07:45 PM   #4
thedark_master
Senior Member
 
Join Date: Feb 2008
Posts: 152
Default

I actually just got how to separate it:

ActionScript Code:
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?
thedark_master is offline   Reply With Quote
Old 11-25-2009, 08:44 PM   #5
Slowburn
[Invent:Design:Create]
 
Join Date: Sep 2002
Location: Toronto, ON
Posts: 2,068
Default

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 ).

ActionScript Code:
// 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 Code:
<?php
    $file
=fopen("file.csv","a+");
    
    
$new_content $_POST['new_content'];
    
$mc1content .= $new_content "\n";

    
fwrite($file$mc1content);

    
fclose($file);
?>
__________________
o Please do a search before you post
o Please use the [ as][ /as] tag for your code (so we can read it clearly)
Slowburn is offline   Reply With Quote
Old 11-25-2009, 08:46 PM   #6
thedark_master
Senior Member
 
Join Date: Feb 2008
Posts: 152
Default

thank you so much for your help!
thedark_master is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 05:21 AM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.