Page 1 of 1

Fetching all parts of an array

Posted: Thu Aug 30, 2007 1:50 pm
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?

Posted: Thu Aug 30, 2007 3:58 pm
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.

Posted: Thu Aug 30, 2007 4:36 pm
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
        )

Posted: Thu Aug 30, 2007 5:48 pm
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'));