开发者

loops with fwrite for XML

开发者 https://www.devze.com 2023-03-31 05:35 出处:网络
function xmlParse() { $fh = fopen(\'schools/\' . $this->id . \'/books/school_books.xml\', \'a\'); $xmlstr = \"<?xml version=\'1.0\' ?>\\n\" .
function xmlParse() {

    $fh = fopen('schools/' . $this->id . '/books/school_books.xml', 'a');


    $xmlstr = "<?xml version='1.0' ?>\n" .
            "<rows></rows>";

    // create the SimpleXMLElement object with an empty <book> element
    $xml = new SimpleXMLElement($xmlstr);


    $x = 0;

    // add some more child nodes
    for ($i = 0; $i < sizeof($this->students); $i++) {

        $this->students[$i]->getmyBooks();


        for ($j = 0; $j < sizeof($this->students[$i]->myBooks); $j++) {



            $row = $xml->addChild("row");
            $row->addAttribute("id", $x);
            $row->addChild("cell", $this->students[$i]->myBooks[$j]->owner);
            $row->addChild("cell", $this->students[$i]->myBooks[$j]->title);
            $row->addChild("cell", $this->students[$i]->myBooks[$j]->category);
            $row->addChild("cell", $this->students[$i]->myBooks[$j]->price);
            $row->addChild("cell", $this->students[$i]->myBooks[$j]->description);
            $row->addChild("cell", "Test");
            $x++;

            fwrite($fh, $xml->asXML());

        }



    }


}

I know what开发者_开发问答 the problem is: its fwrite($fh, $xml->asXML());

if I keep calling that within the loop it doesn't append it keeps starting the xml document from scratch and posting the tags again.

My Problem is that it keeps writing from all over again the xml tags... instead of just continuing with the xml. If i do it for just 1 student it works perfect, but instead when I try to loop through all my students it keeps printing the xml tags instead of continuing on to the next student.

<?xml version="1.0"?>
   <rows>
     <row id="0">
       <cell>Owner</cell>
       <cell>test</cell>
       <cell>Math</cell>
       <cell>11</cell>           
       <cell>test</cell>
       <cell>Test</cell>
     </row>
   </rows>

   <?xml version="1.0"?>

continues with the next one then it does xml tags again and again.

This is how its to look like for one student:

<rows>
     <row id="0">
       <cell>Owner</cell>
       <cell>Calculus III</cell>
       <cell>Math</cell>
       <cell>82</cell>
       <cell>This book is in great condition! Available asap.</cell>
       <cell>Test</cell>
     </row>
     <row id="1">
       <cell>Owner</cell>
       <cell>Discrete Mathematics</cell>
       <cell>Math</cell>
       <cell>62</cell>
       <cell>This book is in poor condition.</cell>
       <cell>Test</cell>
     </row>
     <row id="2">
       <cell>Owner</cell>
       <cell>Calculus I</cell>
       <cell>Math</cell>
       <cell>12</cell>
       <cell>Really good book.</cell>
       <cell>Test</cell>
     </row>
    </rows>


You're really looking for file_put_contents($name, $contents). That function adds all of the content en masse, so you would call it once at the end of the loop.

The code might look like:

// after i >= sizeof($this->students)
file_put_contents('schools/' . $this->id . '/books/school_books.xml', 
                  $xml->asXML());

fwrite on the other hand, appends a file every single time it is called. This means that it will add the contents of the XML to the file sizeof($this->students) times, which is what you're seeing right now.

By the way, depending on the size of sizeof($this->students), you may want to declare a local variable to cache that before you look, sizeof it will be called every time.

$studentSize = sizeof($this->students);
for ($i = 0; $i < $studentSize; $i++) {

On the other hand, you might want to change that to a foreach loop (can't describe how at the moment but I'll add that in later if I remember).

0

精彩评论

暂无评论...
验证码 换一张
取 消