|
||||
| Register--Login--Top 20 Posters--Search Topics |
Forum Main>>Tutorials>>Pagination of articles/stories | ||
Chipmunk![]() Rank:Settler of Bobland Group: Head Administrator Posts: 2867 IP Logged PM ID and RPS ID: 1 PM [Chipmunk] View Member Photo | Last replied to on Tue May 15, 2007 05:10:02 Edit Post|Quote This is how to paginate a long story/article by sentences. This tutorial will teach you how to split large texts into arrays, get rid of the parts not in the page, collapse the parts you want together and finally paginate the parts. So first I will show you the entire example code, this tutorial does not require any databases.
So lets go through this code step by step. At the top I declare
This is basically saying I want 3 sentences per page. In reality, if you were doing pagination on a long story/article, this would be like 50 for 50 sentences per page or something. Next I set $ID.
$ID will be the parameter I accept in the URL to tell me what page I'm on. If there is nothing passed in, I will assume its zero(1st page). Next I set the test string:
This is the example string I will be paginating. This string has 9 periods and ten sentences, so it will make for a good example since I will have 4 pages using this string at the rate of 3 sentences per page. In reality, $string would be the article or story you are trying to parse. You'd probably either read it in from a file or query it in from a database. In this example we are just going to use the simple string. Then I use the explode function to make the string into an array:
Now $stringparts holds what was in string in array format. So $stringparts[0] is the first sentence and $stringparts[1] is the second sentence and so on. Now we define the starting and ending sentences for the page we are on:
Since $ID is the page we are on and $numsent is the number of sentences per page, it makes sense that our starting sentences would be the page we are on multiplied by the number of sentences per page. Ending sentence is also set to the starting sentence plus the number of sentences per page. Setting these variables that way will give you $numsent of sentences per page, just like what we want. Next we want to count how many parts there are in the array or in other words how many sentences are in the entire string. We can do this with this code:
Now we get to the meaty part of the tutorial. For this page we need to delete the parts(sentences) of the array before the part we want and after the parts we want. So we do this code:
Since the first sentence of the array is always the 0th element, we start $i at zero and we have it loop through the starting number minus 1 of the array. We then use unset() to delete all the elements in that range. This effectively deletes all the sentences in the array before the start of our page. The second loop works the same way, except we loop from the ending sentence of our page to the end of the array and delete the parts in that range using unset(). After deleting these two chunks, we now have an array with just the text we want for the page, but it also has alot of blank parts. We need to now combine those blank parts into a new string with the implode() function. So we do this:
The implode() function simply collapses all the leftover elements of the array together into a new string with the period as the delimiter. The empty parts of the array now have nothing between periods so it just gets rid of those when imploding. The we simply print out the string and two line breaks. Finally we have to make the page numbers show up so we do:
First we have to determine the number of pages we have. We do this by dividing the number of sentences in our original string by the number of sentences we want per page. We use ceil() to round up because if we have 3.2 pages, we want it to show the last sentence on the 4th page. Then we tell $i to start at 1 and go through all the pages we have. We have to set $actualstart to $i-1 because $ID is always one behind $i because when $ID is zero is when we have our 1st page. Then we check to see if $ID is equal to $actualstart or not. If it isn't, we want it to print a hyperlink for the navigation number because it is not the page we are currently on. If it is equal, then just print a number as it is the current page we are on. Thats it for the pagination tutorial. ----------------------------- Chipmunk, Supreme Administrator | ||||||||||
| Celox Rank:acorn Group: members Posts: 3 IP Logged PM ID and RPS ID: 12835 [PM Celox] RPS score: 0 RPS challenge | Posted at Mon May 14, 2007 09:29:25 Edit post|Quote Nice tutorial! This is exactly what I needed for my tutorials CMS. Though I've modded it so it functions on <p> elements instead of sentences (dots).Thanks a lot for this tutorial, keep 'em coming! Celox | ||||||||||
LegosJedi![]() Rank:acorn Group: members Posts: 2 IP Logged PM ID and RPS ID: 12324 [PM LegosJedi] | Posted at Mon May 14, 2007 12:01:46 Edit post|Quote Very nice! I plan on using it! ----------------------------- Firefox owns. Period. [url=http://www.firefox.com/]Get Firefox now![/url] | ||||||||||
Chipmunk![]() Rank:Settler of Bobland Group: Head Administrator Posts: 2867 IP Logged PM ID and RPS ID: 1 [PM Chipmunk] View Member Photo | Posted at Mon May 14, 2007 20:06:29 Edit post|Quote Thanks for the compliments guys, I'm sort of running out of things to write tutorial on though, ideas would be appreciated. I know some of you want me to write an in-depth tutorial on how to write a PHP game like killmonster with extensions. I'm looking to write something less than a 100-page book. ----------------------------- Chipmunk, Supreme Administrator | ||||||||||
| Celox Rank:acorn Group: members Posts: 3 IP Logged PM ID and RPS ID: 12835 [PM Celox] RPS score: 0 RPS challenge | Posted at Tue May 15, 2007 04:39:33 Edit post|Quote Maybe you can make an addition to this pagination tutorial. For example on how to make a max numbers of total pages it shows, so it only shows the page your on in the middle and 2 pages on both sides, below is an example... | 5 | 6 | 7 | 8 | 9 | ..I'm sure you know where I'm talking about, would be great if you could write something up about this since I'm totally clueless on this subject. | ||||||||||
| Celox Rank:acorn Group: members Posts: 3 IP Logged PM ID and RPS ID: 12835 [PM Celox] RPS score: 0 RPS challenge | Posted at Tue May 15, 2007 05:10:02 Edit post|Quote I got a problem with the pagination script. I've modded it but I think it's not something that I've changed. The problem is that it shows a last page but that page isn't there. Though it doesn't do this on all pages. You can view a live example of the problem here: http://www.celoxdesign.net/new/tutorials/viewtut.php?id=20 And here it works fine: http://www.celoxdesign.net/new/tutorials/viewtut.php?id=61 Any help is greatly appreciated. Edit: Another thing I noticed that the script still loads the entire article (in my case tutorials with images). I'd like it to load only the part the user is on, not the entire article. Is this possible? Thanks. Another edit: Fixed now No help needed anymore. | ||||||||||
Page: 1 |