• twitter image
  • facebook image
  • youtube image
  • linkedin image
Language: CMS Made Simple Czech CMS Made Simple France CMS Made Simple Spain CMS Made Simple Hungary CMS Made Simple Russia CMS Made Simple Netherlands

All times are UTC




Post new topic Reply to topic  [ 19 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: HOWTO: Proper news pagination (with page numbers!)
PostPosted: Tue Feb 20, 2007 10:33 am 
Offline
Forum Members
Forum Members

Joined: Sat Feb 17, 2007 1:56 pm
Posts: 45
Location: Melbourne, Australia
I've been looking around for how to do pagination with the News module (for an archive page), but didn't find anything. So, I decided to try it myself, based on bits and pieces I found on this forum :). Since there's not much documentation on this, I though I should post my solution here (I'll post it to the Wiki eventually ;))

Although I'm a bit experienced with Smarty (I used it to power my site), I'm still new to CMS Made Simple, so please tell me if I did anything wrong. I had no idea how to approach this, so I've tried my best ;)

Create a new User Defined Tag called "newsindex" (or similar), and place the following into it:
Code:
global $gCms;

// Current page number
$pagenum = isset($_REQUEST['pagenum']) ? $_REQUEST['pagenum'] : 1;
// Number of items per page
$number = $params['number'];

// Base URL for everything (page number is appended to this)
$base_url = $next_uri = 'index.php?page=' . $gCms->variables['page_name'] . '&pagenum=';
// Various URL's
$next_uri = $base_url . ($pagenum + 1);
$prev_uri = $base_url . ($pagenum - 1);

// Get the page count
$db =& $gCms->db;
$query = "SELECT COUNT(news_id) AS count FROM " . cms_db_prefix() . "module_news";
$dbresult = $db->Execute($query);
$row = $dbresult->FetchRow();
$numOfArticles = $row['count'];

// Assign some stuff
$smarty->assign('news_start', ($number * ($pagenum - 1)));
$smarty->assign('news_number', $number);
$smarty->assign('news_base_url', $base_url);
$smarty->assign('news_next_url', $next_uri);
$smarty->assign('news_prev_url', $prev_uri);
$smarty->assign('news_num_articles', $numOfArticles);
$smarty->assign('news_pages', ceil($numOfArticles / $number));
$smarty->assign('news_curr_page', $pagenum);


Then, on a Content Page (wherever you want the news archive to appear), place the following:
Code:
{newsindex number='10'}

{cms_module module='news' number=$news_number start=$news_start}
{if $news_curr_page > 1}
   <a href="{$news_prev_url}">Back</a>
{/if}

{section name=pagenum start=0 loop=$news_pages}
   {if $smarty.section.pagenum.index+1 == $news_curr_page}
      <b>[ {$smarty.section.pagenum.index+1} ]</b>
   {else}
      <a href="{$news_base_url}{$smarty.section.pagenum.index+1}">{$smarty.section.pagenum.index+1}</a>
   {/if}
{/section}

{if $news_curr_page < $news_pages}
   <a href="{$news_next_url}">Next</a>
{/if}

Change "newsindex" above, depending on what you called the tag. Also, you may change number="10" to the number of items you'd like to display per page.

The URL's will look a bit like index.php?page=news&pagenum=2. If you're using friendly URL's with a .htm extension, you may change:
Code:
$base_url = $next_uri = 'index.php?page=' . $gCms->variables['page_name'] . '&pagenum=';

to
Code:
$base_url = $gCms->variables['page_name'] . '.htm?pagenum=';

This will make the URL's slightly nicer, but still a bit ugly (news.htm?pagenum=2. With a slight change to the .htaccess file, you'll be able to make the URL's even nicer (something like news-2.htm)... I'll post details on this later :)

Edit: Fixed typo in code

_________________
Daniel15 :)


Last edited by Daniel15 on Tue Feb 20, 2007 10:37 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: HOWTO: Proper news pagination (with page numbers!)
PostPosted: Tue Feb 20, 2007 12:00 pm 
Daniel15 wrote:
So, I decided to try it myself, based on bits and pieces I found on this forum :).


Thanx for that solution ...

Quote:
I though I should post my solution here (I'll post it to the Wiki eventually ;))


Nice idea  8) ...


Last edited by cyberman on Tue Feb 20, 2007 12:07 pm, edited 1 time in total.

