PHP A to Z sorting script
Let's say you have a customer table ( tbl_cutomers) you want to navigate alphabetically by customer last name.
| id | firstname | lastname |
| 1 | John | Smith |
| 2 | Joanne | Johnson |
tbl_customers
To accomplish this task we need to create PHP script that will:
- get the letter user clicked on
- execute MySQL query based on that letter
- display customers whose last name starts with selected letter.
If no letter was clicked we will display all customers. Let's call that script getcustomers.php.
getcustomers.php
|
<? php//first we need to connect to the database //this makes the connection /* Now let's get the letter user clicked on and assign it a variable called $sort */ /* 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. */ //next step is to execute the query. /* 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. */ /* 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. ?> |
As you can see it is very easy to create alphabetical sort using PHP/MySQL.
Emir Plicanic owns and operates a web design company (