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
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.