SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot DarkMonkey's Avatar
    Join Date
    Apr 2001
    Location
    uk
    Posts
    170
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Catergory type problem.

    Okay, I'll try and explain this as simple as I can.
    I have a tableof articles, the articles have their own categories 'reviews, features, columns etc. etc.' now I have another table of categories and the articles reference those categories with something like 'catref' so a review might have the catref '2' that category is named 'Media Reviews'...

    What I'm looking to do is cycle though all the articles and display them under their respective categories like so.

    Media Reviews.
    Review 1
    Review 2
    Review 3
    Book Reviews
    Review 1
    Review 2

    etc. etc. I'd like it to be formated like that I could probably do it with nested queries but I know that's not wise at all. So if it can be done in a single query, and I'm pretty sure it can, then do tell Ta.

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Why don't you list your tables with one row of data each and the output you want. It would be less typing for you, and your question would be infinitely clearer.

  3. #3
    SitePoint Zealot DarkMonkey's Avatar
    Join Date
    Apr 2001
    Location
    uk
    Posts
    170
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay then.

    Articles (table)
    id | name | content | section
    ----------------------------------
    1 | Turok | Crap game | 3
    2 | Wave Race: Blue Storm | Great | 4

    Sections (table)
    id | name
    ----------------------------------
    3 | N64 Reviews
    4 | Gamecube Previews


    There are lots and lots of sections but from those two tables I want to get an output where the result might be .

    Gamecube Previews
    .. All articles from this section
    N64 Reviews
    .. All articles from this section

    etc. etc. I'm creating a master list, a place to get to any article on the entire site, categorised by section. Best way to do this, most needed. Thanks

  4. #4
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Something that follows this logic. The trick is for each row of your result set, test whether the section name needs to be printed (because it is a different section name to the last record).

    PHP Code:
    // assume that a database connection already exists
    $sql "SELECT Sections.name AS sectionName,
                   Articles.name AS articleName
            FROM Section, Article
            WHERE Articles.section = Sections.id
            ORDER BY sectionName, articleName"
    ;

    $result mysql_query($sql);

    while(
    $row mysql_fetch_array($result) {
       
    // chech whether we need to display a new sectionName
       
    if ( $row["sectionName"] != $currentName ) {
          echo 
    "<b>$sectionName</b><br>";
          
    $currentName $sectionName;
       }
      
    // display the articleName
      
    echo "$articleName <br>";


  5. #5
    SitePoint Zealot DarkMonkey's Avatar
    Join Date
    Apr 2001
    Location
    uk
    Posts
    170
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As usual, this place rules. Thanks a lot Sid

  6. #6
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Thank you freakysid this solved a question I asked in another forum.

    Sean


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
  •