xProteuSx Posted December 14, 2008 Share Posted December 14, 2008 Ok, this is actually more difficult than I thought it would be, but here's the idea: I have a DB with a listing of countries. I would like to display the contents of this DB, but I need to do it in a very specific way. I would like results to look like this: A Afghanistan Albania Algeria .... B Bahamas Bahrain Bangladesh ... C ... Z ... Zimbabwe It is easy enough to sort the entire list as one, without splitting it up under its letter heading. Also, I can sort the countries letter by letter, but this calls for 26 DB queries, and a lengthy segment of code. This kind of stuff gets done all the time. Has anyone done anything like this? Any ideas, pointers, snippets of code for displaying things in this manner? Thanks a mil! Quote Link to comment Share on other sites More sharing options...
phpSensei Posted December 14, 2008 Share Posted December 14, 2008 Try SELECT DISTINCT UPPER(LEFT(title, 1)) FROM table ORDER BY title Quote Link to comment Share on other sites More sharing options...
DarkSuperHero Posted December 14, 2008 Share Posted December 14, 2008 You Could try obtaining all names from the database and then using a series of array and string functions to order them to your liking... :-) Quote Link to comment Share on other sites More sharing options...
xProteuSx Posted December 14, 2008 Author Share Posted December 14, 2008 phpSensei, Just as I thought, that piece of code only lists the letters that the country names start with. Its an interesting piece of code, and I'll see if I can't implement it in combination with other snippets to put together what I need. DarkSuperHero, You're absolutely right. But to me its a complex series of logical steps. Its making my head spin. I guess I will have to do this. I was hoping that someone had devised a 'smart system' because this is something that would, and does, get used often on a site. I'm just trying to stay away from querying the DB 26 times (one time for each letter of the alphabet). Might have to do that in the end. I'll let you all know how I made out. Quote Link to comment 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.