Page 1 of 1

multidimensional array to an xml string ?

Posted: Mon Jul 07, 2003 11:57 pm
by grahama
I am trying to convert a multidimensional array [taken from a mySQL query] and convert it to an XML tree

I am pretty excited as I am almost there...
Everything works....except the conversion back to XML

if anyone has had experience with this kind of thing....could you look at:
http://www.siren.cc/mySql2xml.php.sit

essentially, the query works, and the array has been properly populated
I am just not sure how to convert it back to XML

basically I have a mutlidimensional array called $category that looks something like:


1 | West Coast Show Reel| 10 | taste | Atticus | Ross
2 | West Coast Show Reel | 7 | yamaha | Chris | Desmond
3 | West Coast Show Reel | 5 | color | Graham | Anderson

field names:
reel.category_id
category.name
reel.spot_id
spot.name
composer.fname
composer.lname

reel is a ' join' table containing: reel.id, reel.category_id, and reel.spot_id...it joins the 'category' and 'spot' tables
category is a table of categories
spots is a table of commercials

Somehow, I need to loop thru the Category Array and print the XML list

I was attempting to put together something like:

/// Create XML to return to the Qt movie
/**
This is what is not working :( as I am a bit shaky turning multidimensional arrays into an XML tree
**/


print '<sirenreels>';


foreach($category as $categoryID => $spotArray)
{
print '<category>';
print '<meta>'.$spotArray['id'].'</meta>';

print '<content>';

foreach($spotArray as $spotInfoArray))

{

print '<spot>';
print '<id>'.$songInfoArray['spot_id'].'</id>';
print '<title>'.$songInfoArray['title'].'</title';
print '<fname>'.$songInfoArray['fname'].'</fname>';
print '<lname>'.$songInfoArray['lname'].'</lname>';
print '</spot>';

}

print '</content>';

print '</category>';
}

print '</sirenreels>';


I know I am close........
I am sure it is some part of the key=>Value stuff that is not translating






many thanks :)
g

Posted: Tue Jul 08, 2003 5:49 pm
by trollll
Correct me if have this incorrectly, but the array will have two dimensions? The code will try to get to a third dimension. If you take out the second loop and adjust the code so all of your syntax seems happy it may work that way.

Posted: Tue Jul 08, 2003 6:03 pm
by McGruff
Also $spotInfoArray becomes $songInfoArray - it may just be the way you typed it in the post and isn't like that in your code.

It's maybe slightly clearer what's going on with:

Code: Select all

<?php
foreach($spotArray as $value) {  // extra ) removed

    print '<spot>'; 
    print '<id>' . $value . '</id>';
    print '<title>' . $value . '</title';
    print '<fname>' . $value . '</fname>';
    print '<lname>' . $value . '</lname>';
    print '</spot>';

} 

?>