Multiple Pages of Data from a Text File - Dividing the Data
(Page 2 of 4 )
Before we can do anything we need to know 3 important things:
How many news items (or whatever) do we want to display?What page are we currently viewing?Based on the answers of the above two questions, where do we begin displaying content?
Each question can be answered individually with a single line of code.
<?php $display = 5; $pg = (isset($_REQUEST['pg']) && ctype_digit($_REQUEST['pg'])) ? $_REQUEST['pg'] : 1; $start = $display * $pg - $display; ?> |
$display is given the value of the maximum amount of news you want to display. This can be virtually any number. $pg first checks to see if we have a _REQUEST variable named "pg" available in the query string, and checks whether it is a valid number with the ctype_digit() function; if it is set and a valid number then we keep that value, otherwise we give it the value 1. The reason we perform the check to make sure it is a valid number, is because the query string might be altered by the visitor and therefore we can't trust it.
The $start variable although simple, takes bit of a further explanation. Whatever news we decide to display will be stored in an array and the value of $start will be the key to let us know what part of the array we should start at (depending what page we're on). This value needs to change dynamically. If we're on page 1, then the value of $start will result in 0 because 5 x 1 - 5 = 0 (that is: $display * $pg - $display). Otherwise, if we're on page 2 then the value of $start will be 5, giving us the "start" of the next 5 items, and so on.
To get the array of news we want to display varies slightly depending on whether you're using a flat file or a database, but it's the same concept.
<?php /* paginating from a database (not including connection) */ $result = mysql_query("SELECT count(*) FROM news_table"); $total = mysql_result($result, 0); $news = mysql_query("SELECT * FROM news_table ORDER BY date_field ASC LIMIT $start, $display");
/* paginating from a flat file */ $data = file('news.txt'); $total = count($data); $news = array_slice(array_reverse($data), $start, $display); ?> |
In both versions, we get the total number of news items ($total) which will be used later. The database version collects only the news items we want to display using MySQL's LIMIT. The flat file version is almost identical, but uses different functions. The array_slice() acts similar to LIMIT giving us only the key elements that we want to display.
In the examples above, with MySQL we sort the new items by a date_field and specify ASC (ascending, or newest items first); whereas with the flat file we simply reverse the data since the newest items are usually appended to the end of the file. You can sort your data any way that you prefer.
Ultimately, to apply pagination to any given page we will be adding roughly seven lines of code (depending if/how you handle your MySQL connection). The 7th line will be a call to the paginate() function, discussed in the next section. Here is where we're at so far:
<?php $display = 5; $pg = (isset($_REQUEST['pg']) && ctype_digit($_REQUEST['pg'])) ? $_REQUEST['pg'] : 1; $start = $display * $pg - $display;
$data = file('news.txt'); $total = count($data); $news = array_slice(array_reverse($data), $start, $display);
/* will draw the page links */ // paginate($display, $pg, $total); ?> |
How you display your news is entirely up to you and shouldn't effect the actual pagination in any way. For testing purposes, you might try something like:
<?php foreach($news as $value) { echo $value.'<hr />'; }
// or...
while($row = mysql_fetch_assoc($news)) { echo $row['title'].'<br />'; echo $row['body'].'<hr />'; } ?> |
With the exception of the paginate() function, the code should be perfectly functional at this point in that changing the query string variable "pg" (ie: script.php?pg=3) will only display the appropriate news items that you want for that specific page. The next thing to do is create the navigation.
Next: Navigating the Data >>
More Database Articles Articles
More By notepad