Top
  
 
 Post subject: Re: HOWTO: Proper news pagination (with page numbers!)
PostPosted: Tue Feb 20, 2007 4:26 pm 
Daniel15, this is an excellent piece of code. Great work!

I recently posted a topic about pagination of news articles on the backend or from within the admin interface.
http://forum.cmsmadesimple.org/index.php/topic,10199.0.html

I've made reference to your post but I was also hoping that with your expertise, you might be able to also come up with a routine or adapt your code to help for pagination of news articles on the backend.

I look forward to your thoughts on this.

Thanks!
Gazoo


Top
  
 
 Post subject: Re: HOWTO: Proper news pagination (with page numbers!)
PostPosted: Wed Feb 21, 2007 6:01 am 
Offline
Forum Members
Forum Members

Joined: Sat Feb 17, 2007 1:56 pm
Posts: 45
Location: Melbourne, Australia
Quote:
I've made reference to your post but I was also hoping that with your expertise, you might be able to also come up with a routine or adapt your code to help for pagination of news articles on the backend.

I haven't really looked at the CMS Made Simple code in much detail, so I'm not quite sure how to do this. I may have a look at it later, but my free time is quite limited (due to school).

EDIT: Take a look at my reply to that topic ;)

_________________
Daniel15 :)


Last edited by Daniel15 on Wed Feb 21, 2007 6:05 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: HOWTO: Proper news pagination (with page numbers!)
PostPosted: Thu Feb 22, 2007 2:19 am 
Do you have a site where I can see it in action?  Great work!


Top
  
 
 Post subject: Re: HOWTO: Proper news pagination (with page numbers!)
PostPosted: Thu Feb 22, 2007 5:33 am 
Offline
Forum Members
Forum Members

Joined: Sat Feb 17, 2007 1:56 pm
Posts: 45
Location: Melbourne, Australia
While I don't have a site with it in action (as I'm still converting www.dansoftaustralia.net over to CMS Made Simple), I can show you some screenshots :).

First page:
Image

Second page:
Image.

I'll eventually work on month-based pagination (one page for each month), but this will require some coding. I'm aiming to get something similar to what I've got at http://www.dansoftaustralia.net/ (it's flat-file at the moment. I coded it before learning MySQL, so all the news is in one large PHP array :o. The current archive just loops through the array, and checks if the month and year selected equal that of the news article).

_________________
Daniel15 :)


Top
 Profile  
 
 Post subject: Re: HOWTO: Proper news pagination (with page numbers!)
PostPosted: Fri Feb 23, 2007 4:56 am 
Offline
Forum Members
Forum Members

Joined: Mon Jun 12, 2006 6:00 pm
Posts: 227
There is only one big problem with this otherwise ingenious script. The total count doesn't take into account news categories and expiration dates. For instance, I need to display news of the past for members (tagged with the "member" category) on an archives page.

Code:
{newsindex number='10'}

{cms_module module=CustomContent}
{if $customcontent_loggedin}
  {if $customcontent_memberof_member}
      {cms_module module="news" category="general,member" showarchive="true" number=$news_number start=$news_start}
  {else}
      {cms_module module="news" category="general" showarchive="true" number=$news_number start=$news_start}
{/if}

_________________
Mysql Dump - Backup and restore
Postgres Dump - Backup and restore


Top
 Profile  
 
 Post subject: Re: HOWTO: Proper news pagination (with page numbers!)
PostPosted: Fri Feb 23, 2007 6:46 am 
Offline
Forum Members
Forum Members

Joined: Sat Feb 17, 2007 1:56 pm
Posts: 45
Location: Melbourne, Australia
mahjong wrote:
There is only one big problem with this otherwise ingenious script. The total count doesn't take into account news categories and expiration dates. For instance, I need to display news of the past for members (tagged with the "member" category) on an archives page.

Code:
{newsindex number='10'}

{cms_module module=CustomContent}
{if $customcontent_loggedin}
  {if $customcontent_memberof_member}
      {cms_module module="news" category="general,member" showarchive="true" number=$news_number start=$news_start}
  {else}
      {cms_module module="news" category="general" showarchive="true" number=$news_number start=$news_start}
{/if}



Oops, I forgot about that...
Unfortunately, I have almost no experience with CMS Made Simple, and don't really know how to take those factors into account (I don't have the time to look through the code, unfortunately). Anyone else know how to do this?

_________________
Daniel15 :)


