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 "

" ;
/* 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 "

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

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.

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 …

Bginner

Hi,

How can I modify your code to display the datas with numerical name ?

Thx

Bginner

It’s ok for me (-> ascii table)……………… !

Thank you for this tuto !

Shaunbo

Thanks for this! What would be the best way to reset and show all results again?

elangovan

very useful tips:) Thanks very much

Steven

This is the code altered to use the mysqli API since the mysql API is being deprecated.