HOWTO: Proper news pagination (with page numbers!)

Do something cool with CMS? Show us ...
This board is for 'Answers', and the discussion of answers... Not for questions.
Jos
Support Guru
Support Guru
Posts: 4017
Joined: Wed Sep 05, 2007 8:03 pm
Location: The Netherlands

Re: HOWTO: Proper news pagination (with page numbers!)

Post by Jos »

this is not php code for a UDT, this is Smarty code for a template
swarfega
Forum Members
Forum Members
Posts: 174
Joined: Mon Sep 06, 2010 10:51 am

Re: HOWTO: Proper news pagination (with page numbers!)

Post by swarfega »

yea I added it as a udt and called it in a content page.
carasmo
Power Poster
Power Poster
Posts: 506
Joined: Thu Feb 08, 2007 6:11 pm
Location: Florida

Re: HOWTO: Proper news pagination (with page numbers!)

Post by carasmo »

I am wanting to do the 1 2 3 4 5 pagination on the news and the cgblog module, but I can't follow this thread and am unsure what to do. Any information for the latest version of CMSMS?

Thanks!
User avatar
Dr.CSS
Moderator
Moderator
Posts: 12709
Joined: Thu Mar 09, 2006 5:32 am
Location: Arizona

Re: HOWTO: Proper news pagination (with page numbers!)

Post by Dr.CSS »

I use the jquery cycle plugin to make pagination in modules it can be numbers or < > or images, whatever...
carasmo
Power Poster
Power Poster
Posts: 506
Joined: Thu Feb 08, 2007 6:11 pm
Location: Florida

Re: HOWTO: Proper news pagination (with page numbers!)

Post by carasmo »

Is there a "how to" place I can look for using the cycle and the 1 2 3 in blog and news? I googled this and found only one cool thing related, I suspect Ajax is involved?

http://calguy1000.com/Blogs/9/60/jquery ... -news.html

Thanks!
User avatar
Dr.CSS
Moderator
Moderator
Posts: 12709
Joined: Thu Mar 09, 2006 5:32 am
Location: Arizona

Re: HOWTO: Proper news pagination (with page numbers!)

Post by Dr.CSS »

Used here, the blog post and the recent post on the right, also in Episodes page...

http://www.vobuzzweekly.com/blog.html

Used in Products module in this page and its children, categories are used for background images in small boxes and the page background in detail view...

http://www.cantinastrella.com/wijnen.html
Jean le Chauve

Re: HOWTO: Proper news pagination (with page numbers!)

Post by Jean le Chauve »

brentnl wrote:
Peciura wrote:

Code: Select all

* Create array of pagination links
*
* @params	string	$params['link']		Mandatory. Href or complete link (href will be extracted).
* @params	string	$params['current']	Mandatory. Current page number
* @params	string	$params['max']		Mandatory. Max page number
* @params	string	$params['param']	Mandatory. Unique part of url param. e.g. 'pagenumber'
* @params	string	$params['assign']	Mandatory. Name of variable to assign result array($page_number=>'http://www... ', ...)
* @params	string	$params['pattern']	Pagination pattern default '10,10'.
* @params	string	$params['extreme']	Include first and last links
*
*/

$default_pattern = '10,10';

