Page 1 of 1

listing links by category

Posted: Wed Jan 15, 2003 11:21 pm
by pinkerton
Here's a problem I've recently run into. I am developing a "favorite links" section of a web site, and I want to somehow show links from a database by category. For instance, I might have a News category that lists CNN.com, MSN.com, and so forth, while also having a few other categories such as Politics and Technology all on the same page.

I have a good understanding of how to list links from a database, but I dont know how to list several links without printing out the respective category name more than once. So the problem i'm running into is that I would have to print out the category of News everytime I print out, say, CNN, MSN, and any new link under that category, so I end up getting needless repetition. Any ideas of how I can avoid this repetition of categories while still printing out all the links?

Posted: Thu Jan 16, 2003 6:48 pm
by fractalvibes
Sure - some psuedo-code

savecat = ""

open db and perform query
while more results
Cat = category value from DB
Link = Link text from DB
If Cat == savecat
PrintCat = " "
Else
PrintCat = Cat
savecat = Cat
End If
write html tags and printcat and link text to page
move to next record in resultset
end while

So that you print out the category only when it changes..

Phil J.