Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > PHP Databases
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
PHP Databases Using PHP in conjunction with databases. PHP questions not specific to databases should be directed to one of the other PHP forums.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP Databases section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old June 12th, 2004, 03:15 PM
Registered User
 
Join Date: Jun 2004
Location: Noho, CA, USA.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default multidimensional array help for a newbie

Hi :)

I am creating an XML file out of a mysql query with nested arrays.
Currently I can get 1 element and 1 child with a properly formatted XML file with the below script .

My question: is, How do I add 3 to 4 more child elements to the below 'playlist' array ?
Currently ,I have one parent 'Artist' and one child 'english' ...
I need to add more child elements tot he 'Artist' array like urlPath, spanish, biography, etc

Do I have to add another dimension to the 'playlist' array? Do I need another foreach loop ?

I am a bit new to this so any help would be greatly appretiated ....

<?php
@ $db = mysql_connect('127.0.0.1','name','pass');

    if (!$db)
    {
    echo 'Error:Could Not Connect';
    exit;
    }

    mysql_select_db('univision');


$sql = 'SELECT artist.artist_name, media.english, media.path ';
$sql .= 'FROM media, artist ';
$sql .= 'WHERE artist.artist_id = media.artist_id LIMIT 0, 30 ';

$result = mysql_query($sql);
while ($row = mysql_fetch_array($result))
{
$playlist[$row['artist_name']] [] = $row['english'];
//would like to add more children here...
}

$xml = "<sirenreels>\n";

foreach ($playlist as $artist => $media)
{
$num_media = count($media);
       $xml .= "<artist>\n";

       $xml .= "\t<meta>\n";
       $xml .= "\t\t<title>".$artist."</title>\n";
    $xml .= "\t</meta>\n";

    $xml .= "\t<content>\n";

foreach ($media as $mediaVal)
{
            $xml .= "\t\t<media>\n";
            $xml .= "\t\t\t<english_name>".$mediaVal."</english_name>\n";
            ///add more children
            ///add more children
            $xml .= "\t\t</media>\n";

}

    $xml .= "\t</content>\n";
    $xml .= "</artist>\n";
}
$xml .= "</sirenreels>\n";
print $xml

Reply With Quote
  #2 (permalink)  
Old May 13th, 2008, 09:41 AM
Friend of Wrox
Points: 1,515, Level: 15
Points: 1,515, Level: 15 Points: 1,515, Level: 15 Points: 1,515, Level: 15
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2003
Location: Oxford, , United Kingdom.
Posts: 488
Thanks: 0
Thanked 3 Times in 3 Posts
Default

I'd use your primary key field (probably called id) to index the array. And use mysql_fetch_assoc, cos we want an array of associative arrays. Like this:
Code:
while ($row = mysql_fetch_assoc($result))
{
$id = $row['id']; // want a globally unique id, assuming primary key is called id
$playlist[$id] = $row; // row is an associative array, so we can now say $playlist[$id]['artist_name'] or whatever
// etc...
}
And (didn't recreate your XML, but to give you the idea):
Code:
foreach($playlist AS $id=>$entry) {
   echo "Record ID: $id\n";
   echo "Artist: " . $entry['artist_name'] . "\n";
   echo "Track: " . $entry['track_name'] . "\n--\n";
}
HTH
Charlie

--
Charlie Harvey's website - linux, perl, java, anarchism and punk rock: http://charlieharvey.org.uk
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
reading data from multidimensional array shish1 Beginning PHP 4 April 1st, 2007 06:42 PM
Multidimensional Array macsham Beginning PHP 2 February 24th, 2005 03:30 PM
Multidimensional array in ch9 willburke BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 0 November 2nd, 2004 04:44 PM
Efficient implementation of multidimensional array Utwig C# 2 October 26th, 2004 04:34 AM
Multidimensional Array help grahama Beginning PHP 0 June 12th, 2004 09:01 PM



All times are GMT -4. The time now is 04:33 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.