if (!empty($params['link'])		&&
	!empty($params['current'])	&&
	!empty($params['max'])		&&
	!empty($params['param'])	&&
	!empty($params['assign'])){

	$link = preg_replace("/.*href=["\' ]*([^"\']*).*/", '$1', $params['link']);
	$link = html_entity_decode(trim(strip_tags($link)));	//extract href
	$param = trim($params['param']);
	$assign = trim($params['assign']);
	$max = intval($params['max']);
	$current = intval($params['current']);
	$params['pattern'] = (empty($params['pattern']))? $default_pattern : $params['pattern'];
	$pattern = explode(',', trim($params['pattern']));
	$extreme = (empty($params['extreme']))? FALSE : TRUE;

	if(strpos($link, $param.'=')){
		$gCms = cmsms(); [color=grey]//global $gCms;[/color]
		$smarty = $gCms->GetSmarty();
		$links = array();

		$i_min = $i_max = $current;
		foreach($pattern as $exp=>$count){
			for($i = 1; $i  ($delta + 1)){
					$i_min -= $delta;
					$links[$i_min] = htmlentities(preg_replace( "/$param=\d+/", $param.'='.$i_min, $link));
				}
				if($i_max assign($assign, $links);
	}
}
When I try to add the UDT code mentioned above I get an error like this:

Code: Select all

Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /www/p/a/r/domain.nl/public_html/inloggen/edituserplugin.php(108) : eval()'d code on line 9 Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /www/p/a/r/domain.nl/public_html/inloggen/edituserplugin.php(108) : eval()'d code on line 9 
How can I fix this?
There is an error in the preg_replace :

Code: Select all

$link = preg_replace("/.*href=["\' ]*([^"\']*).*/", '$1', $params['link']);
should be :

Code: Select all

$link = preg_replace("/.*href=[\"\' ]*([^\"\']*).*/", '$1', $params['link']);
carasmo
Power Poster
Power Poster
Posts: 506
Joined: Thu Feb 08, 2007 6:11 pm
Location: Florida

Re: HOWTO: Proper news pagination (with page numbers!)

Post by carasmo »

This UDT is displays a lot errors.


Also, what is:

Code: Select all

      $gCms = cmsms(); [color=grey]//global $gCms;[/color]
carasmo
Power Poster
Power Poster
Posts: 506
Joined: Thu Feb 08, 2007 6:11 pm
Location: Florida

Re: HOWTO: Proper news pagination (with page numbers!)

Post by carasmo »

danmburu wrote:I customized the code from the German website...
Thank! This works:

Code: Select all

{if $pagenumber <= 1}
	{assign var="page_link" value=$nexturl}
{else}
	{assign var="page_link" value=$prevurl}
{/if}

{if $pagecount > 1}
<ul>
{section name="pages" start=1 loop=$pagecount+1}
<li>
{if $smarty.section.pages.index == $pagenumber}
<span class="current_news_page pagenav">
{else}
<a class="pagenav" href="{$page_link|regex_replace:"/pagenumber=\d+/":"pagenumber=`$smarty.section.pages.index`"}">
{/if}
{$smarty.section.pages.index}
{if $smarty.section.pages.index == $pagenumber}
</span>
{else}
</a>
{/if}
</li>
{/section}
</ul>
{/if}
carasmo
Power Poster
Power Poster
Posts: 506
Joined: Thu Feb 08, 2007 6:11 pm
Location: Florida

Re: HOWTO: Proper news pagination (with page numbers!)

Post by carasmo »

Dr.CSS wrote:Used here, the blog post and the recent post on the right, also in Episodes page...

http://www.vobuzzweekly.com/blog.html

Nice, but does the page get bogged down, if you are splitting out hundreds in groups of 20 per page (fake page) this way?

Let's just say you are showing all of the posts since the beginning of time with no archive.
carasmo
Power Poster
Power Poster
Posts: 506
Joined: Thu Feb 08, 2007 6:11 pm
Location: Florida

Re: HOWTO: Proper news pagination (with page numbers!)

Post by carasmo »

@Dr. CSS:

Can you share how you wrapped every 5 of the for each loop in the cycle div?

Thanks!
carasmo
Power Poster
Power Poster
Posts: 506
Joined: Thu Feb 08, 2007 6:11 pm
Location: Florida

Re: HOWTO: Proper news pagination (with page numbers!)

Post by carasmo »

Never mind, got it to work with ajax. Thanks!
carasmo
Power Poster
Power Poster
Posts: 506
Joined: Thu Feb 08, 2007 6:11 pm
Location: Florida

Re: HOWTO: Proper news pagination (with page numbers!)

Post by carasmo »

HUGE, HUGE thanks to vilkis in this thread:
http://forum.cmsmadesimple.org/viewtopi ... 71#p286071

+ some modifications and this smarty code does page numbers and if you have loads and loads of pages, then it adjusts accordingly based on what page you're on. IT ROCKS!!!!

This goes in replacement of the news or cgblog pagination in the SUMMARY templates:

Code: Select all

{strip}{*pagination begin*}
{if $pagecount>1}
{if $pagenumber < $pagecount}
{capture assign=pageurl}
{$nextpage|regex_replace:'/href=\"(.*)\"/':'$1'|replace:'<a ':''|replace:'>&rarr;</a>':''} {*adjust according to your lang file*}
{/capture}
{else}
{capture assign=pageurl}
{$prevpage|regex_replace:'/href=\"(.*)\"/':'$1'|replace:'<a ':''|replace:'>&larr;</a>':''} {*adjust according to your lang file*}
{/capture}
{/if}
{capture assign=from_start_to_num}
{$pagenumber-2}
{/capture}
{capture assign=from_num_to_end}
{$pagenumber+2}
{/capture}
<ul style="clear:both;" id="pagenav">
{if $pagenumber > 1}
<li><a href="{$prevurl}">Previous</a></li>
{/if}
{if $from_start_to_num>1}
{if $pagenumber eq 1}
<li><span class="selected">1</span></li>
{else}
{capture assign=pagestr}
pagenumber=1
{/capture}
<li><a href="{$pageurl|regex_replace:'/pagenumber=(\d+)/':$pagestr}">1</a></li>
{/if}
{if $pagenumber eq 2}
<li><span class="selected">2</span></li>
{else}
{capture assign=pagestr}
pagenumber=2
{/capture}
<li><a href="{$pageurl|regex_replace:'/pagenumber=(\d+)/':$pagestr}">2</a></li>
{/if}
<li class="continue">...</li>
{capture assign=pagestr}
pagenumber={$pagenumber-1}
{/capture}
<li><a href="{$pageurl|regex_replace:'/pagenumber=(\d+)/':$pagestr}">{$pagenumber-1}</a></li>
<li><span class="selected">{$pagenumber}</span></li>
{else}
{section name=foo start=1 loop=$pagenumber+1 step=1}
{capture assign=pagestr}pagenumber={$smarty.section.foo.index}{/capture}
{capture assign=pageurl1}{$pageurl|regex_replace:'/pagenumber=(\d+)/':$pagestr}{/capture}
{if  $smarty.section.foo.index eq $pagenumber}
<li><span class="selected">{$smarty.section.foo.index}</span></li>
{else}
<li><a class="selected" href="{$pageurl1}">{$smarty.section.foo.index}</a></li>
{/if}
{/section}
{/if}
{if $from_num_to_end<$pagecount}
{capture assign=pagestr}
pagenumber={$pagenumber+1}
{/capture}
<li><a href="{$pageurl|regex_replace:'/pagenumber=(\d+)/':$pagestr}">{$pagenumber+1}</a></li>
<li class="continue">...</li>
{capture assign=pagestr}
pagenumber={$pagecount-1}
{/capture}
<li><a href="{$pageurl|regex_replace:'/pagenumber=(\d+)/':$pagestr}">{$pagecount-1}</a></li>
{capture assign=pagestr}
pagenumber={$pagecount}
{/capture}
<li><a href="{$pageurl|regex_replace:'/pagenumber=(\d+)/':$pagestr}">{$pagecount}</a></li>
{else}
{section name=foo start=$pagenumber+1 loop=$pagecount+1 step=1}
{capture assign=pagestr}pagenumber={$smarty.section.foo.index}{/capture}
{capture assign=pageurl1}{$pageurl|regex_replace:'/pagenumber=(\d+)/':$pagestr}{/capture}
{if  $smarty.section.foo.index eq $pagenumber}
<li><span class="selected">{$smarty.section.foo.index}</span></li>
{else}
<li><a class="selected" href="{$pageurl1}">{$smarty.section.foo.index}</a></li>
{/if}
{/section}
{/if}
{if $pagenumber < $pagecount}
<li><a href="{$nexturl}">Next</a></li>
{/if}
</ul>
{/if}
{*pagination end*}{/strip}
This is the CSS

Code: Select all

/*  news/cgblog summary pagination
================================================== */
#pagenav {
	font-family: arial;
	clear:both;
	font-size: .85em;
	margin: 0 0 2em;
	padding: 0;
	list-style: none;
	position: relative;
	left: -2px;
}
#pagenav li {
	display: inline-block;
	background: #ddd;
	margin: 0 2px;
	line-height: 1;
}
#pagenav li a {
	display: block;
	padding: 5px;
	text-decoration: none;
}
#pagenav li a:hover {
	background: #333;
	color: #fff;
}
#pagenav span {
	font-weight: 700;
	background: #000;
	color: #fff;
	display: block;
	padding: 5px;
}
#pagenav li.continue {
	padding: 0;
	background: none;
	font-size: 1.5em;
}
/*using an html class and modernizer, set according to your set up, IE 6 hacks or fixes not included*/
.lt-ie8 #pagenav li {
	zoom: 1;
	display: inline;
}
/* did not test anything in IE so fix accordingly */
Attached is what it does.
Attachments
Screen Shot 2012-09-11 at 7.47.47 PM.png
Screen Shot 2012-09-11 at 7.47.47 PM.png (6.38 KiB) Viewed 12983 times
Last edited by carasmo on Wed Sep 12, 2012 1:43 pm, edited 1 time in total.
vilkis

