Jump to content

Writing data to an xml file, almost there


azpaul

Recommended Posts

Hi,

 

I am not that versed in PHP.  I have been working with this snippet of code that I found on a tutorial site.  It works like 97% and I get the format  mostly like I need it. 

 

Except, I can not figure how to add an extra single node to the list.

 

The code generates 20 random numbers and puts them into an array.  The array is then used to create a numbers node in the xml file.  It goes through and creates all the nodes.  I can not seem to figure out how to put a single game numbernode at the top of all the numbers nodes.

 

I am also not  understanding how to write the output to create the actual xml file.  I have written code in the past  to write a string to a text file but I am not sure about this because it is in an array?  The xml file will be appended every 5 minutes for data comparison down the road.    I think I will need to add a routine that calls the data back in at the beginning of the script and looks for the last game number if it is there and add 1.  The game number must increment each time it is appended. 

 

Any ideas on how to finish this up?

 

The final out put will look like this.  I only did a few lines instead of all 20 numbers to keep this short.

 

<?xml version="1.0">
<Container>
     <gamenumber>10</gamenumber>
     <numbers>47</numbers>
     <numbers>10</numbers>
     <numbers>5</numbers>
     <numbers>27</numbers>
</Container>

 

My Script

  <?php
  $balls = range(1,80);
  shuffle($balls);
  $pick = array_slice($balls,1,20);
  //$drawn = implode(", ",$pick);
  $arraySize = sizeof($pick);
// print ("Arraw is $drawn\n\n");
  
  $doc = new DOMDocument();
  $doc->formatOutput = true;
  
  $drawn = array();
  $r = $doc->createElement( "Container" );
  $doc->appendChild( $r );
  
   for ($i=0; $i!= $arraySize; $i++){
  $drawn [] = array(
  'numbers' => $pick[$i],
  );
  }  
  
  foreach( $drawn as $draw )
  {    
  $numbers = $doc->createElement( "numbers" );
  $numbers->appendChild(
  $doc->createTextNode( $draw['numbers'] )
  );
  $r->appendChild( $numbers);
  
  }
    
  echo $doc->saveXML();
  ?>

 

 

Thanks for  your advise and help!!!

 

 

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.