A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2008 yearcharacter-based pagination - inserting page breaks on text, not punctuation or code - page 1
User InfoPosts
character-based pagination - inserting page breaks on text, not punctuation or code#1
I'm writing code to generate character-based pagination. I have articles in my site that I want to split up based on length.

The code I have so far is working albeit two issues:


It's splitting pages in the middle of words and HTML tags; I want it to
only split after a complete word, tag, or a punctuation mark.
In the pagination bar, it's generating the wrong number of pages.


In the
pagination bar, it's generating the
wrong number of pages.

Need help addressing these two issues. Code follows:

$text = file_get_contents($View);
$ArticleLength = strlen($text);
$CharsPerPage = 5000;
$NoOfPages = round((double)$ArticleLength / (double)$CharsPerPage);
$CurrentPage = $this->ReturnNeededObject('pagenumber');
$Page = (isset($CurrentPage) && '' !== $CurrentPage) ? $CurrentPage : '1';
$PageText = substr($text, $CharsPerPage*($Page-1), $CharsPerPage);
echo $PageText, '<p>';
for ($i=1; $i<$NoOfPages+1; $i++)
{
if ($i == $CurrentPage)
{
echo '<strong>', $i, '</strong>';
}
else
{
echo '<a href="', $i, '">', $i, '</a>';
}
echo ' | ';
}
echo '</p>';


What am I doing wrong?

posted date: 2008-12-11 03:29:00


Re: character-based pagination - inserting page breaks on text, not punctuation or code#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2008-12-11 03:29:01


Re: character-based pagination - inserting page breaks on text, not punctuation or code#3
You're calculating the number of pages wrong... you should be using ceil() not round() (for example 4.1 pages worth of text is still 5 pages to display).To fix the other issue, you're going to have big problems if there's arbitrary HTML in there. For example, you need to know that <div>s and <p>s are OK to split, but <table>s aren't (unless you want to get really fancy)!To do it properly you should use an HTML library to build a tree of elements and then go from there.

posted date: 2008-12-11 03:38:00


Re: character-based pagination - inserting page breaks on text, not punctuation or code#4
Thanks, guys. I put in the fix for the 1st point and it worked beautifully.Hm. I guess it is messy to do the second point. I've found some regex on-line. Will think, write, and get back to you when I make some progress.Thanks again.

posted date: 2008-12-11 04:08:00


Re: character-based pagination - inserting page breaks on text, not punctuation or code#5
Based on your first statement,It's splitting pages in the middle of words and HTML tagsit appears that your character count is being done after markup is inserted. That would imply that e.g. long URLs in links would be counted against the page length you're trying to achieve. However, you didn't say how the articles were being created initially.I'd suggest looking for a point in the process of creating the article where you could examine the raw text. By regarding the actual content (without markup) as a set of paragraphs, and estimating the vertical length of each paragraph based on typical number of characters per line, you can come up with a more consistent sizing.I would also consider only breaking between paragraphs, to keep units of thought together on the same page. Speaking as a reader, I really hate going to sites that force me to pause, hit a button or link, and wait for a page reload, all in the middle of a single thought.

posted date: 2008-12-11 04:57:00


select page: « 1 »
Copyright ©2008-2017 www.momige.com, all rights reserved.