ALFIE
ALFIE

Reputation: 31

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

Upvotes: 1

Views: 1157

Answers (2)

Suman Sapkota
Suman Sapkota

Reputation: 570

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

Upvotes: 2

Mark Phillips
Mark Phillips

Reputation: 273

You can do this with SQL queries alone:

$letter = "A";
$query = "SELECT supplier, contact, telephone, email FROM suppliers WHERE supplier LIKE '$letter%'";

For your "ALL" page, just use a different SQL SELECT query omitting the LIKE from the query above

Upvotes: 0

Related Questions