PHP A to Z sorting script
Here is a quick way to filter records from a MySQL table using dynamically generated alphabet links. The MySQL table may look something like this:
| id | firstname | lastname |
| 1 | John | Smith |
| 2 | Joanne | Johnson |
And here is what the dynamically generated alphabet navigation would look like:
The script will do following:
- get the letter user clicked on
- execute MySQL query using that letter
- display filtered results (if no letter was clicked we will display all records)
getcustomers.php
/* Get the letter user clicked on and assign it a variable called $sort */
$sort = $_REQUEST['letter'];
/* Let's check if variable $sort is empty. If it is we will create a query to display all customers alphabetically ordered by last name. */
if($sort == ""){
$qry= "SELECT * FROM tbl_customers ORDER BY lastname ASC " ;
}else{
/* if varible $sort is not empty we will create a query that sorts out the customers by their last name, and order the selected records ascendingly. */
$qry = "SELECT * FROM tbl_customers WHERE lastname LIKE '$sort%' ORDER BY lastname ASC" ;
}
/* Notice the use of '%' wilde card in the above query "LIKE '$sort%'". */
//next step is to execute the query.
$execute = mysql_query($qry) or die(mysql_error());
/* Before we display results let's create our alphabetical navigation. The easiest way to create the navigation is to use character codes and run them through the "for" loop. */
echo "
" ;
for ($i = 65; $i < 91; $i++) {
printf('%s | ',
$PHP_SELF, chr($i), chr($i));
}
echo "
" ;
/* now we are ready to display the results. Since out tbl_customers table has only three fileds we will display the results in a paragraphs. In the real world you may need to display the results in a table.
To display the results we will use "do while" loop to fetch the results. If no customers are found we will display an error message. */
if(mysql_num_rows($execute)>0){
do{
echo "
" .$result['id']. " " .$result['firstname']. " " .$result['lastname']. "
" ;
}while($result = mysql_fetch_assoc($execute));
}else{
echo "
No customer found.
" ;
}

rinaldo
this is important to display more results from database you can identify easy your
value result . thanks keep going….>
Brian
Hi,
I am trying to implement your A to Z script into my site. All is good up to the output of the script. How come I am getting all results from the db before clicking on a letter? Also, when I click on the associating letter (letter that should be where the ads are according to the results) I get nothing / nada. This is the same for the other letters as well, and it doesn’t show the ‘No customer found.’ message that is supposed to show if no results are found. Can you help resolve?
The page: http://reunitemysite.com/getads.php
Thank you
Brian
eplicanic
The script if made to sort/filter, meaning it displays all results and sorts/filters them after one of the letters is clicked. Both of your samples start with letter T. Add few more rows of test where the content starts with a different letter.
I hope this gives you a good starting point to build something that will fit your own needs.
Cheers!
Brian
Thank you eplicanic! I am going to try that now. Hope it works!
Brian
I added a couple of more entries to the database, and still have the same results. Any other ideas?
eplicanic
In your query: do you have % at the end of $sort (LIKE ‘$sort%’ )?
Brian
Yes, I do have the $sort% in my query.
Brian
I haven’t tested this idea, but maybe it’s because I am trying to titles, it’s for advertisements. I didn’t try this script on a database for sorting user names. Maybe that’s the issue? It’s the only thing I can think of, being that the titles have spaces in the title, where as last names for example do not have a space.
What do you think?
Brian
I tried this on another row in that same database / same table / different row (of course, just said that)…Same results, so there’s something wrong somewhere in the code. Anyone have any suggestions?
Thank you
Brian
Brian
Hi,
Do you know how to put a number search in the code? For ex: 0-9?
Brian
Hi,
I don’t know if you got the last message on how I got the code to work. If not, here it is:
if($sort = “”) was incorrect…It should have been: if($sort == “”) (double ==)
the php equality operator is ‘==’, NOT ‘=’ which is the assignment operaqtor
I found that on another site.
eplicanic
Facepalm!!! Embarrassing! Of course it’s == and not =
I should have caught it, but I didn’t, so thanks for catching it!
Brian
Hi,
Could you help me get a pagination script working with your script so I can paginate the alphabetical index? To make the results into multiple pages.
Dimitrios
Hi there ,
At first , I would say that your blog is a good place to stop for a while .
Though , this script isn’t recommended for production environments .
My golden rules when working with PHP are :
1) Never use $_REQUEST super-globals –> it’s highly dependent on the configuration of your server’s environment variables ( variables_order , register_globals ) . So in some cases your script my be vulnerable to script-kidies .
2) Always filter data that is provided from visitors . Nowadays a 15-year script-kidie can embed malicious code (Javascript) into your website that will “disappoint ” your visitors . or , even crash your database . Fortunately PHP has a bunch of filter functions that defense your server against SQL-injection and CSSF – attacks .
Well , I’m not a security freak , but basic security measures should be on the first lines of our priority list .
Nice blog anyway , keep publishing articles …