|
|
|
|
|
|
| |
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... |
|
| |
| 10 PHP Functions I Bet You Didn't Know About! Categories : PHP, PHP Functions, Filesystem, Arrays, Errors and Logging | | | 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 5 of 15 : Rank And File Categories : PHP, Beginner Guides, Filesystem | | | Working With Text Files in PHP Categories : PHP, Filesystem | | | Working with Dates and Times in PHP Categories : PHP, Date Time | | | Date Arithmetic With MySQL Categories : PHP, Databases, MySQL, Date Time | | | PHP CLI and Cron Categories : PHP, CLI, Cron | | | Aspect-Oriented Programming and PHP Categories : PHP, Aspect Oriented Programming | | | Saving Images in MySQL Categories : MySQL, PHP, Graphics, Databases | | | PHP References Explained Categories : PHP References, PHP | | | Beginners guide to PHP and MySQL Categories : PHP, Beginner Guides, Databases, MySQL, Installation | | | Logging with PHP Categories : PHP, Log Files | | | Building A Persistent Shopping Cart With PHP and MySQL Categories : PHP, MySQL, Databases, Ecommerce | | | Who's Linking? Categories : PHP, Beginner Guides, To PHP | |
| |
|
|