Re: HOWTO: Proper news pagination (with page numbers!)

Post by vilkis »

thanks for sharing.
vilkis
nervino
Power Poster
Power Poster
Posts: 448
Joined: Sun Dec 28, 2008 12:15 pm
Location: Roma, Italy

Re: HOWTO: Proper news pagination (with page numbers!)

Post by nervino »

Hi, I've found this post and I agree with you carasmo: IT ROCKS!!!!

Well, I'm trying to use your Smarty code with pretty urls, but I have a problem: all links have the same pagenumber (2). After a lot of attempts to debug it, I still can't figure out a solution.

If I remove pretty url, all works fine.

May someone give me a suggestion?

Thank you all.

Code: Select all

Here is my PHP code:
// DEBUG (remove)
$pagelimit = 1;
// END DEBUG




// Get the number of rows (so we can determine the numer of pages)
$pagecount = -1;
$startelement = 0;
$pagenumber = 1;

  // get the total number of items that match the query
  // and determine a number of pages
  
  //$row2 = $db->GetRow($query2,$parms);
  $row2 = $db->GetRow($query2);
  $count = intval($row2['count']);
  if( isset( $params['start'] ) )
    {
      $count -= (int)$params['start'];
    }
  $pagecount = (int)($count / $pagelimit);
  if( ($count % $pagelimit) != 0 ) $pagecount++;