Top
 Profile  
 
 Post subject: Re: HOWTO: Proper news pagination (with page numbers!)
PostPosted: Sat Mar 31, 2007 3:34 pm 
Offline
Power Poster
Power Poster

Joined: Sun Jan 07, 2007 8:54 pm
Posts: 267
It's about time someone posted the solution of the count of news depending on a news category and expiration date. I had in mind to figure the problem out earlier, but didn't have time - until now. So, here is my version of the news pagination UDT:
Code:
global $gCms, $number;

$start = ($_REQUEST['start']) ? $_REQUEST['start'] : $params['start'];
$number = ($_REQUEST['number']) ? $_REQUEST['number'] : $params['number'];

function join_uri($middle)
{
  global $gCms, $number;
   return 'index.php?page=' . $gCms->variables['page_name'] . '&start=' . $middle . '&number=' . $number;
}
$next_uri = join_uri($start + $number);
$prev_uri = join_uri($start - $number);

$smarty->assign('news_start', $start);
$smarty->assign('news_number', $number);
$smarty->assign('news_next_url', $next_uri);
$smarty->assign('news_prev_url', $prev_uri);

$db = &$gCms->db;
$tables=cms_db_prefix().'module_news';
$cond='';
if (isset($params['count_expired'])) {
  switch ($params['count_expired']) {
    case 'true': $cmpr='<';
       break;
    case 'false': $cmpr='>';
       break;
  default: return 'Wrong argument in "count_expired"';
  }
   $cond='(end_time '.$cmpr.' NOW() )';
}
if (isset($params['category'])) {
  $tables.=', '.cms_db_prefix().'module_news_categories';
  if ($cond!='') $cond.=" AND ";
  $cond.="(news_category_name
   LIKE '".$params['category']."')
   AND (".cms_db_prefix()."module_news_categories.news_category_id =
   ".cms_db_prefix()."module_news.news_category_id)";
}
if($cond!='') $cond='WHERE ('.$cond.')';
$query = 'SELECT COUNT(news_id) FROM '.$tables.' '.$cond;
$newscount = &$db->GetOne($query);
$smarty->assign('newscount',$newscount);

$crumbcount=Floor($newscount/$number);
unset($newscrumbs);
for ($i=0;$i<=$crumbcount;$i++ ) {
    $newscrumbs.="<a href=\"".join_uri($i*$number)."\">".($i+1)."</a>";
    if ($i<$crumbcount){
      if (isset($params['delimiter'])) {
         $newscrumbs.=$params['delimiter'];
      }
    }
  }
$smarty->assign('newscrumbs',$newscrumbs);

Call the UDT {newsindex} with these parametres:
(needed)start(the starting number of the first news; (first is 0))
(needed)number(the maximum of the news showed per page)
(optional)category
(optional)count_expired='true/false' (if not specified, it will count all the news in the scope)
(optional)delimiter (for instance '|')

Then, further in the page:
{if $news_start > '0'}Previous {/if}
shows the link to the previous news (if any);
{if $news_start + $news_number< $newscount}Next {/if}
the same for the next ones;
{$newscrumbs}
for the news pagination.

Maybe I should rewrite wiki...


Top
 Profile  
 
 Post subject: Re: HOWTO: Proper news pagination (with page numbers!)
PostPosted: Sun Apr 08, 2007 6:27 am 
Offline
Power Poster
Power Poster

Joined: Sun Jan 07, 2007 8:54 pm
Posts: 267
See updated documenation: http://wiki.cmsmadesimple.org/index.php ... fined_Tags


Top
 Profile  
 
 Post subject: Re: HOWTO: Proper news pagination (with page numbers!)
PostPosted: Wed Jun 06, 2007 3:47 am 
Offline
Forum Members
Forum Members

Joined: Tue May 01, 2007 2:52 am
Posts: 27
Thanks Daniel15 and Vin! Very useful code!  ;D


Top
 Profile  
 
 Post subject: Re: HOWTO: Proper news pagination (with page numbers!)
PostPosted: Wed Jun 06, 2007 10:24 am 
Offline
Forum Members
Forum Members

Joined: Tue Aug 22, 2006 5:37 pm
Posts: 80
Location: Zierikzee
Is it possible that this option becomes available in the next News module release?


Top
 Profile  
 
 Post subject: Re: HOWTO: Proper news pagination (with page numbers!)
PostPosted: Wed Jun 06, 2007 10:39 am 
From Blog

