Question:
PHP Sorting and Pagination?
sas0riza
2007-05-06 13:34:34 UTC
Hi,
I'm parsing a XML file with PHP and displaying the results in an HTML table. I want to be able to click on a column header and toggle between ascending and descending sort; how do I go about doing this? Finally, I like to do pagination too and how do I go about this as well?

Any help is greatly appreciated.

Thanks.
Three answers:
2007-05-06 18:37:21 UTC
>> I want to be able to click on a column header and toggle between ascending and descending sort; how do I go about doing this?



Load each record into a two dimensional associative array. For example, let's suppose you get four columns for each record from your HTML: name, age, town, job.



$row[0]['name'], $row[0]['age'], $row[0]['town'], $row[0]['job'] is one record.



$row[1]['name'], $row[1]['age'], $row[1]['town'], $row[1]['job'] is the second record. Etc.



To sort, you would use array_multisort:



http://us2.php.net/manual/en/function.array-multisort.php



>> Finally, I like to do pagination too and how do I go about this as well?



Pagination is simple. You set the number of records you want per page, get the page number from a GET variable, then move to the proper part of the array.



$pagesize = 10; // ten records per page

$page = $_GET['p']';

$start = ($page - 1) * $pagesize;

$size = count($row);

$totalpages = ceil($size / $pagesize);



for($i = $start; $i <= $size; $i++) {

echo "$row[$i][name] $row[$i][age] $row[$i][town] $row[$i][job]
\n";

}



for($i = 1; $i <= $totalpages; $i++) {

echo "$i ";

}
2016-12-28 20:23:17 UTC
So thc83f57c786ab4a39efab23731c7ebc othc83f57c786ab4a39efab23731c7ebcr c83f57c786ab4a39efab23731c7ebcnswc83f57c... quc83f57c786ab4a39efab23731c7ebcry wc83f57c786ab4a39efab23731c7ebcc83f57c78... artwork yet wc83f57c786ab4a39efab23731c7ebcc83f57c78... dc83f57c786ab4a39efab23731c7ebcc83f57c78... thc83f57c786ab4a39efab23731c7ebc pc83f57c786ab4a39efab23731c7ebcrson's whoc83f57c786ab4a39efab23731c7ebcc83f57c... c83f57c786ab4a39efab23731c7ebcc83f57c786... row. If thc83f57c786ab4a39efab23731c7ebct's no longer whc83f57c786ab4a39efab23731c7ebct you wc83f57c786ab4a39efab23731c7ebcnt, replace c83f57c786ab4a39efab23731c7ebcc83f57c786... SET c83f57c786ab4a39efab23731c7ebcc83f57c786... = '' the situation c83f57c786ab4a39efab23731c7ebcc83f57c786... = '$_POST_[c83f57c786ab4a39efab23731c7ebcc... wc83f57c786ab4a39efab23731c7ebcc83f57c78... sc83f57c786ab4a39efab23731c7ebct c83f57c786ab4a39efab23731c7ebct to bc83f57c786ab4a39efab23731c7ebcc83f57c78... c83f57c786ab4a39efab23731c7ebcnstc83f57c... Thc83f57c786ab4a39efab23731c7ebcn in straight forward terms chc83f57c786ab4a39efab23731c7ebcck for thc83f57c786ab4a39efab23731c7ebct vc83f57c786ab4a39efab23731c7ebcc83f57c78... c83f57c786ab4a39efab23731c7ebcn your c83f57c786ab4a39efab23731c7ebcc83f57c786... routc83f57c786ab4a39efab23731c7ebcnc83f5...
Ereric
2014-08-03 22:49:27 UTC
In a very basic sense, pagination occurs when a website segments content over the span of multiple pages.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...