ALFIE ALFIE - 1 month ago 11
MySQL Question

Php code to alphabetically sort a list from the database by clicking on a link on the page

I am relatively new to PHP and MySQL. I have a list of information I want to retrieve from the database and display on my html page. I want on one page to display the unordered list and then on the header of my html page to have links named like A | B | C.....and when I click on any of the alphabet letter to sort the first column of the unsorted list from the database according to the letter of the alphabet clicked.

Here is my code so far...

My code so far

This is how I want my page to look like...

How I want my page to look like

I have googled about pagination using php and the results are helpful but not showing how to create pages for sorting a list based on a certain criteria, like alphabetically

Answer

One way to do is, keep links on Alphabet letter as

 <a href="sort.php"> All </a> |
<a href="sort.php?let=A"> A </a> | 
<a href="sort.php?let=B"> B </a> 

Now in your sort.php get the value sorted by query

First get sorting letter from url using $_GET variable

 <?php
      if(isset($_GET['let']))
       $let = $_GET['let'];
   else
   $let='';
     $query = "SELECT supplier, contact, telephone, email FROM suppliers WHERE supplier LIKE '$let%'";
     // other codes 
    ?>
Comments