简体   繁体   中英

Sorting recordset when using LIMIT in SQL clause with jQuery and PHP

I have a jQuery/PHP tutorial i followed in a book that created pagination that uses Ajax to retrieve the records from a MySQL table each time the page is changed using the limit clause in the SQL statement. I'd like to add the ability to have the user click the column headings to sort them but i tried tablesorter but that just sorts the visible records. Is there a way to sort in the SQL statement and have it remember the sorting if the user clicks the next or previous buttons?

I would suggest having a URL parameter for the page which holds the current column you would like to order by. You can then append something similar to "ORDER BY ".$_GET["order"] to the end of your SQL query. Then simply make each column heading a link to the page with ?order=column added to the end, replacing "column" with whatever column in the table you want it to be ordered by.

Of course, you should use something like mysql_real_escape_string to make sure that the URL parameter is safe to use in the query.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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