Sort alphabetically ... kind of

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sleepydad
Forum Commoner
Posts: 75
Joined: Thu Feb 21, 2008 2:16 pm

Sort alphabetically ... kind of

Post by sleepydad »

I have a database that I would like to allow visitors to my site to search alphabetically. However, due to space on the page, I would like to offer links to sort by names A-G, H-P, Q-Z (Something like that, although the range of the letters is subjective) For example it'll look something like:

Search names:
A-G H-P Q-Z (each of these being a link)

How would I write my query(s) in PHP?

Thanks in advance.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Sort alphabetically ... kind of

Post by AbraCadaver »

Something like this:

Code: Select all

SELECT * FROM table_name WHERE LEFT(name_field, 1) BETWEEN 'A' AND 'G'
In php you could use get vars in your link: search.php?start=A&stop=G

Then use the $_GET['start'] and $_GET['stop'] vars in your query.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
sleepydad
Forum Commoner
Posts: 75
Joined: Thu Feb 21, 2008 2:16 pm

Re: Sort alphabetically ... kind of

Post by sleepydad »

That's what I needed. I had a feeling it would be pretty simple. Thanks much!
Post Reply