Return to Snippet

Revision: 22316
at January 8, 2010 19:08 by aristoworks


Initial Code
function paraPag($data) {

		$chars = "<p> </p>"; // CHARACTERS TO REMOVE FROM LEFT AND RIGHT OF ORIGINAL TEXT

		$text = trim($data, $chars);
		
		$delimiter = "<br /><br />"; // COULD BE <BR><BR>, \n\r, 
 OR ANYTHING THAT APPEARS BETWEEN YOUR PARAGRAPHS
		
		$paragraphs = explode($delimiter, $text);
	
		$count = count($paragraphs); // GET THE COUNT OF PARAGRAPHS
		
		$num_page = 10; // THE NUMBER OF PARAGRAPHS YOU WANT DISPLAYED ON EACH PAGE

		$pages = ceil($count / $num_page);  // GET THE NUMBER OF PAGES TOTAL
		
		$output = ''; // INITIATE THE VARIABLE

			// SET THE PAGE NUMBER
			if(isset($_GET['page'])) {
				$page = $_GET['page'];
			} else {
				$page = 1;
			}

			$start = ($page * $num_page) - $num_page;
			
			$end = ($page * $num_page) - 1;

			// PRINT THE PAGE CONTENTS
			for($i=$start; $i<=$end; $i++) {
				
				if($i< $count) {					
				$output .= "<p>".$paragraphs[$i]."</p>\n";
				}

			}

			// PRINT OUT THE PAGE NUMBERS
			for($i=1; $i<=$pages; $i++) {

			if($i == $page) {
				$class = "class=\"activePage\"";
			} else {
				$class = 'class="page"';
			}

			$output .= "<a $class href=\"?page=$i\">$i</a> ";

			}
			
			return $output;

	}

Initial URL
http://www.aristoworks.com

Initial Description
I wrote this really quick and dirty script to paginate a very large chunk of text.  A client site was setup with the understanding their about page would consist of 5-6 paragraphs but their biography ended up being 60 paragraphs.  At the end of the day instead of breaking segments out into separate pages as stored in the database I kept it intact (including the way they edit it in the CMS) but this simple function paginates it only showing a certain number of paragraphs at a time.

Please email code -at- aristoworks.com if you need some clarification on how to use this.

Initial Title
Paginate Paragraphs - Show Certain Number Of Paragraphs Per Page

Initial Tags
mysql, php, text

Initial Language
PHP