codeigniter 看一看 (2) 翻页控件改编版

由于有一个翻页的需求,原来做的翻页比较垃圾,所以就改装下Pagination了。 目前完成的,没有留出数据库的交互的参数,基于在很少改动原来程序的情况下,实现翻页,代码如下:
  1. <?php   
  2. /**   
  3.  * Pagination Class  
  4.  *    
  5.  * @category    Pagination   
  6.  * @author        Martin   
  7.  * @link        This class is based on the file from codeigniter   
  8.  */   
  9. class Pagination {   
  10.   
  11.     var $base_url            = ''; // The page we are linking to       
  12.     var $total_rows          = ''; // Total number of items (database results)   
  13.     var $per_page             = 3; // Max number of items you want shown per page   
  14.     var $num_links            =  4; // Number of "digit" links to show before/after the currently viewed page   
  15.     var $cur_page             =  0; // The current page being viewed   
  16.     var $first_link           = '&lsaquo; First';   
  17.     var $next_link            = '&gt;';   
  18.     var $prev_link            = '&lt;';   
  19.     var $last_link            = 'Last &rsaquo;';       
  20.     var $full_tag_open        = '';   
  21.     var $full_tag_close        = '';   
  22.     var $first_tag_open        = '';   
  23.     var $first_tag_close    = '&nbsp;';   
  24.     var $last_tag_open        = '&nbsp;';   
  25.     var $last_tag_close        = '';   
  26.     var $cur_tag_open        = '&nbsp;<b>';   
  27.     var $cur_tag_close        = '</b>';   
  28.     var $next_tag_open        = '&nbsp;';   
  29.     var $next_tag_close        = '&nbsp;';   
  30.     var $prev_tag_open        = '&nbsp;';   
  31.     var $prev_tag_close        = '';   
  32.     var $num_tag_open        = '&nbsp;';   
  33.     var $num_tag_close        = '';       
  34.     var $url_params          = null;   
  35.   
  36.     /**   
  37.      * Constructor   
  38.      *   
  39.      * @access    public   
  40.      * @param    array    initialization parameters   
  41.      */   
  42.     function Pagination($params = array())   
  43.     {   
  44.         if (count($params) > 0)   
  45.         {   
  46.             $this->initialize($params);           
  47.         }           
  48.     }   
  49.        
  50.     // --------------------------------------------------------------------   
  51.        
  52.     /**   
  53.      * Initialize Preferences   
  54.      *   
  55.      * @access    public   
  56.      * @param    array    initialization parameters   
  57.      * @return    void   
  58.      */   
  59.     function initialize($params = array())   
  60.     {   
  61.         if (count($params) > 0)   
  62.         {   
  63.             foreach ($params as $key => $val)   
  64.             {   
  65.                 if (isset($this->$key))   
  66.                 {   
  67.                     $this->$key = $val;   
  68.                 }   
  69.             }           
  70.         }   
  71.         $this->url_params = $_GET;   
  72.     }   
  73.        
  74.     // --------------------------------------------------------------------   
  75.        
  76.     /**   
  77.      *    
  78.      *        
  79.      * @param    array    initialization parameters   
  80.      * @return    string    
  81.      */   
  82.     function getUrl()   
  83.     {   
  84.         $linkUrl =     $this->base_url . "?";   
  85.         foreach ($this->url_params as $key => $value)   
  86.         {   
  87.             $linkUrl.= $key . "=".$value."&";               
  88.         }   
  89.         return $linkUrl;   
  90.     }   
  91.        
  92.     function setCurrentPage($currentpage)   
  93.     {           
  94.            
  95.         $this->url_params['cp'] = $currentpage;   
  96.            
  97.     }   
  98.        
  99.     function setPerPage($perpage)   
  100.     {   
  101.         $this->url_params['pp'] = $currentpage;   
  102.     }   
  103.        
  104.     // --------------------------------------------------------------------   
  105.        
  106.     /**   
  107.      * Generate the pagination links   
  108.      *   
  109.      * @access    public   
  110.      * @return    string   
  111.      */       
  112.     function create_links()   
  113.     {   
  114.         if( !$this->url_params['pp'] )   
  115.         {   
  116.             $this->setPerPage($this->per_page);   
  117.         }   
  118.         // If our item count or per-page total is zero there is no need to continue.   
  119.         if ($this->total_rows == 0 OR $this->per_page == 0)   
  120.         {   
  121.            return '';   
  122.         }   
  123.   
  124.         // Calculate the total number of pages   
  125.         $num_pages = ceil($this->total_rows / $this->per_page);   
  126.   
  127.         // Is there only one page? Hm... nothing more to do here then.   
  128.         if ($num_pages == 1)   
  129.         {   
  130.             return '';   
  131.         }           
  132.            
  133.         if ( ! is_numeric($this->cur_page))   
  134.         {   
  135.             $this->cur_page = 0;   
  136.         }           
  137.         // Is the page number beyond the result range?   
  138.         // If so we show the last page   
  139.         if ($this->cur_page > $this->total_rows)   
  140.         {   
  141.             $this->cur_page = ($num_pages - 1) * $this->per_page;   
  142.         }   
  143.            
  144.         $uri_page_number = $this->cur_page;   
  145.         $this->cur_page = floor(($this->cur_page/$this->per_page) + 1);   
  146.   
  147.         // Calculate the start and end numbers. These determine   
  148.         // which number to start and end the digit links with   
  149.         $start = (($this->cur_page - $this->num_links) > 0) ? $this->cur_page - ($this->num_links - 1) : 1;   
  150.         $end   = (($this->cur_page + $this->num_links) < $num_pages) ? $this->cur_page + $this->num_links : $num_pages;   
  151.   
  152.         // Add a trailing slash to the base URL if needed   
  153.         //$this->base_url = preg_replace("/(.+?)/*$/""\1/",  $this->base_url);   
  154.         echo $this->base_url;   
  155.            
  156.           // And here we go...   
  157.         $output = '';   
  158.   
  159.         // Render the "First" link   
  160.         if  ($this->cur_page > $this->num_links)   
  161.         {               
  162.             $output .= $this->first_tag_open.'<a href="'.$this->base_url.'">'.$this->first_link.'</a>'.$this->first_tag_close;   
  163.         }   
  164.   
  165.         // Render the "previous" link   
  166.         if  (($this->cur_page - $this->num_links) >= 0)   
  167.         {   
  168.             $i = $uri_page_number - $this->per_page;   
  169.             if ($i == 0) $i = '';   
  170.             $this->setCurrentPage($i);   
  171.             $output .= $this->prev_tag_open.'<a href="'.$this->getUrl().'">'.$this->prev_link.'</a>'.$this->prev_tag_close;   
  172.         }   
  173.   
  174.         // Write the digit links   
  175.         for ($loop = $start -1; $loop <= $end$loop++)   
  176.         {   
  177.             $i = ($loop * $this->per_page) - $this->per_page;   
  178.                        
  179.             if ($i >= 0)   
  180.             {   
  181.                 if ($this->cur_page == $loop)   
  182.                 {   
  183.                     $output .= $this->cur_tag_open.$loop.$this->cur_tag_close; // Current page   
  184.                 }   
  185.                 else  
  186.                 {   
  187.                     $n = ($i == 0) ? '' : $i;   
  188.                     $this->setCurrentPage($n);   
  189.                     $output .= $this->num_tag_open.'<a href="'.$this->getUrl().'">'.$loop.'</a>'.$this->num_tag_close;   
  190.                 }   
  191.             }   
  192.         }   
  193.   
  194.         // Render the "next" link   
  195.         if ($this->cur_page < $num_pages)   
  196.         {   
  197.             $this->setCurrentPage($this->cur_page * $this->per_page);   
  198.             $output .= $this->next_tag_open.'<a href="'.$this->getUrl().'">'.$this->next_link.'</a>'.$this->next_tag_close;   
  199.         }   
  200.   
  201.         // Render the "Last" link   
  202.         if (($this->cur_page + $this->num_links) < $num_pages)   
  203.         {   
  204.             $i = (($num_pages * $this->per_page) - $this->per_page);   
  205.             $this->setCurrentPage($i);   
  206.             $output .= $this->last_tag_open.'<a href="'.$this->getUrl().'">'.$this->last_link.'</a>'.$this->last_tag_close;   
  207.         }   
  208.   
  209.         // Kill double slashes.  Note: Sometimes we can end up with a double slash   
  210.         // in the penultimate link so we'll kill all double slashes.   
  211.         $output = preg_replace("#([^:])//+#""\1/"$output);   
  212.   
  213.         // Add the wrapper HTML if exists   
  214.         $output = $this->full_tag_open.$output.$this->full_tag_close;           
  215.         return $output;           
  216.     }   
  217. }   
  218. // END Pagination Class  
  219. ?>  
