Jump to content

Two Variables, one NODE!


Jerred121

Recommended Posts

I'm trying to add/update an xml like so:

$xml->Header->Record->$curblk->$row = $value;

where $curblk and $row are obviously nodes.

Problem is that sometimes I need to add/update nodes of the same name so I tried this:

$xml->Header->Record->$curblk->$row[$multi] = $value;

where $multi would represent the number (or instance or key?) of that node I wish to add/update.  Issue with that is, php thinks that I'm tring to set the node as an array, which is not possible!

 

So how do I dynamically add/update $row[0] , $row[1] , $row[2] base on the two variable, $row and $multi? ie:

$row = Jim Bob;
$multi = 0;

<author>Jim Bob</author>

$row = Tom Reed;
$multi = 1;

<author>Tom Reed</author>

You get:

<book>
  <author>Jim Bob</author>
  <author>Tom Reed</author>
</book>

Of course there is muuuuch more to it than that, but that is about as simple as I can explain it...  Of course thank you for any help, i've been stuck on this for 3 days now! and my boss is not happy, especially since I'm a vary novice developer that took on this assignment...

Link to comment
Share on other sites

So basically what I'm asking is - how do I get the effect of something like:

 

$xml->Header->Record->Node[2] = $value;

 

but using dynamic variables to provide the node's name ($row="Node") and the node's key number ($multi=2)?  Is this even possible? Or will I have to use another method of writing xmls?

Link to comment
Share on other sites

What about if you tried to create a string representation - like:

 

$customNode = $row.'['.$multi.']';

 

Then use $customNode to refer to it instead.

 

EDIT:

 

To be honest, I'm shooting in the dark here. I've not used simplexml much. Only other suggestion is bump the thread couple more times if the above doesn't work, and also ask on stackoverflow.com. If all else fails, use a different class or make a small extension to simplexml to manage this add/update style.

Link to comment
Share on other sites

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.