0

I'm trying to have mysql outputting a list of article categories. there are lots of articles, each with preset categories that are stored in mysql. But many articles have the same category, so my list get very long with similar category results. My idea is that if the user has posted 1 post in a category, the category gets listed. but this needs to understand that the category should just be listed once even if the user has posted multiple times in that specific category. How can i do this?

This is what i got, but not working:

foreach( $result as $row ) {
if($result>1){
$kategorilink= "{$row['kategori']}";
echo $kategorilink;
}
}
1
  • Identical, the list of categories are preset Commented Jan 17, 2012 at 10:33

2 Answers 2

1

Try SELECT DISTINCT * FROM .... This will give you each different value only once.

Sign up to request clarification or add additional context in comments.

5 Comments

That gives me 1 result in total. ` $query = "SELECT DISTINCT * FROM article where full_name='$safe_name'"; $result = mysql_query($query); while($row = mysql_fetch_array($result)) { $coverlink= $row['kategori']; echo $coverlink; }`
Are there several options in your DB?
hmm, i dont understand the question, but it has multiple posts, some with similar category, some with just 1 in another category. Categories are : sport,gaming,fashion. My alternative is to make one query per category, but i want it to be abit more dynamic than that.
Thanks for helping, i had an error with my var's. fixed it and it worked: $query = "SELECT DISTINCT * FROM article where full_name='$full_navn' group by kategori"; $result = mysql_query($query); while($row = mysql_fetch_assoc($result)) { echo $row['kategori']; } Big ups to you both! =)
It's a little misunderstanding between us. You need to select distinct "a column to contain the different values" from "the table to contain the column"...
1

modifying mysql data in php is not a good idea, you can select disting categories from mysql like

select distinct(category) from article where full_name='$safe_name'

or you can add group by clause to your query to group your result according to categories

select * from article where full_name='$safe_name' group by (category)

if you want to check number of results you can use mysql_num_rows() like

if (mysql_num_row($result) > 1){ //code here}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.