例子如下:
  1. $currentPage = $_GET['cp'];   
  2.     $perPage = $_GET['pp'];   
  3.     $query = "SELECT * table";   
  4.     $result = mysql_query($query,$GLOBALS['db']);   
  5.     $hitCount = mysql_num_rows ($result);   
  6.     $offset = '0,';   
  7.     $limit = '3';   
  8.     if($currentPage)   
  9.     {   
  10.         $offset = $currentPage.",";           
  11.     }   
  12.            
  13.     if($perPage)   
  14.     {   
  15.         $limit = $perPage;   
  16.     }       
  17.        
  18.     $query .= " LIMIT ".$offset.$limit;       
  19.        
  20.        
  21.     //now you can print your content    
  22.     ...   
  23.        
  24.        
  25.     //disaply the pagination hyperlinker   
  26.     $pageTest = new Pagination();      
  27.         $pageTest->initialize( array(   
  28.                                     'base_url'         => 'testPagination.php',   
  29.                                     'total_rows'     => $hitCount,   
  30.                                     'per_page'         => $limit,                               
  31.                                     'cur_page'       => $currentPage,   
  32.                                     'full_tag_open'     => '<p>',                                       
  33.                                     'full_tag_close' => '</p>'   
  34.                                     )   
  35.                                );   
  36.         echo $pageTest->create_links();        
好了,放心使用吧,当然如果你有需要还可以根据需要扩展下数据的绑定,样式等
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值