[thelist] Splitting Articles across multiple pages with PHP

jon steele jjsteele22 at yahoo.com
Thu Jan 31 12:03:15 CST 2002


Mark Joslyn Wrote:
>Thanks for the comments, however, I am a newbie with PHP and am unfamiliar
>with inserting delimiters. I would like to take a look at the source code
>you developed for your article system if that is not too much trouble.

In your article, put this wherever you want to split up the article:

<!-- Name_of_upcoming_section -->

You can have as many as you want. In my code, I also added the option of having a "header" (i.e.
title of article, so you article will have the format:

<!-- header -->
<h1>Title of article goes here</h1>
<h2>This will show on every page...</h2>
<!-- Name_of_first_section -->
text text text
text text text
<!-- Name_of_second_section -->
text text text
text text text
...
..
.

The titles within the comments will be used for navigation, and the underscores will be converted
to spaces.

Anyway, here's the code =) Jon

<?php if($show){ //show is name of file to show...

	//read content
	$file = 'publications/'.$show.'.txt';
	$fp = fopen($file,'r');
	$content = fread($fp, filesize($file));
	fclose($fp);

	//set delimiter
	$content = str_replace('<!--','§<!--',$content);

	//split
	$secs = explode('§',$content);
	$head = $secs[1];
	for($i=2;$i<sizeof($secs);$i++){
		$sections[] = $secs[$i];
	}

	//set part title in second dimension of array
	for($i=0;$i<sizeof($sections);$i++){
		$temp1 = substr($sections[$i],0,strpos($sections[$i],'>'));
		$temp2 = explode(" ",$temp1);
		$title = str_replace("_"," ",$temp2[1]);
		$titles[$i] = $title;
	}

	//set part int variable (0 if first page or passed in url)
	if(!$part){
		$part=0;
	}

	//set nav bar
	$navBar = '<table width="608" style="border-top:1px solid blue;border-bottom:1px solid
blue;"><tr>';
	$navBar .= '<tr><td colspan="2" align="center">| ';

	for($i=0;$i<sizeof($sections);$i++){
		if($i != $part) $navBar .= '<a class="pubNav" href="?show='.$show.'&part='.$i.'">';
		else			$navBar .= '<span class="pubNav">';
		$navBar .= $titles[$i];
		if($i != $part)	$navBar .= '</a>';
		else			$navBar .= '</span>';
		$navBar .= ' | ';
	}
	$navBar .= '</td></tr><tr><td colspan="2"><hr size="1" color="blue">';
	$navBar .= '</td></tr><tr><td align="left" width="50%">';
	if($part == 0){
		$navBar .= '&nbsp;';
	}else{
		$navBar .=  '<a class="pubNav" href="?show='.$show.'&part='.($part-1).'">Previous -
'.$titles[$part-1].'</a>';
	}
	$navBar .= '</td><td align="right" width="50%">';
	if($part >= sizeof($sections)){
		$navBar .= '&nbsp;';
	}else{
		$navBar .= '<a class="pubNav" href="?show='.$show.'&part='.($part+1).'">'.$titles[$part+1].' -
Next</a>';
	}
	$navBar .= '</tr></table>';

	//echo content
	echo '<!-- Header -->'.$head.'<!-- Header -->'.Chr(10).Chr(10);
	echo '<!-- navBar --><br />'.$navBar.'<br /><!-- navBar -->'.Chr(10).Chr(10);
	echo '<!-- CONTENT -->'.$sections[$part].'<!-- CONTENT -->'.Chr(10).Chr(10);
	echo '<!-- navBar --><br />'.$navBar.'<br /><!-- navBar -->'.Chr(10).Chr(10);
?>

__________________________________________________
Do You Yahoo!?
Great stuff seeking new owners in Yahoo! Auctions!
http://auctions.yahoo.com



More information about the thelist mailing list