SitePoint Sponsor

User Tag List

Results 1 to 22 of 22
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2003
    Location
    UK
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Database Navigation...

    Say I have a table with a field called 'URL' and a field called 'Category' and I want to make a menu that displays all the different categories and displays the links of that category under them.

    How would I do this without doing "SELECT URL FROM tblMenu WHERE Category='Gaming'" and just having it display all the different categories on its own.

    Thanks in advance!

  2. #2
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    SELECT DISTINCT Category from tblMenu LIMIT 30
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2003
    Location
    UK
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cyborg from dh
    Code:
    SELECT DISTINCT Category from tblMenu LIMIT 30
    That would bring me back the Categories from tblMenu but now I have them I want to display everything from the URL field in tblMenu under the correct category.

  4. #4
    SitePoint Enthusiast
    Join Date
    Feb 2003
    Location
    UK
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Like:

    Gaming
    link1
    link2
    Food
    link3
    link4
    TV
    link5
    link6

  5. #5
    SitePoint Evangelist
    Join Date
    Nov 2001
    Location
    UK
    Posts
    466
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    select * from
    while($row = ......)
    {
    $menu_array[$row['category']][] = $row['url'];
    }

    now play with the array which would look like
    $menu_array['games'][0] = 'hsdjhgkhfg';
    $menu_array['games'][1] = 'asgdtfassd';
    etc etc
    teckis - that's news to me.

  6. #6
    SitePoint Enthusiast
    Join Date
    Feb 2003
    Location
    UK
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pootergeist
    select * from
    while($row = ......)
    {
    $menu_array[$row['category']][] = $row['url'];
    }

    now play with the array which would look like
    $menu_array['games'][0] = 'hsdjhgkhfg';
    $menu_array['games'][1] = 'asgdtfassd';
    etc etc
    I'm very new to this. Not sure what goes in the ...... ($row = ......)

    But I guess that creates an array for each category.
    $menu_array['Games'], $menu_array['TV'] etc... then the [0],[1],[2] etc, is every link under that category.

    So once I have that array, how do I make it loop through displaying the category then showing the links under it.

    Any ideas?

  7. #7
    SitePoint Enthusiast
    Join Date
    Feb 2003
    Location
    UK
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    bump

  8. #8
    SitePoint Enthusiast
    Join Date
    Feb 2003
    Location
    UK
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I guess, we are stumped...

  9. #9
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    foreach($menu_array as $cat => $links) {
       echo 
    "<h1>$cat</h1>";
       foreach (
    $links as $link){
          echo 
    "$link<br />";
       }

    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  10. #10
    SitePoint Enthusiast
    Join Date
    Feb 2003
    Location
    UK
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmmm, I'm sorry.
    I braindead.
    I've been trying and trying different things.
    I forgot that I needed a URL in there somewhere, so the output for a link would be something like:

    <a href="http://www.sitepointforums.com">SPF</a>

    Sorry for asking too much

  11. #11
    SitePoint Guru GamerZ's Avatar
    Join Date
    Nov 2001
    Location
    Singapore
    Posts
    617
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $listmenu_query "SELECT * From tblMenu";
    $listmenu mysql_query($listmenu_query);
    while (
    $listmenu_data mysql_fetch_array($listmenu)) {
    if(
    $temp_cat != $listmenu_data['Category']) {
    echo 
    $listmenu_data['Category'];
    }
    echo 
    $listmenu_data['URL']
    $temp_cat $listmenu_data['Category'];

    Don't really know it works or not.

  12. #12
    SitePoint Enthusiast
    Join Date
    Feb 2003
    Location
    UK
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Works a treat, sorry for the late reply and thanks.

  13. #13
    SitePoint Evangelist
    Join Date
    Nov 2001
    Location
    UK
    Posts
    466
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    plop the database structure here and I'll try to find a few moments to run up some code.

    the while($row = .) was left blankish as I didn't know whether you prefered to iterate a mysql_fetch_array or mysql_fetch_assoc set of calls.
    basically it just puts the returned rows into an array where the index is represented by the category.

    database structure first - then code......
    teckis - that's news to me.

  14. #14
    SitePoint Enthusiast
    Join Date
    Feb 2003
    Location
    UK
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry for the late reply. Havn't checked this post in a while.
    Attached a section of the table from phpMyAdmin.

    Hope this helps.
    Attached Images Attached Images

  15. #15
    SitePoint Enthusiast
    Join Date
    Feb 2003
    Location
    UK
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Doesn't look like it attached....

    http://www.sh*tsite.net/misc/tblMenu.gif

  16. #16
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,629
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Of course the * should be replaced by i
    Ian Anderson
    www.siteguru.co.uk

  17. #17
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No bad domain name though ?

  18. #18
    SitePoint Enthusiast
    Join Date
    Jun 2003
    Location
    Singapore
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $temp 1;
    $query mysql_query('SELECT * From tblMenu ORDER BY Category ASC');
    print 
    '<select>';
    while (
    $row mysql_fetch_array($query)) {
        if(
    $temp != $row['Category']) {
            print 
    '<option>' $row['Category'] . '</option>';
            
    $temp $row['Category'];
        }
        print 
    '<option>' $row['Name'] . '</option>';
    }
    print 
    '</select>'

  19. #19
    SitePoint Enthusiast
    Join Date
    Feb 2003
    Location
    UK
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    And how do I get the links and categories to be in the order I want. Is this asking for too much?

  20. #20
    gingham dress, army boots... silver trophy redux's Avatar
    Join Date
    Apr 2002
    Location
    Salford / Manchester / UK
    Posts
    4,838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dannyscorner
    Doesn't look like it attached....
    it is now...all attachments need to be validated by an advisor first before becoming visible...sorry for the delay
    re·dux (adj.): brought back; returned. used postpositively
    [latin : re-, re- + dux, leader; see duke.]
    WaSP Accessibility Task Force Member
    splintered.co.uk | photographia.co.uk | redux.deviantart.com

  21. #21
    SitePoint Enthusiast
    Join Date
    Jun 2003
    Location
    Singapore
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dannyscorner
    And how do I get the links and categories to be in the order I want. Is this asking for too much?
    just edit the ORDER BY to match your requirements.

  22. #22
    SitePoint Enthusiast
    Join Date
    Feb 2003
    Location
    UK
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm, so would it be sensible to create a field called Order or something which just holds an integer, then arrange the numbers in the order I like?


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
  •