nogginj Posted January 28, 2009 Share Posted January 28, 2009 howdy i have a loop that iterates over an array pulled from a mysql query. the idea is to create an element for each $field name, and put a text node containing the data into each $field element. However, when I run the following loop, my browser basically freezes at 'transferring data from....'. It just stays there. What I dont understand is that if i simply concatenate each element (whose name i would like to pull from $field) with another string BEFORE $field (ie. 'e'.$field), it works. Example 1 - this does NOT work: foreach ($array as $field => $value){ // create child element $item = $dom->createElement($field); $root->appendChild($item); // create text node $text = $dom->createTextNode($value); $item->appendChild($text); } Example 2 - this DOES work: foreach ($array as $field => $value){ // create child element $item = $dom->createElement('e'.$field); //doesnt matter what character i replace 'e' with $root->appendChild($item); // create text node $text = $dom->createTextNode($value); $item->appendChild($text); } This is the first time i've done this, and I am confused why this is happening. Is it somehow not a string at first but then being converted to a string? thank yall Quote Link to comment https://forums.phpfreaks.com/topic/142832-solved-xml-creation-foreach-array-as-field-valuefield-not-printing/ Share on other sites More sharing options...
.josh Posted January 28, 2009 Share Posted January 28, 2009 not really versed in XML but if concating a string to it makes it work, then why not just concat an empty string to it, like ('' . $field) or maybe cast $field as a string before using it $field = (string) $field; Quote Link to comment https://forums.phpfreaks.com/topic/142832-solved-xml-creation-foreach-array-as-field-valuefield-not-printing/#findComment-748714 Share on other sites More sharing options...
nogginj Posted January 28, 2009 Author Share Posted January 28, 2009 Thanks for the reply...unfortunately neither of those seem to work. -j Quote Link to comment https://forums.phpfreaks.com/topic/142832-solved-xml-creation-foreach-array-as-field-valuefield-not-printing/#findComment-748717 Share on other sites More sharing options...
.josh Posted January 28, 2009 Share Posted January 28, 2009 again, not really versed in xml, so maybe i'm talking out my ass, but are you by some chance using an existing xml list or whatever, and it can't create it because the element already exists, and adding something like 'e' to the front of it makes a new, unmade field (and therefore allows it to be created)? Quote Link to comment https://forums.phpfreaks.com/topic/142832-solved-xml-creation-foreach-array-as-field-valuefield-not-printing/#findComment-748718 Share on other sites More sharing options...
nogginj Posted January 28, 2009 Author Share Posted January 28, 2009 unfortunately, no (at least i dont think so). the code is very basic...all i do on this page is pull the data and then try to output xml. the other tricky thing is that concatenating AFTER $field does not work... 'e'.$field works, $field.'e' does not. again, not really versed in xml you and me both ;] Quote Link to comment https://forums.phpfreaks.com/topic/142832-solved-xml-creation-foreach-array-as-field-valuefield-not-printing/#findComment-748723 Share on other sites More sharing options...
nogginj Posted January 28, 2009 Author Share Posted January 28, 2009 well i found it... the first element in my array was keyed as SURPRISE! "0" (well, with [] around it). as such, $dom->createElement(0); (when $field = 0) basically kills the script. once i filtered the data to not include this (what i would think is very common) key, it works fine. -j Quote Link to comment https://forums.phpfreaks.com/topic/142832-solved-xml-creation-foreach-array-as-field-valuefield-not-printing/#findComment-748814 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.