Jump to content

Alphabetize and Group by First Letter


xProteuSx

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/136915-alphabetize-and-group-by-first-letter/
Share on other sites

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.

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.