Jump to content

Displaying multiple instances of a field under separate distinct field


neo926

Recommended Posts

What a fun topic name that is. Okay, this is the last big challenge for me and my site. As I've said in my earlier threads, I have a db that contains columns that are posted for a sports site. One of the fields in my table that contains the column information is the specific sport being written about. Another field contains the name of the author of the column. So, what I would like to do is query the db to achieve the following display result:

Table contains:

Sport Author
NFL Mark
MLB Joe
NFL Matt
NBA John
MLB Steve

Query displays:

NFL
Mark
Matt

MLB
Joe
Steve

NBA
John

Now I know I can use the SELECT DISTINCT(sport) to display each sport individually. But can this be used in conjunction somehow with another function to get the names of the authors of said sport to display underneath? I would appreciate any help on this matter.
Link to comment
Share on other sites

Best you can do is use ORDER BY sport, author

That way you get:

MLB Joe
MLB Steve
NBA John
NFL Mark
NFL Matt

You'll have to do some post processing in PHP to make sure you print, for instance, MLB only once. Something like:

[code]while ($row = mysql_fetch_assoc()) {
   if ($save_sport != $row['sport']) {
      echo $row['sport'] . "<br>\n";
      $save_sport = $row['sport'];
   }
   echo $row['author'] . "<br>\n";
}[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.