SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast jpschwartz's Avatar
    Join Date
    Jul 2003
    Location
    Topeka
    Posts
    77
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Displaying a list within a list

    I'm using a While statement to display a list. Now I want the list to contain a secondary list for each primary list entry. For instance:

    John
    -apples
    -peaches
    -pears

    Michael
    -bananas
    -oranges
    -apples

    Molly
    -grapes
    -oranges
    -pears

    Do I just embed another While statement in the first one? If so, how do the arguments for mysql_fetch_array differ from the primary While statement? Can you give me a model or example?

    Thanks!

    Jim

  2. #2
    SitePoint Zealot
    Join Date
    Feb 2003
    Location
    AZ
    Posts
    197
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'd do a foreach, then just format it with <ul>'s and <li>'s

    PHP Code:
     foreach($list as $name)
     {
          echo 
    $name;
          foreach(
    $name as $fruit)
          {
               echo 
    $fruit;
          }
     } 

  3. #3
    SitePoint Enthusiast jpschwartz's Avatar
    Join Date
    Jul 2003
    Location
    Topeka
    Posts
    77
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, I'll try it!

  4. #4
    SitePoint Zealot
    Join Date
    Mar 2004
    Location
    Milano
    Posts
    127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by the e
    PHP Code:
     foreach($list as $name)
     {
          echo 
    $name;
          foreach(
    $name as $fruit)
          {
               echo 
    $fruit;
          }
     } 
    This can't work. If $name is an array you can't echo it, if it's an array you can't instead use a foreach on it.

    Try this:
    PHP Code:
    foreach($list as $name => $fruitList){
        echo 
    $name;
        foreach(
    $fruitList as $fruit){
            echo 
    $fruit;
        }

    The whole array has to be in this format:
    PHP Code:
    $list = array('John' => array('apples''peaches''pears'), 'Michael' => array('bananas''oranges''apples'), 'Molly' => array('grapes''oranges''pears')); 

  5. #5
    SitePoint Zealot
    Join Date
    Feb 2003
    Location
    AZ
    Posts
    197
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Lol, ya, your right, sorry about that


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •