Horje
PHP 5.3.3 - Appending Well Formed XML Data - Wordpress Solution
[Solved] - PHP 5.3.3 - Appending Well Formed XML Data - Wordpress Solution
Try following codes
Hello, I am needing assistance with a PHP Script and I can not figure out how to finish it. I have a script that gets fired off from a Flash Application about every 5 minutes upon completion of a specific event. The PHP script generates 20 random numbers that gets saved to 2 different files. 1 file gets overwritten each time the script is run and the other get appended to the current data. The format of the data file must be XML. I have a working script that generates 1 set of this data but I can not get the proper format on the appending of the data. It becomes malformed and the flash application errors out. The script behavior is very simple. If this data file does not exist, then the file gets created. The game number simply starts at 1 and then increments by 1 each time the script is run. The number nodes are populated by a random sequence that can not have any duplicates. The range of numbers are from 1 to 80 where only 20 numbers get selected. Below is an example of multiple calls to the script. The XML data must be well formed using DOM classes. I believe my actionscript uses DOM standards as well so to stay solid, DOM in PHP should be used. Thanks. I look forward to a resolution for this project! The format that I need is as follows. This would be 3 instances that the script was run.

<?xml version="1.0"?>
<Container>
   <Game>
       <gamenumber>1</gamenumber>
        <number>1</number>
        <number>2</number>
        <number>3</number>
        <number>4</number>
        <number>5</number>
        <number>6</number>
        <number>7</number>
        <number>8</number>
        <number>9</number>
        <number>10</number>
        <number>11</number>
        <number>12</number>
        <number>13</number>
        <number>14</number>
        <number>15</number>
        <number>16</number>
        <number>17</number>
        <number>18</number>
        <number>19</number>
        <number>20</number>
   </Game>   
   <Game>
       <gamenumber>2</gamenumber>
        <number>1</number>
        <number>2</number>
        <number>3</number>
        <number>4</number>
        <number>5</number>
        <number>6</number>
        <number>7</number>
        <number>8</number>
        <number>9</number>
        <number>10</number>
        <number>11</number>
        <number>12</number>
        <number>13</number>
        <number>14</number>
        <number>15</number>
        <number>16</number>
        <number>17</number>
        <number>18</number>
        <number>19</number>
        <number>20</number>
   </Game>
   <Game>
       <gamenumber>3</gamenumber>
        <number>1</number>
        <number>2</number>
        <number>3</number>
        <number>4</number>
        <number>5</number>
        <number>6</number>
        <number>7</number>
        <number>8</number>
        <number>9</number>
        <number>10</number>
        <number>11</number>
        <number>12</number>
        <number>13</number>
        <number>14</number>
        <number>15</number>
        <number>16</number>
        <number>17</number>
        <number>18</number>
        <number>19</number>
        <number>20</number>
   </Game>      
</Container>
Here is my code that generated the first data set.

<?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 
 // save XML as two files
  
 //Appends to $xml_file if it exists
 $handle = fopen($xml_file, 'a+');
        fwrite($handle, $dom->saveXML());
        fclose($handle); 
		
 //Overwrites gameNumbers.xml
 $handle = fopen('gameNumbers.xml', 'w');
        fwrite($handle, $dom->saveXML());
        fclose($handle); 
 
 echo $dom->saveXML();
 ?>


Solution - 1

You have to change the append part to something like this:


 //Appends to $xml_file if it exists

$handle = fopen($xml_file, 'a+');
fseek($handle,-12,SEEK_CUR);
fwrite($handle, $dom->saveXML());
fwrite($handle,"</Container>");
fclose($handle); 

Date: 2022-10-21 05:21:15
Category: Wordpress
More for You