Link structure for sorting an HTML table built using PHP
I appreciate any help I can get with this problem:
- I have 2 search forms, one does a keyword search and the other does a search by country, state and city (with an input for each)
- The results are output to a table. The header row contains a link for sorting each column.
- Problem is I don't know how to build my GET url string for sorting and assign them to the links in the headers
Here is some code to get started (I am using MVC)
Functions
function searchkeyword()
{
/* get keyword an query and output paginated result */
}
function searchcategory()
{
/* get categories and query and output paginated result */
}
function sort()
{
$sortby = $_GET['sortby'];
}
function paginate()
{
}
View
<th>
<a href="sort.php?XXXX"&开发者_如何学编程gt;Sort Column</a>
</th>
I'm not sure what should go in XXXX to sort the results.
if you want it to fit in $sortby = $_GET['sortby'];
then you will need to use:
sort.php?sortby=param
where param
is any of the possible sortby fields.
make sure that before putting this an any sql order by you check it yourself, so you don't get errors or attacks. ie:
switch $sortby:
case 'city':
$sort_string = 'ORDER BY `city`;
...
Do you have to use the server to perform the sort? You could do it in Javascript on the client.
For an example, see any Wikipedia article which allows table sorting, such as this.
精彩评论