用户
 找回密码
 入住 CI 中国社区
搜索
查看: 3513|回复: 9
收起左侧

[已解决] 为什么分页后在view里显示不出首页和尾页呢

[复制链接]
发表于 2012-6-21 16:21:37 | 显示全部楼层 |阅读模式
PHP复制代码
function lists() {
        $this->load->library('pagination');
        $this->load->model('Blog_model');
        $config['base_url'] = base_url() . 'index.php/blog/lists';
        $config['total_rows'] = $this->Blog_model->getBlogs()->num_rows();
        $config['per_page'] = '5';
        $config['first_link'] = '首页';//这两条就是显示不来的
        $config['last_link'] = '尾页';//
        $config['full_tag_open'] = '<code>';
        $config['full_tag_close'] = '</code>';
        $this->pagination->initialize($config);
        $data['result'] = $this->Blog_model->pages($config['per_page'],$this->uri->segment(3));
        $this->load->library('table');
        $this->table->set_heading('标题','发布时间');
        $this->load->view('list',$data);
    }
复制代码
HTML复制代码
<div id="main">
                <table style="border-collapse: collapse;margin-left: 20px;margin-top: 10px">
                <?php foreach($result->result_array() as $row): ?>
                    <tr>
                        <td><?php echo anchor('blog/cont/' . $row['id'],$row['title']); ?></td>
                    </tr>
                <?php endforeach;?>
                </table>      
                <div style="margin-top:330px;margin-left:20px;"><?php echo $this->pagination->create_links(); ?></div>
            </div>
复制代码
 楼主| 发表于 2012-6-21 16:24:35 | 显示全部楼层
显示的就是这样
1.jpg
发表于 2012-6-21 17:43:13 | 显示全部楼层
ci里面分页类问题。
你到第三页的时候,应该就出来了。

评分

参与人数 1威望 +5 收起 理由
pets-queen + 5

查看全部评分

 楼主| 发表于 2012-6-21 17:49:09 | 显示全部楼层
还是显示不出
1.jpg
发表于 2012-6-21 17:54:15 | 显示全部楼层
一直下一页,就没一个成功出来的?
 楼主| 发表于 2012-6-21 17:54:18 | 显示全部楼层
我知道了,如果记录少的话,就不会出来的
发表于 2012-6-21 18:06:35 | 显示全部楼层
嗯 。其实吧,这是ci分页类存在的一个瑕疵问题。
我以前也遇到过一个。
给你一个修改过了的我一直在用的分页类吧。
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* CodeIgniter
*
* An open source application development framework for PHP 5.1.6 or newer
*
* @package                CodeIgniter
* @author                ExpressionEngine Dev Team
* @copyright        Copyright (c) 2008 - 2011, EllisLab, Inc.
* @license                http://codeigniter.com/user_guide/license.html
* @link                http://codeigniter.com
* @since                Version 1.0
* @filesource
*/

// ------------------------------------------------------------------------

/**
* Pagination Class
*
* @package                CodeIgniter
* @subpackage        Libraries
* @category        Pagination
* @author                ExpressionEngine Dev Team
* @link                http://codeigniter.com/user_guide/libraries/pagination.html
*/
class CI_Pagination {

        var $base_url                        = ''; // The page we are linking to
        var $prefix                                = ''; // A custom prefix added to the path.
        var $suffix                                = ''; // A custom suffix added to the path.

        var $total_rows                        =  0; // Total number of items (database results)
        var $per_page                        = 10; // Max number of items you want shown per page
        var $num_links                        =  2; // Number of "digit" links to show before/after the currently viewed page
        var $cur_page                        =  0; // The current page being viewed
        var $use_page_numbers        = FALSE; // Use page number for segment instead of offset
        var $first_link                        = '首页';
        var $next_link                        = '下一页';
        var $prev_link                        = '上一页';
        var $last_link                        = '尾页';
        var $uri_segment                = 3;
        var $full_tag_open                = '';
        var $full_tag_close                = '';
        var $first_tag_open                = '';
        var $first_tag_close        = '&nbsp;';
        var $last_tag_open                = '&nbsp;';
        var $last_tag_close                = '';
        var $first_url                        = ''; // Alternative URL for the First Page.
        var $cur_tag_open                = '&nbsp;<strong>';
        var $cur_tag_close                = '</strong>';
        var $next_tag_open                = '&nbsp;';
        var $next_tag_close                = '&nbsp;';
        var $prev_tag_open                = '&nbsp;';
        var $prev_tag_close                = '';
        var $num_tag_open                = '&nbsp;';
        var $num_tag_close                = '';
        var $page_query_string        = FALSE;
        var $query_string_segment = 'per_page';
        var $display_pages                = TRUE;
        var $anchor_class                = '';

