Realitätsverlust
Realitätsverlust

Reputation: 3953

Foreach loop only runs once

got a problem with a foreach loop. The problem is the following:

foreach($content as $c) {
    $root->appendChild($node = $dom->createElement($table));
    $node->setAttribute("id", $content['id']);

    foreach($c as $key => $value) {
        $node->appendChild($dom->createElement($key, $value));
    }
}

$content contains all the data sets from a table. So $c should contain only one. And thats exactly what it does:

Array
(
    [0] => 11
    [id] => 11
    [1] => admin
    [username] => admin
    [2] => $2a$08$tpTWqFtZq1KzyIfD/UYI2u5iyGQw.m0.TGJSWx5gwsb/RaJAmdULK
    [password] => $2a$08$tpTWqFtZq1KzyIfD/UYI2u5iyGQw.m0.TGJSWx5gwsb/RaJAmdULK
    [3] => 
    [group] => 
    [4] => 0
    [deleted] => 0
)

So in the 2nd foreach loop, it should get every single element together with its key and attack it to the node, but its only running once. If i dump $key and $value inside the foreach loop, i only get 2 values, 0 and 11. Thats correct, but it should run 8 times, not only once. Can anybody tell me where my error is?

Edit/ Content of $content:

array(2) {
  [0]=>
  array(10) {
    [0]=>
    string(2) "11"
    ["id"]=>
    string(2) "11"
    [1]=>
    string(5) "admin"
    ["username"]=>
    string(5) "admin"
    [2]=>
    string(60) "$2a$08$tpTWqFtZq1KzyIfD/UYI2u5iyGQw.m0.TGJSWx5gwsb/RaJAmdULK"
    ["password"]=>
    string(60) "$2a$08$tpTWqFtZq1KzyIfD/UYI2u5iyGQw.m0.TGJSWx5gwsb/RaJAmdULK"
    [3]=>
    string(0) ""
    ["group"]=>
    string(0) ""
    [4]=>
    string(1) "0"
    ["deleted"]=>
    string(1) "0"
  }
  [1]=>
  array(10) {
    [0]=>
    string(2) "25"
    ["id"]=>
    string(2) "25"
    [1]=>
    string(6) "mlange"
    ["username"]=>
    string(6) "mlange"
    [2]=>
    string(60) "$2a$08$X/fDcVsbrIE3sAHjU44aNOxQPe2Gg2wRDdd/YeRFT54rmdNucbJ5e"
    ["password"]=>
    string(60) "$2a$08$X/fDcVsbrIE3sAHjU44aNOxQPe2Gg2wRDdd/YeRFT54rmdNucbJ5e"
    [3]=>
    string(0) ""
    ["group"]=>
    string(0) ""
    [4]=>
    string(1) "0"
    ["deleted"]=>
    string(1) "0"
  }
}

Upvotes: 2

Views: 1895

Answers (3)

Renier
Renier

Reputation: 1535

try this:

     foreach($content as $key => $value) {
          $root->appendChild($node = $dom->createElement($table));
          $node->setAttribute("id", $key['id']);
          $node->appendChild($dom->createElement($key, $value)); 

        }

this should do what you want. if I understand correctly

Upvotes: 1

Realitätsverlust
Realitätsverlust

Reputation: 3953

Nevermind, im just stupid, i found the problem: the first index is "0". XML Names cant be numbers, so the whole script just exits after the "appendChild" in the second foreach loop. I forgot to turn on errorreporting and thought its not an error but a syntaxerror ... well, im just retarded. Heres the error:

generate_xml.php:0 Fatal error: Uncaught exception 'DOMException' with message 'Invalid
Character Error' in /srv/www/htdocs/ksoldner/Projekt_Vertragsverwaltung/generate_xml.php
on line 23 DOMException: Invalid Character Error in generate_xml.php on line 23 Call 
Stack: 0.0003 640480 1. {main}() /srv/www/htdocs/ksoldner/Projekt_Vertragsverwaltung
/generate_xml.php:0 0.0066 1265680 2. DOMDocument->createElement() generate_xml.php:23 

Maybe this helps someone.^^

Upvotes: 0

Roopendra
Roopendra

Reputation: 7762

Change

$node->setAttribute("id", $content['id']);

to

$node->setAttribute("id", $c['id']);

What is $key and $value in $node->appendChild($dom->createElement($key, $value)); as per your $content array

Upvotes: 1

Related Questions