Automatically Generate Second Page

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
christh
Forum Commoner
Posts: 25
Joined: Sat Jan 16, 2010 5:27 am

Automatically Generate Second Page

Post by christh »

Hi and I hope you can help

Whilst this is a question on pagination, I'm not looking for help on displaying multiple results over several pages - I 'know' how to do this and use this already on my site - I have a slightly different problem.

I have news articles stored in a SQL database and when one is viewed it can overflow outside it's designated area - I'd like to be able to automatically create a page 2 (or more) link for the article if possible.

I could add the second page to the database and link to it manually but this appears to be a silly solution - I also don't want to use the html overflow:auto as this adds nasty scroll bars and spoils the look of the site.

I'm going to have a play with my current pagination script and see if I can change it some way (I was thinking of using word count to display the second page, rather than records returned (?)) but in the mean time would you have any suggestions/advice.

I have searched for a solution but without success - the standard pagination solution tends to dominates the results.

Thanks in advance.

Chris
rnoack
Forum Commoner
Posts: 34
Joined: Mon May 03, 2010 12:38 am

Re: Automatically Generate Second Page

Post by rnoack »

i've never done this so someone else might have a better way,

but i think what you should try is counting the characters in the string which is the article, and cutting off once you reach a certain point.
it would also help to use the css display:table style on the content box so that if your cut off is too late the container will grow to fit your text.

also make sure to set a doctype before </head> so that internet explorer can do display:table correctly.
christh
Forum Commoner
Posts: 25
Joined: Sat Jan 16, 2010 5:27 am

Re: Automatically Generate Second Page

Post by christh »

Cheers Moack

I put this together and it does the job should anybody else be looking. The import thing was to ensure the page stops at a suitable position, ie at the end of last paragraph rather than mid word or mid sentence.

Code: Select all

$id=$_GET['article_id'];     //id of the news article to be pulled from the database
$start=$_GET['start'];   //start is set to 0 at this stage

//database query in here and assign $article//

$length=strlen($article);	//get length of article
$article=nl2br($article);	//pop line breaks into article
$full_page=mb_substr ($article, $start, 4000);	//total characters required for full page - 4000 in this instance
			
If (strlen($full_page)==4000){		//is this page bigger than 4000 characters - if so...
	$last_para = strrpos($full_page, "\r");		//...find the last carriage return from within the 4000 characters
	$view_page=mb_substr ($article, $start, $last_para);	//$view is now the partial page we want
	echo $view_page;	//show page up to paragraph break
	$start=$start+$last_para+2;   //move $start forward for next page, passed the last line break
	echo "<a href=/news.php?id=$id&s=$start>[Next Page]</a>";  //put in a link to move to next page
}
else{
	$last_para=$length;	//...else the remaining article is less than 4000, so don't find last paragraph
	$view_page=mb_substr ($article, $start, $last_para);							
	echo $view_page;
}				
Post Reply