if( isset( $params['pagenumber'] ) && 
    $params['pagenumber'] != '' )
  {
    // if given a page number, determine a start element
    $pagenumber = (int)$params['pagenumber'];
    $startelement = ($pagenumber-1) * $pagelimit;
	
$prettyurl = $returnid.'/Listing/'.$params['pagenumber']."/page";
	
  }
if( isset( $params['start'] ) )
  {
    $startelement = $startelement + (int)$params['start'];
  }

// Assign some pagination variables to smarty
if( $pagenumber == 1 )
  {
    $smarty->assign('prevpage',$this->Lang('prevpage'));
    $smarty->assign('firstpage',$this->Lang('firstpage'));
	
	$prettyurl = $returnid.'/Listing/'.$params['pagenumber']."/page";
  }
 else
  {
    $params['pagenumber']=$pagenumber-1;
	
$prettyurl = $returnid.'/Listing/'.$params['pagenumber']."/page";
	
    $smarty->assign('prevpage',
		    $this->CreateFrontendLink($id,$returnid,'default',
					      $this->Lang('prevpage'),'','','','','','',$prettyurl));
    $smarty->assign('prevurl',
		    $this->CreateFrontendLink($id,$returnid,'default','',
					      '','',true,'','','',$prettyurl));
    $params['pagenumber']=1;
	
	$prettyurl = $returnid.'/Listing/'.$params['pagenumber']."/page";
	
    $smarty->assign('firstpage',
		    $this->CreateFrontendLink($id,$returnid,'default',
					      $this->Lang('firstpage'),'','','','','','',$prettyurl));
    $smarty->assign('firsturl',
		    $this->CreateFrontendLink($id,$returnid,'default','',
					      '','',true,'','','',$prettyurl));
  }
    
if( $pagenumber >= $pagecount )
  {
    $smarty->assign('nextpage',$this->Lang('nextpage'));
    $smarty->assign('lastpage',$this->Lang('lastpage'));
  }
 else
   {
     $params['pagenumber']=$pagenumber+1;

	 
$prettyurl = $returnid.'/Listing/'.$params['pagenumber']."/page";
	 
     $smarty->assign('nextpage',
						   $this->CreateFrontendLink($id,$returnid,'default',
					       $this->Lang('nextpage'),
					       '','','','','rel="next"','',$prettyurl));
					
						   
     $smarty->assign('nexturl',
		    $this->CreateFrontendLink($id,$returnid,'default','',
					      '','',true,'','','',$prettyurl ));
     $params['pagenumber']=$pagecount;
	 
	 $prettyurl = $returnid.'/Listing/'.$params['pagenumber']."/page";
	 
     $smarty->assign('lastpage',
		     $this->CreateFrontendLink($id,$returnid,'default',
					       $this->Lang('lastpage'),
					       '','','','','','',$prettyurl));
     $smarty->assign('lasturl',
		    $this->CreateFrontendLink($id,$returnid,'default','',
					      '','',true,'','','',$prettyurl));
   }
$smarty->assign('pagenumber',$pagenumber);
$smarty->assign('pagecount',$pagecount);
$smarty->assign('oftext',$this->Lang('prompt_of'));
$smarty->assign('pagetext',$this->Lang('page'));
This is the tpl:

Code: Select all

