Jump to content

[SOLVED] XML creation - foreach ($array as $field => $value)...$field not printing


Recommended Posts

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

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;

 

 

 

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)?

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 ;]

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.