madmenyo Posted September 24, 2010 Share Posted September 24, 2010 Hi, I have a XHTML/PHP page that loads a PHP file each couple of seconds for building up the page. The PHP file should retreive data that keeps changing. What would be the best way to store and retreive the data? Obviously i can do this with a database but that will put a lot of stress on the server as each user keeps polling the database. So i was looking into other methods of doing this. I have tried doing this with a plain text file and CSV files using fwrite to put the initial data into a file with separators. Then retrieving that file and explode it into a multidimensional array again. I am unsuccessful here to assign the key values to the new array. I have also tried many snippets for this but these where also not working properly for me. Maybe XML is the key, i didn't really delve into it yet but it seems i have to get extra programs or parser to actually write and change files? But then again i can't find any working tutorials on how to put multi dimensional strings back and forth between PHP and XML. So what needs exactly to be done. Well i'm creating a game where you can fight each other with 4 units on a turn base. The units have a speed stat that determines when it's there turn to act. Like if (unit 1) has 3 speed and (unit 2) 5 speed they will move like this on the time line (unit 1) -> (unit 2) -> (unit 1) -> (unit 1) -> (unit 2). So the plan is each time the user acts with a unit to increment it's speed by the base value. A script will run each time picking up the next unit in line. The PHP page does a check on which player the unit belongs to and if that matches the players ID it will sent the necessary information. The other player will get something like "not your turn". Tx, Quote Link to comment Share on other sites More sharing options...
Chris92 Posted September 24, 2010 Share Posted September 24, 2010 I'd suggest JSON. Quote Link to comment Share on other sites More sharing options...
madmenyo Posted September 24, 2010 Author Share Posted September 24, 2010 This looks promising, i have looked at http://www.electrictoolbox.com/php-encode-array-json/ which shows an easy way to sent a PHP array to js. However is it possible to to write this to a file and retrieve this again using PHP or do you need Js to retrieve this? And a good place where i can find some info on this would be very helpfull. I will look further into this but i'm still open for other suggestions. Quote Link to comment Share on other sites More sharing options...
Chris92 Posted September 24, 2010 Share Posted September 24, 2010 http://php.net/json_encode http://php.net/json_decode Quote Link to comment Share on other sites More sharing options...
madmenyo Posted September 24, 2010 Author Share Posted September 24, 2010 Didn't look into your resources Chris but i will. -edit-(errr ow actually i did obviously )-/edit- Looks very promising indeed! And very easy too. Though i'm encountering a small problem which probably is easy to fix. i made a temp array that is similar to the array i need to fetch from my DB. The code looks like this now: <?php // Create some test data $dbaray[0]['name'] = "Rage"; $dbaray[0]['health'] = "225"; $dbaray[0]['speed'] = "40"; $dbaray[1]['name'] = "Katosh"; $dbaray[1]['health'] = "150"; $dbaray[1]['speed'] = "50"; $dbaray[2]['name'] = "Snake"; $dbaray[2]['health'] = "275"; $dbaray[2]['speed'] = "60"; $dbaray[3]['name'] = "Dan"; $dbaray[3]['health'] = "350"; $dbaray[3]['speed'] = "45"; $dbaray[4]['name'] = "Wicked"; $dbaray[4]['health'] = "200"; $dbaray[4]['speed'] = "55"; print_r ($dbaray); echo "<br><br>"; $myFile = "testFile.txt"; $fh = fopen($myFile, 'w+') or die("can't open file"); $stringData = "".json_encode($dbaray).""; fwrite($fh, $stringData); $file = file_get_contents('testFile.txt'); print_r (json_decode($file)); The problem is (if it's even a problem as i don't know what it is) that the Decoded JSON returns: Array ( [0] => stdClass Object ( [name] => Rage [health] => 225 [speed] => 40 ) [1] => stdClass Object ( [name] => Katosh [health] => 150 [speed] => 50 ) [2] => stdClass Object ( [name] => Snake [health] => 275 [speed] => 60 ) [3] => stdClass Object ( [name] => Dan [health] => 350 [speed] => 45 ) [4] => stdClass Object ( [name] => Wicked [health] => 200 [speed] => 55 ) ) And the print_r ($dbaray); above returns: Array ( [0] => Array ( [name] => Rage [health] => 225 [speed] => 40 ) [1] => Array ( [name] => Katosh [health] => 150 [speed] => 50 ) [2] => Array ( [name] => Snake [health] => 275 [speed] => 60 ) [3] => Array ( [name] => Dan [health] => 350 [speed] => 45 ) [4] => Array ( [name] => Wicked [health] => 200 [speed] => 55 ) ) Whats the difference between those with stdClass Object? Do they work the same? What should i use to make them the same? Quote Link to comment Share on other sites More sharing options...
madmenyo Posted September 24, 2010 Author Share Posted September 24, 2010 Ow i guess i need this : http://www.phpro.org/examples/Convert-Object-To-Array-With-PHP.html but i'm gonna look into working with objects too as i have no clue -edit- Well this does not seem to work: $obj = (json_decode($file)); $array = (array) $obj; print_r ($array); -edit- But his function does do the job I can't believe i have been fiddling around for hours and hours to get a array into a file and read it back in. Json is a live saver and you are to Chris thanks a lot . Quote Link to comment Share on other sites More sharing options...
madmenyo Posted September 24, 2010 Author Share Posted September 24, 2010 For further reference i'll put in my complete code. The function is from http://www.phpro.org/examples/Convert-Object-To-Array-With-PHP.html <?php // function to put JSON object to an array. function objectToArray( $object ) { if( !is_object( $object ) && !is_array( $object ) ) { return $object; } if( is_object( $object ) ) { $object = get_object_vars( $object ); } return array_map( 'objectToArray', $object ); } // An array for tesing purpose. $dbaray[0]['name'] = "Rage"; $dbaray[0]['health'] = "225"; $dbaray[0]['speed'] = "40"; $dbaray[1]['name'] = "Katosh"; $dbaray[1]['health'] = "150"; $dbaray[1]['speed'] = "50"; $dbaray[2]['name'] = "Snake"; $dbaray[2]['health'] = "275"; $dbaray[2]['speed'] = "60"; $dbaray[3]['name'] = "Dan"; $dbaray[3]['health'] = "350"; $dbaray[3]['speed'] = "45"; $dbaray[4]['name'] = "Wicked"; $dbaray[4]['health'] = "200"; $dbaray[4]['speed'] = "55"; print_r ($dbaray); echo "<br><br>"; $myFile = "testFile.txt"; $fh = fopen($myFile, 'w+') or die("can't open file"); //Put the array into a JSON encoded string wich is sent to file. $stringData = "".json_encode($dbaray).""; fwrite($fh, $stringData); //get the file contents and decode these into an object $file = file_get_contents('testFile.txt'); $obj = (json_decode($file)); //use the function to make an array from the object again. $array = objectToArray( $obj ); print_r($array); ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.