{*pagination begin*}
{if $pagecount>1}
{if $pagenumber < $pagecount}
{capture assign=pageurl}
{$nextpage|regex_replace:'/href=\"(.*)\"/':'$1'|replace:'<a ':''|replace:'>></a>':''} {*adjust according to your lang file*}
{/capture}
{else}
{capture assign=pageurl}
{$prevpage|regex_replace:'/href=\"(.*)\"/':'$1'|replace:'<a ':''|replace:'>></a>':''} {*adjust according to your lang file*}
{/capture}
{/if}
{capture assign=from_start_to_num}
{$pagenumber-2}
{/capture}
{capture assign=from_num_to_end}
{$pagenumber+2}
{/capture}
<ul style="clear:both;" id="pagenav">
{if $pagenumber > 1}
<li><a href="{$prevurl}">Previous</a></li>
{/if}
{if $from_start_to_num>1}
{if $pagenumber eq 1}
<li><span class="selected">1</span></li>
{else}
{capture assign=pagestr}
pagenumber=1
{/capture}
<li><a href="{$pageurl|regex_replace:'/pagenumber=(\d+)/':$pagestr}">1</a></li>
{/if}
{if $pagenumber eq 2}
<li><span class="selected">2</span></li>
{else}
{capture assign=pagestr}
pagenumber=2
{/capture}
<li><a href="{$pageurl|regex_replace:'/pagenumber=(\d+)/':$pagestr}">2</a></li>
{/if}
<li class="continue">...</li>
{capture assign=pagestr}
pagenumber={$pagenumber-1}
{/capture}
<li><a href="{$pageurl|regex_replace:'/pagenumber=(\d+)/':$pagestr}">{$pagenumber-1}</a></li>
<li><span class="selected">{$pagenumber}</span></li>
{else}
{section name=foo start=1 loop=$pagenumber+1 step=1}
{capture assign=pagestr}pagenumber={$smarty.section.foo.index}{/capture}
{capture assign=pageurl1}{$pageurl|regex_replace:'/pagenumber=(\d+)/':$pagestr}{/capture}
{if  $smarty.section.foo.index eq $pagenumber}
<li><span class="selected">{$smarty.section.foo.index}</span></li>
{else}
<li><a class="selected" href="{$pageurl1}">{$smarty.section.foo.index}</a></li>
{/if}
{/section}
{/if}
{if $from_num_to_end<$pagecount}
{capture assign=pagestr}
pagenumber={$pagenumber+1}
{/capture}
<li><a href="{$pageurl|regex_replace:'/pagenumber=(\d+)/':$pagestr}">{$pagenumber+1}</a></li>
<li class="continue">...</li>
{capture assign=pagestr}
pagenumber={$pagecount-1}
{/capture}
<li><a href="{$pageurl|regex_replace:'/pagenumber=(\d+)/':$pagestr}">{$pagecount-1}</a></li>

{capture assign=pagestr}
pagenumber={$pagecount}
{/capture}
<li><a href="{$pageurl|regex_replace:'/pagenumber=(\d+)/':$pagestr}">{$pagecount}</a></li>
{else}
{section name=foo start=$pagenumber+1 loop=$pagecount+1 step=1}
{capture assign=pagestr}pagenumber={$smarty.section.foo.index}{/capture}
{capture assign=pageurl1}{$pageurl|regex_replace:'/pagenumber=(\d+)/':$pagestr}{/capture}
{if  $smarty.section.foo.index eq $pagenumber}
<li><span class="selected">{$smarty.section.foo.index}</span></li>
{else}
<li><a class="selected" href="{$pageurl1}">{$smarty.section.foo.index}</a></li>
{/if}
{/section}
{/if}
{if $pagenumber < $pagecount}
<li><a href="{$nexturl}">Next</a></li>
{/if}
</ul>
{/if}
{*pagination end*}

And here the result:

Code: Select all

<ul style="clear:both;" id="pagenav">
<li><span class="selected">1</span></li>
<li><a href="http://www.mysite-1-11.ubuntu-12-04.vmware/45/Listing/2/page.html" rel="next 
">2</a></li>
<li class="continue">...</li>
<li><a href="http://www.mysite-1-11.ubuntu-12-04.vmware/45/Listing/2/page.html" rel="next 
">3</a></li>

<li><a href="http://www.mysite-1-11.ubuntu-12-04.vmware/45/Listing/2/page.html" rel="next 
">4</a></li>
<li><a href="http://www.mysite-1-11.ubuntu-12-04.vmware/45/Listing/2/page.html">Next</a></li>
</ul>
Locked

Return to “Tips and Tricks”