{PAGE_NUM} rep tag - for contentpart pagination

Post custom hacks and enhancements for phpwcms here only. Maybe some of these things will be included in official release later.
Post Reply
User avatar
jsw_nz
Posts: 907
Joined: Fri 2. Apr 2004, 02:42
Location: New Zealand

{PAGE_NUM} rep tag - for contentpart pagination

Post by jsw_nz »

A wee snippet of code
to get page number in situations
where articles have 'paginated contentparts'

Returns -1 for single page (no pagination) or listing
and page number if the article does have paginated contentparts.
first page will be returned as 0, all else as integer 2,3, etc

frontend_render directory

Code: Select all

<?php 
/* ----------------------------------------------------------------------------------------
{PAGE_NUM}  replacement tag
* developed by jsw_nz for phpWcms  
* pulls equivalent of $content['aId_CpPage']
* uses {PAGE_NUM} 
* save as page_number.php in frontend directory
--------------------------------------------------------------------------------------------
*/
function get_page_num(){
	global $_CpPaginate; // sniff for pagination flag
	if($_CpPaginate == 1){
		$page_number = $GLOBALS["content"]['aId_CpPage']; 
	}else{
		$page_number = -1;
	} // end if
	return $page_number;
} // end function

if( ! ( strpos($content["all"],'{PAGE_NUM')=== false ) ) {
  $content["all"] = preg_replace('/\{PAGE_NUM\}/e','get_page_num();',$content["all"]);
 } 	
?>
I use this code to send via FlashVars in custom Flash Header
still guessing someone may find helpful for other situations as well
:)

UPDATE:

if needing total page count

Code: Select all

<?php 
/* ----------------------------------------------------------------------------------------
{PAGE_NUM}  replacement tag
* developed by jsw_nz for phpWcms  
* pulls equivalent of $content['aId_CpPage']
* uses {PAGE_NUM} save as page_number.php in frontend directory
--------------------------------------------------------------------------------------------
*/
function get_page_num($db){
	global $_CpPaginate; // sniff for pagination flag
	if($_CpPaginate == 1){
	   $article_id = $article_id ? $article_id : $GLOBALS["content"]["article_id"];
		$sql_cnt  = "SELECT DISTINCT IF(acontent_paginate_page=1, 0, acontent_paginate_page) AS acontent_paginate_page ";
		$sql_cnt .= "FROM ".DB_PREPEND."phpwcms_articlecontent WHERE ";
		$sql_cnt .= "acontent_aid=".$article_id." AND acontent_visible=1 AND acontent_trash=0 ";
		$sql_cnt .= "AND acontent_block IN ('', 'CONTENT') ORDER BY acontent_paginate_page DESC";
		$sql_cnt  = _dbQuery($sql_cnt);
		$total_pages = count($sql_cnt);
		$page_number = $GLOBALS["content"]['aId_CpPage']." of ".$total_pages; 
	}else{
		$page_number = -1;
	} // end if
	return $page_number;
} // end function

if( ! ( strpos($content["all"],'{PAGE_NUM')=== false ) ) {
  $content["all"] = preg_replace('/\{PAGE_NUM\}/e','get_page_num($db);',$content["all"]);
 } 	
?>
(straight from OliG's code)
User avatar
Oliver Georgi
Site Admin
Posts: 9892
Joined: Fri 3. Oct 2003, 22:22
Contact:

Post by Oliver Georgi »

little hint: Do not use preg_replace for simple replacement! Is slow.

Code: Select all

if(strpos($content["all"],'{PAGE_NUM}')!== false) {
  $content["all"] = str_replace{'{PAGE_NUM}',get_page_num(),$content["all"]);
}
Oliver
Oliver Georgi | phpwcms Developer | GitHub | LinkedIn | Систрон
User avatar
jsw_nz
Posts: 907
Joined: Fri 2. Apr 2004, 02:42
Location: New Zealand

Post by jsw_nz »

cheers Oliver
- always learning - :D
Post Reply