简体   繁体   English

php代码,通过单击页面上的链接,按字母顺序对数据库中的列表进行排序

[英]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. 我对PHP和MySQL比较陌生。 I have a list of information I want to retrieve from the database and display on my html page. 我有一个要从数据库检索并显示在html页面上的信息列表。 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 | 我希望在一页上显示无序列表,然后在我的html页的页眉上显示名为A |的链接。 B | 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. C .....并且当我单击任何字母以根据单击的字母将数据库中未排序列表的第一列排序时。

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 我已经用php搜索了分页,结果很有帮助,但没有显示如何根据特定条件(例如按字母顺序)创建用于对列表进行排序的页面

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 现在在您的sort.php获取按查询排序的值

First get sorting letter from url using $_GET variable 首先使用$_GET变量从url获得排序字母

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

You can do this with SQL queries alone: 您可以单独使用SQL查询来执行此操作:

$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 对于您的“所有”页面,只需使用其他SQL SELECT查询,就可以忽略上面查询中的LIKE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM