Jump to content

Get content from table into a list, without repeating.


nonexistentera

Recommended Posts

Alright this is kinda an odd thing, so I need some help.

 

I have a table "quote" with the three columns `id`, `quote`, and `author`.

 

Now I am wondering if there is anyway that I can output all the different values in the column `author`, and if a repeat is found, not to display it again.

 

I know I can do this in a for loop, but I know there is a better way to do it. Always is.

 

Any help or suggestions are always greatly appreciated.

 

Thanks in advanced.

--nonexistentera

It would help if you told us more about your problem and gave us more specifics, like where do you want to use it? If it's SQL then CV's solution will do but you will get one quote per author (last or first, not sure)

 

If you want to create this in HTML using PHP then it would be something like:

$result = mysql_query('SELECT * FROM quote ORDER BY author');// it's important to sort by author or the author's name will be duplicated across the page

$author = "";
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    if (0 === strcasecmp($author, $row['author'])) {
        echo '<h2>', $row['author'], '</h2>';
        $author = $row['author'];
    }
    echo '<p>', $row['quote'], '</p>';
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.