Results 1 to 4 of 4

Thread: Displaying categories

  1. #1
    Web Extraordinaire
    Join Date
    Aug 2004
    Posts
    301
    Thanks
    0
    Thanked
    0 times in 0 posts

    Displaying categories

    Hi,

    I am having a problem with one of my sites when trying to display categories.

    Basically i have a sql table which is set out like this:

    title | category | category2 | description | tags

    There are thousands of entrys in the table and some have the same category and same category2

    What i am trying to achieve is a list of all the categorys then a list of all the category2 under the categorys so basically it is like a directory with your main cat and then the sub cats under.

    What i have done is the following:

    Code:
    <table width="690" border="0" cellspacing="0" cellpadding="0">
    						<?php
    												
    					$selects = "SELECT DISTINCT(category2), category FROM recipes ORDER BY category ASC, category2 ASC";
    					$result = mysql_query($selects) or die ( mysql_error ( ) );
    
    					while ($res = mysql_fetch_array($result)) {
    						$resu[] = $res;	
    					}
    					mysql_free_result($result);
    					
    						$item=0;	
    						$last_category = '';
    foreach ($resu AS $row) {
    
        if($item % 3 == 0)
            print "  <tr>\n";
    
        if ($row['category'] != $last_category) {
    ?>
        <td bgcolor="#E8F3BE">
          <h1><?php echo $row['category']; ?></h1>
          <p>
    <?php
        }
    ?>
          <a href="recipe-details.php?id=<?php echo"".$row['id'].""; ?>">
            <?php echo"<span class='categories'>".$row['category2']."</span>"; ?>
          </a>
    <?php
        if ($row['category'] != $last_category) {
    ?>
          </p>
        </td>
    <?php
            $item++;
        }
    
        if($item % 3 == 0)
            print "  </tr>\n";
        $last_category = $row['category'];
    }
    				?>
                    </table>
    Which from what i can see unless i am missing something should show correctly.

    This is what is happening if you look here: foundrecipes.com

    Any help would be appreciated.

    Cheers,
    Adam

  2. #2
    Web Extraordinaire
    Join Date
    Aug 2004
    Posts
    301
    Thanks
    0
    Thanked
    0 times in 0 posts
    Anyone??

  3. #3
    Large Member
    Join Date
    Apr 2004
    Posts
    3,720
    Thanks
    47
    Thanked
    99 times in 64 posts
    Looks like it's working to me. Can you be more specific?
    To err is human. To really foul things up ... you need a computer.

  4. #4
    www.dougmcdonald.co.uk
    Join Date
    May 2007
    Location
    Bath
    Posts
    523
    Thanks
    5
    Thanked
    20 times in 20 posts
    • DougMcDonald's system
      • Motherboard:
      • Asus P5B Deluxe
      • CPU:
      • Inter Core 2 Duo E6600
      • Memory:
      • 2 x 2GB - Geil Black Dragon PC6400
      • Storage:
      • 2 x 400GB Samsung Spinpoints (Running in Matrix array) 100GB @ RAID0 + 300GB @ RAID1
      • Graphics card(s):
      • BFG nVidia 8800GTS 320MB OC2
      • PSU:
      • Corsair HX520W modular
      • Case:
      • Lian-Li PC7 II Plus
      • Monitor(s):
      • LG 17" Flat Thingy
      • Internet:
      • Crappy BT 1MB Unreliable wank :s
    Admittedly, this is not a direct coding of your solution, but should give some ideas.

    Simplest (not neccessarily most efficient) way to do this is to use nested querys and outputs something along these Pseudo lines:

    $query to select categories;

    while not end of category rowset
    print current category
    $subquery where subquery belongs to category
    whilte not end of sub category row set
    print current subcategory
    increment subcategory row counter
    Increment category row counter

    I have code along these lines:

    PHP Code:
    function dynamicWriteNav($category) {
        
    //SELECT CATEGORIES AND SUB CATEGORIES
        
    $query = ("SELECT DISTINCT c.categoryid as catid,categoryname, sc.categoryid as subcat_catid FROM Category c LEFT JOIN SubCategory sc on c.categoryid = sc.categoryid ORDER BY c.categoryid");
        
    $result mysql_query($query);
        
    $data mysql_fetch_assoc($result);
        
        
    //LOOP THROUGH CATEGORIES
        
    while ($data[catid] != 0) {

            if(
    $data[catid] == $category) {
                print(
    "<li class=\"nav\"><a href=\"index.php?punchline=" $data[catid] . "\" class=\"navselected\">" $data[categoryname] . "</a></li>\n");    
                
    //IF IT'S GOT SUB CATEGORIES, DISPLAY THEM
                
    if($data[subcat_catid] = $data[catid]) {
                    
    $subquery = ("SELECT subcategoryid, subcategoryname FROM SubCategory WHERE categoryid = " $data[catid] . " ORDER BY subcategoryname;");
                    
    $subresult mysql_query($subquery);
                    
    $subdata mysql_fetch_assoc($subresult);

                    print(
    "<ul class=\"littlenav\">\n");
                    while (
    $subdata[subcategoryid] != 0) {
                        print(
    "<li class=\"littlenav\"><a href=\"index.php?punchline=" $data[catid] . "&amp;topic=" $subdata[subcategoryid] . "\" class=\"littlenav\">" $subdata[subcategoryname] . "</a></li>\n");                                    
                        
    $subdata mysql_fetch_assoc($subresult);
                    }
                    print(
    "</ul>\n");
                }
            } 
            else 
            
    //ELSE DISPLAY THE NORMAL NAV ITEM
            
    {
                print(
    "<li class=\"nav\"><a href=\"index.php?punchline=" $data[catid] . "\" class=\"nav\">" $data[categoryname] . "</a></li>\n");
            }
            
    $data mysql_fetch_assoc($result);
        }

    There is a little more complexity than you may need here as i test for selected nav item and display a different class based on this.

    I use it to display the navigation in http://www.1liners.co.uk/test/index.php?punchline=1 and it seems to work fine.

    In my example, the cats only display sub cats if they are selected, hence this line :

    PHP Code:
    if($data[catid] == $category) { 
    You could make it simpler by removing some of this,

    Not the most efficient though, not optimised yet, just testing functionality

    Hope it helps
    Last edited by DougMcDonald; 18-07-2007 at 01:46 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Computer not posting or displaying
    By Re-Invented in forum Help! Quick Relief From Tech Headaches
    Replies: 3
    Last Post: 30-05-2007, 03:37 PM
  2. My monitor is displaying flashing colors.
    By dk26 in forum Help! Quick Relief From Tech Headaches
    Replies: 0
    Last Post: 09-02-2007, 09:42 PM
  3. Displaying a System variable on a Web Page
    By Jiff Lemon in forum Software
    Replies: 3
    Last Post: 08-10-2003, 03:32 PM

Posting Permissions

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