Forum Moderators: coopster

Message Too Old, No Replies

Parsing XML with PHP

More than 1 child element causing problems

         

damianf

1:01 am on Feb 14, 2006 (gmt 0)

10+ Year Member



Hi guys,

I have managed to get PHP to parse an XML document and put the data into variables so that I can then put it into a database by following this tutorial: <removed>

But I come across a problem when my XML file has more than 1 category per member, as you can see here:

<?xml version="1.0" encoding="utf-8"?>
<members>
<member>
<exchange_id>1</exchange_id>
<f_name>Test First 1</f_name>
<l_name>Test Last 1</l_name>
<job_title>Mr</job_title>
<email>test@test.com</email>
<categories>
<category>Test Category 1</category>
<category>Test Category 2</category>
<category>Test Category 3</category>
</categories>
</member>
<member>
<exchange_id>2</exchange_id>
<f_name>Test First 2</f_name>
<l_name>Test Last 2</l_name>
<job_title>Mr</job_title>
<email>test2@test2.com</email>
<categories>
<category>Test Category 1</category>
</categories>
</member>
</members>

etc etc...

Now, when there is only 1 category in the XML data, the script works perfectly. However, when there is more than 1 category in my XML data, it doesn't work. What I want to do is put all 3 categories (or however many I have) into one variable, separated by commas, so the $category variable for the first piece of data might be "Test Category 1, Test Category 2, Test Category 3"

Hopefully this makes sense to you guys, any help would be greatly appreciated!

If you need me to post my code I can, and I hope using the URL above is OK to explain my situation.

Cheers,
Damian

[edited by: jatar_k at 4:44 pm (utc) on Feb. 14, 2006]
[edit reason] no urls thanks [/edit]

coopster

4:28 pm on Feb 15, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, damianf.

So what is happening currently? Are you only getting the first category? Is it skipping the others?

damianf

12:28 am on Feb 16, 2006 (gmt 0)

10+ Year Member



Hi coopster,

Thanks for replying.

The above XML code would yield this result:

Contact number 1 updated.
Categories for contact number 1 updated to Test Category 1.
Contact number updated.
Categories for contact number updated to Test Category 2.
Contact number updated.
Categories for contact number updated to Test Category 3.
Contact number 2 updated.
Categories for contact number 2 updated to Test Category 1.

So yes, only the first category gets updated, and as you can see it tries to keep updating things until all the categories have been looped through. Would it be easier for me to post code code for you?

coopster

5:08 pm on Feb 16, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I think you need to modify the loop to watch for a change in contacts and do a single insertion at that point rather than insert on every iteration of the loop when the category changes. Inside that loop, of course, you could keep appending the categories to any existing categories by adding a comma, a space, and then the next category description.

On a side note, have you considered making a 'contact-to-categories' table and perhaps inserting rows into that independently of this table? That way you could have a one-to-many relationship for contacts to the multiple categories as opposed to a comma-separated list of categories in a single column in this table. Just some thoughts.