Take the 2-minute tour ×

Hi guys I'm in a bit on a fix here. I know how easy it is to build simple pagination links for dynamic pages whereby you can navigate between partial sets of records from sql queries. However the situation I have is as below:

COnsider that I wish to paginate between records listed in a flat file - I have no problem with the retrieval and even the pagination assuming that the flat file is a csv file with the first field as an id and new reocrds on new lines.

However I need to make a pagination system which paginates backwards i.e I want the LAST entry in the file to appear as the first as so forth. Since I don't have the power of sql to help me here I'm kinda stuck - all I have is a fixed sequence which needs to be paginated, also note that the id mentioned as first field is not necessarily numeric so forget about sorting by numerics here.

I basically need a way to loop through the file but backwards and paginate it as such.

How can I do that - I'm working in php - I just need the code to loop through and paginate i.e how to tell which is the offset and which is the current page etc.

share|improve this question
Can you provide an example of the source file that you are parsing? –  Riley May 26 '10 at 12:59
add comment

2 Answers

I'm assuming you have a well-formed document with delimiters.

$array = explode("<>", $source); //parse data into an array
$backward = array_reverse($array); //entire array is reversed - last elements are now first
share|improve this answer
add comment

Use this code for a jumping off point.

$records = file('filedata.csv');
$recordsInOrder = array_reverse($records);
$first = 5;
$last = 10;
for($x = $first; $x <= $last; $x++) {
    $viewTheseResults[] = $recordsInOrder[$x];

You can use an offset to determine the starting and ending keys in the array similar to how you would if you were pulling the data from a database.

share|improve this answer
add comment

Your Answer


