PHP Development Board php divider

User Options
Register--Login--Top 20 Posters--Search Topics


Forum Main>>Tutorials>>Pagination of articles/stories

New Topic-Reply




Author
Post
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 Sat Feb 06, 2010 08:52:34
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.

Code:


<?php
$numsent=3; //number of sentences per page
if(!isset($_GET['ID']))
{
$ID=0;
}
else
{
$ID=$_GET['ID'];
}
$string="123.234.345.456.567.678.789.890.111.000"; //test string
$stringparts=explode('.',"$string"); //breaks string into parts
$startnum=$ID*$numsent;
$endnum=$startnum+$numsent;
$numpartsinstring=count($stringparts); //counts number of parts in string
for($i=0;$i<$startnum;$i++)
{
unset($stringparts[$i]); //kills parts before
}
for($j=$endnum;$j<$numpartsinstring;$j++)
{
unset($stringparts[$j]); // kill parts after
}
$newstring=implode($stringparts,'.');
print "$newstring";
print "<br><br>";
$numpages=ceil($numpartsinstring/$numsent);
for($i=1;$i<=$numpages;$i++)
{
$actualstart=$i-1;
if($actualstart!=$ID)
{
print "<A href='test.php?ID=$actualstart'>$i</a>&nbsp;";
}
else
{
print "$i&nbsp;";
}
}
?>


So lets go through this code step by step. At the top I declare

Code:

$numsent=3;


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.

Code:

if(!isset($_GET['ID']))
{
$ID=0;
}
else
{
$ID=$_GET['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:

Code:

$string="123.234.345.456.567.678.789.890.111.000"; //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:

Code:

$stringparts=explode('.',"$string"); //breaks string into parts


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:

Code:

$startnum=$ID*$numsent;
$endnum=$startnum+$numsent;


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:

Code:

$numpartsinstring=count($stringparts); //counts number of parts in string


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:

Code:

for($i=0;$i<$startnum;$i++)
{
unset($stringparts[$i]); //kills parts before
}
for($j=$endnum;$j<$numpartsinstring;$j++)
{
unset($stringparts[$j]); // kill parts after
}


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:

Code:

$newstring=implode($stringparts,'.');
print "$newstring";
print "<br><br>";


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:

Code:

$numpages=ceil($numpartsinstring/$numsent);
for($i=1;$i<=$numpages;$i++)
{
$actualstart=$i-1;
if($actualstart!=$ID)
{
print "<A href='test.php?ID=$actualstart'>$i</a>&nbsp;";
}
else
{
print "$i&nbsp;";
}
}
?>


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.

jontyrhodes
Rank:acorn
Group: members
Posts: 1
IP Logged
PM ID and RPS ID: 24259
[PM jontyrhodes]

RPS score: 0
RPS challenge

Posted at Tue Sep 08, 2009 02:09:50
Edit post|Quote
Pagination is the process of dividing web content and displaying it on separate pages. On the Internet, pagination is used for such things as displaying a limited number of results on search engine results pages, or showing a limited number of posts when viewing a forum thread. Pagination is used in some form in almost every web application to divide returned data and display it on multiple pages. Pagination also includes the logic of preparing and displaying the links to the various pages.Pagination can be handled client-side or server-side. Server-side pagination is more common. Client-side pagination can be used when there are very few records to be accessed, in which case all records can be returned, and the client can use Javascript to view the separate pages. By using AJAX, hybid server/client-side pagination can be used, in which Javascript is used to request the subsequent page which is loaded and inserted into the Document Object Model via AJAX.Server-side pagination is appropriate for large data sets providing faster initial page load, accessibility for those not running Javascript, and complex view business logicCorrectly implementing pagination can be difficult. There are many different usability questions such as should "previous" and "next" links be included, how many links to pages should be displayed, and should there be a link to the first and last pages.
-----------------------------
creditcard - auto insurance - mortgage loans

padydude
Rank:acorn
Group: members
Posts: 2
IP Logged
PM ID and RPS ID: 26843
[PM padydude]

RPS score: 0
RPS challenge

Posted at Mon Feb 01, 2010 03:58:40
Edit post|Quote
That’s a great point to bring up. There are certainly a lot of details like that to N10-004 take into consideration. I offer the thoughts above as general inspiration but clearly there are questions like the one you bring up where the most important thing will be working in honest good faith. I don’t know if best practices have emerged around things like that, but I am sure that your job is clearly identified as a fair game if you are commenting as an individual. For the ultimate door to door HP0-S18
success career and to make your dreams come true, our vastly trained faculty is the perfect name. Our direct selling job is intriguing and exciting for all the individuals who are over 18 years and wish to make it big in different certifications. Being a prestigious company, we ensure to provide all the students a comprehensive and success guarantee job environment.70-686


jamespaul
Rank:acorn
Group: members
Posts: 2
IP Logged
PM ID and RPS ID: 26891
[PM jamespaul]

RPS score: 0
RPS challenge

Posted at Thu Feb 04, 2010 07:00:13
Edit post|Quote
On the Internet, pagination is used for such things as displaying a
limited number of results on search engine results pages, or showing a
limited number of posts when viewing a forum thread. Pagination is used
in some form in almost every web application to divide returned data
and display it on multiple pages. Pagination also includes the logic of
preparing and displaying the links to the various pages.Pagination can
be handled client-side or server-side. Server-side pagination is more
common. Client-side pagination can be used when there are very few
records to be accessed, in which case all records can be returned, and
the client can use Javascript to view the separate pages. By using
AJAX, hybid server/client-side pagination can be used.Thanks
The Money System -
Step-by-Step Guide to Making Money Online



grow
Rank:acorn
Group: members
Posts: 8
IP Logged
PM ID and RPS ID: 26893
[PM grow]

RPS score: 0
RPS challenge

Posted at Fri Feb 05, 2010 11:40:39
Edit post|Quote
Good Work

grow
Rank:acorn
Group: members
Posts: 8
IP Logged
PM ID and RPS ID: 26893
[PM grow]

RPS score: 0
RPS challenge

Posted at Fri Feb 05, 2010 11:44:13
Edit post|Quote
Pagination also includes the logic of
preparing and displaying the links to the various pages.Pagination can
be handled client-side or server-side. Server-side pagination is more
common. The Money System - Step-by-Step Guide to Making Money Online

jamespaul
Rank:acorn
Group: members
Posts: 2
IP Logged
PM ID and RPS ID: 26891
[PM jamespaul]

RPS score: 0
RPS challenge

Posted at Sat Feb 06, 2010 08:52:34
Edit post|Quote
In scenarios like this, other sites frequently use pagination, where they divide an article up into some number of pages, and rather than scrolling, you click "next" or a particular page number to move around.Thanks


free people search


Page: 1



Powered by © Chipmunk Board

Flash games Ninja games-Web Design New York