Dynamic XML node creation with php

I had to create an xml export using php pulling the info from a database. The last section that needs done adds in node pieces that could range anywhere up to 20 additions.

I can extract the data I need from the database with no problem, but I don't know how to create these sub & sub-children "on-the-fly". By "on-the-fly" I mean that there could be two additions, or there could be up to 20.

This is my placeholder code for where this needs to be done:
	// PHOTOS SECTION NEEDS DONE
	$node_photos = $doc->createElement("photos");
	$node->appendChild($node_photos);
	$newnode2 = $doc->createElement("photo");
	$node_photo = $node_photos->appendChild($newnode2);
	$node_photo1 = $doc->createElement("url", 'THIS SECTION NEEDS DONE. NEED TO CYCLE THROUGH AND CREATE NODES ON THE FLY.');
	$node_photo->appendChild($node_photo1);

Open in new window


This is the example result that has to display:
		<photos>
			<photo>
				<url>http://doesmobilehomes.com/photos/24/1.jpg</url>
				<title>front view of the home</title>
			</photo>
			<photo>
				<url>http://doesmobilehomes.com/photos/24/2.jpg</url>
				<title>kitchen</title>
		        </photo>
                </photos>

Open in new window


It may or may not be simple to do this, but I've never used the php code for producing xml up until now, so I'm not sure how to go about this. The photo info (as you can see in the example is what I'm adding in) I can pull that into arrays and iterate through. But I don't understand how to code the add node parts as it iterates.

This is the full display at this point: http://www.manufacturedhomesoregon.net/xml-export/xml-export-test.php
So you can see where the photo info section displays in the output.

Any help is appreciated. I can provide more info if needed.
Thanks, Heather
LVL 14
DzynitAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Marco GasiFreelancerCommented:
When you get the photos from the database, they should be in an array, let's say $photos_array, so you should do something like

      // PHOTOS SECTION NEEDS DONE
      $node_photos = $doc->createElement("photos");
      $node->appendChild($node_photos);
      $newnode2 = $doc->createElement("photo");
      $node_photo = $node_photos->appendChild($newnode2);
        foreach ($photos_array as $photo){
         $node_photo1 = $doc->createElement("url", 'THIS SECTION NEEDS DONE. NEED TO CYCLE THROUGH AND CREATE NODES ON THE FLY.');
        $node_photo->appendChild($node_photo1);
       }

Hope this helps
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DzynitAuthor Commented:
ok, so re-using $node_photo1 over and over will still add them all? I made the assumption that was a variable that had to have a new name each time.
0
Marco GasiFreelancerCommented:
No, there is no need to change the variale name: simply its content will change each time :)
0
DzynitAuthor Commented:
Thank you very much. Again - "learn something new every day"!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
XML

From novice to tech pro — start learning today.