Quote:
- News enhancements
- Frontend Pagination for summary articles


Top
  
 
 Post subject: Re: HOWTO: Proper news pagination (with page numbers!)
PostPosted: Thu Jul 05, 2007 3:51 pm 
Offline
Forum Members
Forum Members

Joined: Fri Dec 22, 2006 4:29 pm
Posts: 36
Location: Philadelphia, PA
Unfortunately, this method doesn't work with News 2.3, and I prefer this version to the pagination built into the current news module.

Did anyone out there get this working with News 2.3? I've been trying to figure this out for the past two days with no luck.


Top
 Profile  
 
 Post subject: Re: HOWTO: Proper news pagination (with page numbers!)
PostPosted: Thu Aug 26, 2010 7:11 pm 
Offline
Power Poster
Power Poster
User avatar

Joined: Thu May 14, 2009 8:11 pm
Posts: 1269
Location: Lithuania
Yet another solution to have lots of numbers on page :).

This solution should work with ugly url only.
This method does not depend on any module, but you need to know :
  a. current page number
  b. page count
  c. pagination url parameter (e.g. 'pagenum')

Originally pagination on Products module looks like
Quote:
<< < Page 444 of 1000 > >>
Pagination on template looks like
Code:
{if isset($pagecount) && $pagecount gt 1}

{$firstlink} {$prevlink}  {$pagetext} {$curpage} {$oftext} {$pagecount}  {$nextlink} {$lastlink}

{/if}


We can make it look like this (444 is a current page)
Quote:
1  90  190  290  390  400  410  420  430  440  441  442  443  444 445  446  447  448  458  468  478  488  498  598  698  798  1000
Colors just explains pagination pattern. And here is pagination template
Code:
{if isset($pagecount) && $pagecount gt 1}
   {if $curpage>1}
      {assign var='tmp_link' value=$prevlink}
   {else}
      {assign var='tmp_link' value=$nextlink}
   {/if}

   {pagination link=$tmp_link param='page' max=$pagecount  current=$curpage assign='temp' pattern='4,5,3' extreme='true'}

   
      {foreach from=$temp key='page_nb' item='tmp'}
         {if !empty($tmp)}
            {$page_nb}
         {else}
            {$curpage}
         {/if}
         {' '};
      {/foreach}
   
{/if}
You can see 3 comma separated numbers in pattern. First one corresponds to 4 pages that are next to each other, second - 5 pages that differ by 10 from each other, third - 3 pages that differ by 100 and so on. If parameter extreme is not empty links to the first and the last page will be included if not present.

This is an UDT {pagination} that assigns array of pagination links
Code:
array($page_number => 'http://www ...', ... )
Current page has empty value. Most parameters are from standard pagination.
Code:
/**
* 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;

//debug_display($param);
   if(strpos($link, $param.'=')){
      $smarty = cms_utils::get_smarty();
      $links = array();

      $i_min = $i_max = $current;
      foreach($pattern as $exp=>$count){
         for($i = 1; $i <= $count; $i++){
            $delta = pow(10, $exp);
            if($i_min > ($delta + 1)){
               $i_min -= $delta;
               $links[$i_min] = htmlentities(preg_replace( "/$param=\d+/", $param.'='.$i_min, $link));
            }
            if($i_max < ($max - $delta)){
               $i_max += $delta;
               $links[$i_max] = htmlentities(preg_replace(  "/$param=\d+/", $param.'='.$i_max, $link));
            }
         }
         if ($extreme){
            if($current != 1 && !isset($links[1])){
               $links[1] = htmlentities(preg_replace( "/$param=\d+/", $param.'=1', $link));
            }
            if($current != $max && !isset($links[$max])){
               $links[$max] = htmlentities(preg_replace( "/$param=\d+/", $param.'='.$max, $link));
            }
         }
         $links[$current] = '';
      }
      ksort($links);
      $smarty->assign($assign, $links);
   }
}


EDIT: fixed broken UDT. Removed colors from code and template.

_________________
My best friends are: FAQ: How can I debug my code/site ?...showtemplate=false...module_customhow to create a patch, {process_pagedata}
And Yours ? :)


Last edited by Peciura on Fri Apr 06, 2012 6:24 am, edited 3 times in total.

Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 19 posts ]  Go to page 1, 2  Next

All times are UTC


Who is online

Users browsing this forum: No registered users


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Arvixe - A CMSMS Partner