        /**
         * Constructor
         *
         * @access        public
         * @param        array        initialization parameters
         */
        public function __construct($params = array())
        {
                if (count($params) > 0)
                {
                        $this->initialize($params);
                }

                if ($this->anchor_class != '')
                {
                        $this->anchor_class = 'class="'.$this->anchor_class.'" ';
                }

                log_message('debug', "Pagination Class Initialized");
        }

        // --------------------------------------------------------------------

        /**
         * Initialize Preferences
         *
         * @access        public
         * @param        array        initialization parameters
         * @return        void
         */
        function initialize($params = array())
        {
                if (count($params) > 0)
                {
                        foreach ($params as $key => $val)
                        {
                                if (isset($this->$key))
                                {
                                        $this->$key = $val;
                                }
                        }
                }
        }

        // --------------------------------------------------------------------

        /**
         * Generate the pagination links
         *
         * @access        public
         * @return        string
         */
        function create_links()
        {
                // If our item count or per-page total is zero there is no need to continue.
                if ($this->total_rows == 0 OR $this->per_page == 0)
                {
                        return '';
                }

                // Calculate the total number of pages
                $num_pages = ceil($this->total_rows / $this->per_page);

                // Is there only one page? Hm... nothing more to do here then.
                if ($num_pages == 1)
                {
                        return '';
                }

                // Set the base page index for starting page number
                if ($this->use_page_numbers)
                {
                        $base_page = 1;
                }
                else
                {
                        $base_page = 0;
                }

                // Determine the current page number.
                $CI =& get_instance();

                if ($CI->config->item('enable_query_strings') === TRUE OR $this->page_query_string === TRUE)
                {
                        if ($CI->input->get($this->query_string_segment) != $base_page)
                        {
                                $this->cur_page = $CI->input->get($this->query_string_segment);

                                // Prep the current page - no funny business!
                                $this->cur_page = (int) $this->cur_page;
                        }
                }
                else
                {
                        if ($CI->uri->segment($this->uri_segment) != $base_page)
                        {
                                $this->cur_page = $CI->uri->segment($this->uri_segment);

                                // Prep the current page - no funny business!
                                $this->cur_page = (int) $this->cur_page;
                        }
                }
               
                // Set current page to 1 if using page numbers instead of offset
                if ($this->use_page_numbers AND $this->cur_page == 0)
                {
                        $this->cur_page = $base_page;
                }

                $this->num_links = (int)$this->num_links;

                if ($this->num_links < 1)
                {
                        show_error('Your number of links must be a positive number.');
                }

                if ( ! is_numeric($this->cur_page))
                {
                        $this->cur_page = $base_page;
                }

                // Is the page number beyond the result range?
                // If so we show the last page
                if ($this->use_page_numbers)
                {
                        if ($this->cur_page > $num_pages)
                        {
                                $this->cur_page = $num_pages;
                        }
                }
                else
                {
                        if ($this->cur_page > $this->total_rows)
                        {
                                $this->cur_page = ($num_pages - 1) * $this->per_page;
                        }
                }

                $uri_page_number = $this->cur_page;
               
                if ( ! $this->use_page_numbers)
                {
                        $this->cur_page = floor(($this->cur_page/$this->per_page) + 1);
                }

                // Calculate the start and end numbers. These determine
                // which number to start and end the digit links with
                $start = (($this->cur_page - $this->num_links) > 0) ? $this->cur_page - ($this->num_links - 1) : 1;
                $end   = (($this->cur_page + $this->num_links) < $num_pages) ? $this->cur_page + $this->num_links : $num_pages;

                // Is pagination being used over GET or POST?  If get, add a per_page query
                // string. If post, add a trailing slash to the base URL if needed
                if ($CI->config->item('enable_query_strings') === TRUE OR $this->page_query_string === TRUE)
                {
                        $this->base_url = rtrim($this->base_url).'&amp;'.$this->query_string_segment.'=';
                }
                else
                {
                        $this->base_url = rtrim($this->base_url, '/') .'/';
                }

                // And here we go...
                $output = '';

                // Render the "First" link
       
                  if  ($this->cur_page > 1)
                  {
                           $output .= $this->first_tag_open.'<a href="'.$this->base_url.'">'.$this->first_link.'</a>'.$this->first_tag_close;
              }
                        // Render the "First" link 如果是首页,同样显示,但是没有超链接功能
                       
                // Render the "previous" link
                if  ($this->prev_link !== FALSE AND $this->cur_page != 1)
                {
                        if ($this->use_page_numbers)
                        {
                                $i = $uri_page_number - 1;
                        }
                        else
                        {
                                $i = $uri_page_number - $this->per_page;
                        }

                        if ($i == 0 && $this->first_url != '')
                        {
                                $output .= $this->prev_tag_open.'<a '.$this->anchor_class.'href="'.$this->first_url.'">'.$this->prev_link.'</a>'.$this->prev_tag_close;
                        }
                        else
                        {
                                $i = ($i == 0) ? '' : $this->prefix.$i.$this->suffix;
                                $output .= $this->prev_tag_open.'<a '.$this->anchor_class.'href="'.$this->base_url.$i.'">'.$this->prev_link.'</a>'.$this->prev_tag_close;
                        }

                }
                # // Render the "previous" link 注意:这里是新增不是替换原来的啊。如果为第一页的时候,同样显示上一页标签,但是没有超链接
               
                // Render the pages
                if ($this->display_pages !== FALSE)
                {
                        // Write the digit links
                        for ($loop = $start -1; $loop <= $end; $loop++)
                        {
                                if ($this->use_page_numbers)
                                {
                                        $i = $loop;
                                }
                                else
                                {
                                        $i = ($loop * $this->per_page) - $this->per_page;
                                }

                                if ($i >= $base_page)
                                {
                                        if ($this->cur_page == $loop)
                                        {
                                                $output .= $this->cur_tag_open.$loop.$this->cur_tag_close; // Current page
                                        }
                                        else
                                        {
                                                $n = ($i == $base_page) ? '' : $i;

                                                if ($n == '' && $this->first_url != '')
                                                {
                                                        $output .= $this->num_tag_open.'<a '.$this->anchor_class.'href="'.$this->first_url.'">'.$loop.'</a>'.$this->num_tag_close;
                                                }
                                                else
                                                {
                                                        $n = ($n == '') ? '' : $this->prefix.$n.$this->suffix;

                                                        $output .= $this->num_tag_open.'<a '.$this->anchor_class.'href="'.$this->base_url.$n.'">'.$loop.'</a>'.$this->num_tag_close;
                                                }
                                        }
                                }
                        }
                }

                // Render the "next" link
                if ($this->next_link !== FALSE AND $this->cur_page < $num_pages)
                {
                        if ($this->use_page_numbers)
                        {
                                $i = $this->cur_page + 1;
                        }
                        else
                        {
                                $i = ($this->cur_page * $this->per_page);
                        }

                        $output .= $this->next_tag_open.'<a '.$this->anchor_class.'href="'.$this->base_url.$this->prefix.$i.$this->suffix.'">'.$this->next_link.'</a>'.$this->next_tag_close;
                }

                // Render the "Last" link
               
                 if ($this->last_link !== FALSE AND ($this->cur_page < $num_pages))
                {
                    $i = (($num_pages * $this->per_page) - $this->per_page);
                    $output .= $this->last_tag_open.'<a href="'.$this->base_url.$i.'">'.$this->last_link.'</a>'.$this->last_tag_close;
                }
               
                // Render the "Last" link 如果是最后一页,同样显示,但是没有超链接
                 
                $output = preg_replace("#([^:])//+#", "\\1/", $output);

                // Add the wrapper HTML if exists
                $output = $this->full_tag_open.$output.$this->full_tag_close;

                return $output;
        }               
        function setPage( $pNo, $pCount)        {                        if ($pNo==1)                        echo("        首页  上一页");                else {                        echo(" <a href='?page=1'>首页</a>");                        echo(" <a href='?page=" . ($pNo-1) . "'>上一页</a>");                }                                if ($pCount<=6)                        for( $i=1; $i<=$pCount; $i++)                        {                        if ($pNo==$i)                                echo " <font color=red>" . $i . "</font>";                                else                                echo " <a href='?page=" . $i . "'>" . $i . "</a>";                        }                        else                                {                                        if ($pNo<=4){                                        for( $i=1; $i<=5; $i++)                                        {                                                        if ($pNo==$i)                                                        echo " <font color=red>" . $i . "</font>" ;                                                        else                                                        echo " <a href='?page=" . $i . "'>" . $i . "</a>";                                        }                                echo "...<a href='?page=" . $pCount . "'>" . $pCount . "</a>" ;                                                        }                                                        elseif ($pNo>4 && $pNo<=$pCount-4 ){                                                        echo ("<a href='?page=1'>1</a>...");                                                                        for ($i=$pNo-2; $i<=$pNo+2; $i++)                                                                        {                                                                        if ($pNo==$i)                                                                                        echo (" <font color=red>" . $i . "</font>") ;                                                                                        else                                                                                        echo (" <a href='?page=" . $i . "'>" . $i . "</a>");                                                                                        }                                                                                        echo ("...<a href='?page=" . $pCount . "'>" . $pCount . "</a>");                                                                                        }                                                                                        elseif ($pNo>$pCount-4 ){                                                                                        echo ("<a href='?page=1'>1</a>...");                                                                                        for ($i=$pCount-4; $i<=$pCount; $i++)                                                                                        {                                                                                        if ($pNo==$i)                                                                                                echo (" <font color=red>" . $i . "</font>") ;                                                                                                else                                                                                                echo (" <a href='?page=" . $i . "'>" . $i . "</a>");                        }                        }                                }                                        if ($pNo==$pCount)                        echo("        下一页  尾页");                        else{                        echo(" <a href='?page=" . ($pNo+1) . "'>下一页</a>");                        echo(" <a href='?page=" . $pCount . "'>尾页</a>");                        }                                                echo("  转到");                                        echo(" <select name='selPage' onChange=\"location.href='?page=' + this.options[ this.selectedIndex ].value ;\"> ");                                                for ($i=1; $i<=$pCount; $i++)                                        {                                        if  ($i==$pNo)                                        $slt=" selected";                                        else                                        $slt="";                                                echo("<option value='" . $i . "' " . $slt . ">" .  $i  . "</option>");                }        echo("</select>");        echo(" 页        第" .  $pNo . "页/共" . $pCount  . "页 " );        echo ("<input type=button value=确定 onclick=\" var p=document.getElementById('txtPage').value; if( p.search(/^\d{1,3}$/)==-1) return;   location.href='?page=' + p ; \" >" );        }                       
}
// END Pagination Class

/* End of file Pagination.php */
/* Location: ./system/libraries/Pagination.php */

评分

参与人数 1威望 +5 收起 理由
pets-queen + 5 谢谢分享!

查看全部评分

发表于 2012-6-21 19:12:08 | 显示全部楼层
我觉得这不应该算瑕疵,我觉得这是风格问题,老外的分页风格就是这样,只能说不符合国情。
发表于 2012-12-13 11:26:22 | 显示全部楼层
seesee
发表于 2012-12-13 17:59:35 | 显示全部楼层
Hex 发表于 2012-6-21 19:12
我觉得这不应该算瑕疵,我觉得这是风格问题,老外的分页风格就是这样,只能说不符合国情。 ...

是的 CI没借鉴Wordpress的分页就应该知足了

本版积分规则