By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,591 Members | 650 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,591 IT Pros & Developers. It's quick & easy.

pagination

P: n/a
I tried to do pagination of an article with an arbitrary 2500 characters per
page but if
the cutoff point is not the end of a paragraph (<br><br>), it would be moved
there instead. I am using php5. Here is the skeleton of my code:

$length = strlen($article);
$charsPerPage = 2500;
$numberOfPages = ceil($length/$charsPerPage);
$start = 0;

for($counter=1;counter<$numberOfPages + 1;$counter++) {
$cutOffPoint= strripos(substr($all, $start, $charsPerPage),
'<br><br>');
$pageContent = substr($article, $start, $cutOffPoint-$start);
$start = $cutOffPoint;
echo "page".counter." = ".$pageContent;
}

However, it only works for the first loop for some reason. In the
subsequent loop the $cutOffPoint is located even before the $start (visible
when I
print them out for testing purpose). Could someone show me what I am
missing?
Thanks.


Jun 2 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Peter Parker wrote:
$numberOfPages = ceil($length/$charsPerPage);
This is plain false...
$cutOffPoint= strripos(substr($all, $start, $charsPerPage),
'<br><br>');
$pageContent = substr($article, $start, $cutOffPoint-$start);
$cutOffPoint is relative to "substr($all, $start, $charsPerPage)", not
relative to $article. By the way, why do you have $all and $article??
Besides that, your algorithm doesn't take into account extreme cases: what
if I have no "<br>" at all in my article? What if my article is nothing but
a lot of "<br>"s?

--
----------------------------------
Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

"Only sick music makes money today."
-- Friedrich Nietzsche [1844 - 1900]
Jun 2 '07 #2

P: n/a
Sorry, it should have been '$article' instead of '$all'. I renamed and
missed that one when I posted onto newsgroup. The article format is created
by me so I make sure the format holds. I did print it out to verify. It
contains only text strings in English language with single newline as <br>
and double newlines for new paragraph as <br><br>. Thanks.
"Iván Sánchez Ortega" <ivansanchez-alg@rroba-escomposlinux.-.punto.-.org>
wrote in message news:f3**********@hercules.cohp1...
Peter Parker wrote:
>$numberOfPages = ceil($length/$charsPerPage);

This is plain false...
> $cutOffPoint= strripos(substr($all, $start, $charsPerPage),
'<br><br>');
$pageContent = substr($article, $start, $cutOffPoint-$start);

$cutOffPoint is relative to "substr($all, $start, $charsPerPage)", not
relative to $article. By the way, why do you have $all and $article??
Besides that, your algorithm doesn't take into account extreme cases: what
if I have no "<br>" at all in my article? What if my article is nothing
but
a lot of "<br>"s?

--
----------------------------------
Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

"Only sick music makes money today."
-- Friedrich Nietzsche [1844 - 1900]

Jun 2 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.