nonexistentera Posted December 25, 2009 Share Posted December 25, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/186313-get-content-from-table-into-a-list-without-repeating/ Share on other sites More sharing options...
.josh Posted December 25, 2009 Share Posted December 25, 2009 select distinct(author) from quote Quote Link to comment https://forums.phpfreaks.com/topic/186313-get-content-from-table-into-a-list-without-repeating/#findComment-983910 Share on other sites More sharing options...
ignace Posted December 25, 2009 Share Posted December 25, 2009 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>'; } Quote Link to comment https://forums.phpfreaks.com/topic/186313-get-content-from-table-into-a-list-without-repeating/#findComment-983986 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.