Fetching all parts of an array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
davidtube
Forum Commoner
Posts: 79
Joined: Sun Mar 25, 2007 8:42 pm

Fetching all parts of an array

Post by davidtube »

I have this piece of code, based on this tutorial viewtopic.php?t=60287.

Code: Select all

$musiclist=mysql_query("SELECT subcat FROM subcat WHERE category='Music' ORDER BY subcat");

$sublist=array(1=>array('Anna','Andrew','Andrea','Annakin'),
               2=>array('Bill','Bob','Bernie'),
               3=>array('Charles','Camila','Connie','Constance'),
               'Music'=>mysql_fetch_array($musiclist),
               5=>array('Edmund','Edgar','Elisabeth','Eugene','Emma','Emily'));
I've only changed the "Music" line. I want to list all the results found from the query, but only the second result of the array is being displayed. What am I doing wrong?
Begby
Forum Regular
Posts: 575
Joined: Wed Dec 13, 2006 10:28 am

Post by Begby »

Run this code in its own file outside of ajax and do a print_r($sublist) and then view the source. That might clue you in. If not post back here.
davidtube
Forum Commoner
Posts: 79
Joined: Sun Mar 25, 2007 8:42 pm

Post by davidtube »

Thanks.

I got this source code.

Code: Select all

[3] => Array
        (
            [0] => Charles
            [1] => Camila
            [2] => Connie
            [3] => Constance
        )

    [Music] => Array
        (
            [0] => Childrens Music
            [subcat] => Childrens Music
        )
I still don't understand what I should be doing. There should be a list that looks like this:

Code: Select all

[Music] => Array
        (
            [0] => Childrens Music
            [1] => Dance
[1] => Classical
[2] => Pop
        )
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

You need to iterate mysql_fetch_assoc() if you are expecting more than a single row.

Code: Select all

$music = array();
while ($row = mysql_fetch_assoc($result) {
   $music[] = $row['subcat'];
}

$sublist=array(1=>array('Anna','Andrew','Andrea','Annakin'),
               2=>array('Bill','Bob','Bernie'),
               3=>array('Charles','Camila','Connie','Constance'),
               'Music'=> $music,
               5=>array('Edmund','Edgar','Elisabeth','Eugene','Emma','Emily'));
Post Reply