azpaul
08-09-2010, 03:42 PM
Hello,
I am not very proficient with PHP although I I have been reading a ton of tutorials and forum posts over the last few days and working through getting the results I need. I have written a PHP script that creates an XML data file of 20 random numbers and a game number. I am currently able to use this XML file in my flash application.
This script gets fired with in flash when an even ends. I am not sure how to go about adding another complete set of "Game", "gamemumber" and "number" nodes and text with in the root node. I am thinking that it would be best to read the XML file each time, append the nodes accordingly and then rewrite the XML file. Now, how big is too big of a file in order to do this? Eventually, there will be issues with the file size, right? The script gets fired about every 5 minutes or so.
So, if allGames.xml does not exist, it gets created and starts the game number at 1 and dynamically adds the 20 numbers and nodes through an array. Then the script saves it to allGames.xml. When the script gets fired again, the file is there and the gamenumber increments to 2 and another <Game> data set is appended after </Game> but before </Container>.
I have no idea as to where to start looking for this type of code or the best way to go about doing this.
Any ideas or suggestions would be greatly appreciated.
<?xml version="1.0"?>
<Container>
<Game>
<gamenumber>1</gamenumber>
<number>49</number>
<number>31</number>
<number>79</number>
<number>55</number>
<number>19</number>
<number>5</number>
<number>20</number>
<number>9</number>
<number>25</number>
<number>1</number>
<number>53</number>
<number>66</number>
<number>2</number>
<number>6</number>
<number>42</number>
<number>57</number>
<number>15</number>
<number>12</number>
<number>13</number>
<number>54</number>
</Game>
</Container>
<?php
//Create the random numbers
$balls = range(1,80);
shuffle($balls);
$pick = array_slice($balls,1,20);
//$drawn = implode(", ",$pick);
$arraySize = sizeof($pick);
$xml_file= 'allGames.xml';
$drawn = array();
//Creates XML string and XML document using the DOM
$dom = new DomDocument('1.0');
//add root - <Container>
$container = $dom->appendChild($dom->createElement('Container'));
//add <Game> element to <Container>
$game = $container->appendChild($dom->createElement('Game'));
for ($i=0; $i!= $arraySize; $i++){
$drawn [] = array(
'number' => $pick[$i],
);
}
//add <gamenumber> element to <Game>
$gamenumber = $game->appendChild($dom->createElement('gamenumber'));
//add <gamenumner> text node element to <gamenumber>
$gamenumber->appendChild(
$dom->createTextNode($gNumber+1));
foreach( $drawn as $draw ) //Populates the number element with the random array
{
//add <number> element to <Game>
$number = $game->appendChild($dom->createElement('number'));
//add <number> text node element to <number>
$number->appendChild(
$dom->createTextNode($draw['number']));
}
//Generate the xml data files
$dom->formatOutput = true; // set the formatOutput attribute of
// domDocument to true
//Appends to $xml_file if it exists
$handle = fopen($xml_file, 'a+');
fwrite($handle, $dom->saveXML());
fclose($handle);
echo $dom->saveXML();
?>
I am not very proficient with PHP although I I have been reading a ton of tutorials and forum posts over the last few days and working through getting the results I need. I have written a PHP script that creates an XML data file of 20 random numbers and a game number. I am currently able to use this XML file in my flash application.
This script gets fired with in flash when an even ends. I am not sure how to go about adding another complete set of "Game", "gamemumber" and "number" nodes and text with in the root node. I am thinking that it would be best to read the XML file each time, append the nodes accordingly and then rewrite the XML file. Now, how big is too big of a file in order to do this? Eventually, there will be issues with the file size, right? The script gets fired about every 5 minutes or so.
So, if allGames.xml does not exist, it gets created and starts the game number at 1 and dynamically adds the 20 numbers and nodes through an array. Then the script saves it to allGames.xml. When the script gets fired again, the file is there and the gamenumber increments to 2 and another <Game> data set is appended after </Game> but before </Container>.
I have no idea as to where to start looking for this type of code or the best way to go about doing this.
Any ideas or suggestions would be greatly appreciated.
<?xml version="1.0"?>
<Container>
<Game>
<gamenumber>1</gamenumber>
<number>49</number>
<number>31</number>
<number>79</number>
<number>55</number>
<number>19</number>
<number>5</number>
<number>20</number>
<number>9</number>
<number>25</number>
<number>1</number>
<number>53</number>
<number>66</number>
<number>2</number>
<number>6</number>
<number>42</number>
<number>57</number>
<number>15</number>
<number>12</number>
<number>13</number>
<number>54</number>
</Game>
</Container>
<?php
//Create the random numbers
$balls = range(1,80);
shuffle($balls);
$pick = array_slice($balls,1,20);
//$drawn = implode(", ",$pick);
$arraySize = sizeof($pick);
$xml_file= 'allGames.xml';
$drawn = array();
//Creates XML string and XML document using the DOM
$dom = new DomDocument('1.0');
//add root - <Container>
$container = $dom->appendChild($dom->createElement('Container'));
//add <Game> element to <Container>
$game = $container->appendChild($dom->createElement('Game'));
for ($i=0; $i!= $arraySize; $i++){
$drawn [] = array(
'number' => $pick[$i],
);
}
//add <gamenumber> element to <Game>
$gamenumber = $game->appendChild($dom->createElement('gamenumber'));
//add <gamenumner> text node element to <gamenumber>
$gamenumber->appendChild(
$dom->createTextNode($gNumber+1));
foreach( $drawn as $draw ) //Populates the number element with the random array
{
//add <number> element to <Game>
$number = $game->appendChild($dom->createElement('number'));
//add <number> text node element to <number>
$number->appendChild(
$dom->createTextNode($draw['number']));
}
//Generate the xml data files
$dom->formatOutput = true; // set the formatOutput attribute of
// domDocument to true
//Appends to $xml_file if it exists
$handle = fopen($xml_file, 'a+');
fwrite($handle, $dom->saveXML());
fclose($handle);
echo $dom->saveXML();
?>