2

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"
  }
}
3
  • Could you var_dump your $content variable? Commented Nov 15, 2013 at 8:00
  • sure, added it into the startpost Commented Nov 15, 2013 at 8:02
  • Do you use somewhere in your code $c as reference? (&$c) Commented Nov 15, 2013 at 8:05

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

thx, that was an error too, but it didnt fix my problem with the foreach loop
1

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

1 Comment

Nope, that wasnt the problem, i already solved it myself, was a problem with XML, i tried to add a number as child what doesnt work.^^ Still thanks.
0

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.^^

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.