SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2009
    Location
    Porto,Portugal
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Dynamicly show links by category

    Hi.
    I have a db table with links containing the name,url and a category.
    I want to dynamicly display all the links in the table in a structure like this:

    Category1

    Link1
    Link2
    Link3

    Category2

    Link4
    Link5
    Link6

    How can i do that?

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php
    #Capture
    $sSQL "SELECT name, url, category FROM table";
    $rResult mysql_query($sSQL);
    $aFinal = array();
    while(
    $aRow mysql_fetch_assoc($rResult))
    {
        
    array_push(
            
    $aFinal[$aRow['category']], array(
                
    'name'    =>    $aRow['name'],
                
    'url'    =>    $aRow['url']
            )
        );
    }

    /*
    $aFinal = array(
        'Books'    => array(
            array(
                'name'    =>    'Book One',
                'url'    =>    'http://bookOne.com'
            ),
            array(
                'name'    =>    'Book Two',
                'url'    =>    'http://bookTwo.com'
            )
        ),
        'Cakes'    => array(
            array(
                'name'    =>    'Cake One',
                'url'    =>    'http://cakeOne.com'
            ),
            array(
                'name'    =>    'Cake Two',
                'url'    =>    'http://cakeTwo.com'
            )
        ),
        'Teas'    =>    array()
    );
    */

    #Display
    foreach($aFinal as $sCategoryName => $aCategoryItems)
    {
        
    printf(
            
    '<h4>%s</h4>',
            
    $sCategoryName
        
    );
        if(
    count($aCategoryItems) > 0)
        {
            foreach(
    $aCategoryItems as $aCategoryItem)
            {
                
    printf(
                    
    '<a href="%s"><p>%s</p></a>',
                    
    $aCategoryItem['url'],
                    
    $aCategoryItem['name']
                );
            }
        }
        else
        {
            echo 
    '<p>No Items</p>';
        }
    }
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,341
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    anthony, you're going to want to use an ORDER BY clause in your query

    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


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
  •