|
|
|
|
|
|
| |
Page 1
I'll assume you already know how to store your data in the txt file, otherwise what are you wanting to create multiple pages with? If not, check out this tutorial on Creating a News System with PHP. So let's get right to the point shall we. There are only 2 steps, pretty much, to achieving multiple pages of data.
- define a default page.
- decide how much data to display.
We'll take a look at the above steps in detail. In step one, we will define a default page.
|
|
if($page <= 0)
{
$page = 1;
}
|
|
|
The above reads as: if page is not defined (less than or equal to 0) then we're on page 1. Simple enough, right? M'kay.
In step two, we'll decide how much data to display. First, we'll need to store our data from the txt file into an array so we have something to work with. Let's pretend your txt file is called "data.txt" - then we can create an array containing the information from the txt file with the following code.
|
|
$myArray = file("data.txt");
|
|
|
|
Because of the fact that data is usually stored from "oldest to newest" when writing to a txt file, we'll probably want to display the newest data first eh? In order to achieve this we'll just reverse the data in our array with the following code. |
|
$myArray = array_reverse($myArray);
|
|
|
Okay, so now we have an array called "$myArray" which contains all the data from "data.txt" and has it stored from newest to oldest, and we're still on page 1. Now we need to decide how much data to display, how about 5 lines? To display a given amount of information from an array we'll use the "array_slice" function to extract what we want to display from "$myArray" - and then store that data in another array called "$news" as follows. |
|
$display = 5;
$start = ($page * $display) - $display;
$news = array_slice($myArray, $start, $display);
|
|
|
The "$start" above defines what key of the array we want to start at, and the "$display" defines how many elements to display. Okay sorry don't get confused, the reason for the math in the variable "$start" is because we want the value to change dynamically depending on what page we're viewing. Consider the following.
If we're on page 1, then the value of $start will result in "0" because (1x5)-5=0, so the "array_slice" function will pass 5 elements (starting at 0) to our $news (keys: 0, 1, 2, 3 and 4). Otherwise, if we're on page 2 then the value of $start will be 5, which will result in (keys: 5, 6, 7, 8 and 9). This will determine the appropriate news to display depending on what page we're viewing, make sense?
Now we'll print those five lines of data to the browser.
|
|
foreach($news as $key=>$value)
{
print("line $key: $value<br>\n");
}
|
|
|
Read More... |
|
| |
| PHP 101 Part 5 of 15 : Rank And File Categories : PHP, Beginner Guides, Filesystem | | | 10 PHP Functions I Bet You Didn`t Know About Categories : PHP, PHP Functions, Filesystem, Arrays, Errors and Logging | | | Working With Text Files in PHP Categories : PHP, Filesystem | | | File And Directory Manipulation In PHP (part 1) Categories : PHP, Directories, Filesystem | | | File And Directory Manipulation In PHP (part 2) Categories : PHP, Filesystem, Directories | | | PHP 101 Part 15 of 15 : No News Is Good News Categories : PHP, Beginner Guides, Content Management | | | Using If Else Statements Categories : PHP, Beginner Guides | | | Setup and Install Apache and PHP4 on Windows Categories : PHP, PHP Configuration, Apache, Windows 2000 | | | Create Your Own Search Engine with PHP and Google Web Services Categories : PHP, Search, Web Services | | | Making Sense Of PHP Errors Categories : PHP, Errors and Logging | | | Build Your Own KlipFolio Klip With PHP Categories : XML, Content Management, PHP | | | Grabb'n and Pars'n Categories : Beginner Guides, PHP, To PHP | | | Building a Counter Categories : PHP, Cookies | | | PHP for Beginners by a Beginner: Simple Login, Logout, and Session Handling Categories : PHP, Sessions, Authentication | | | PHP 101 Part 2 of 15 : Calling All Operators Categories : PHP, Beginner Guides, Operators | |
| |
|
|