codeigniter中的分页不起作用

时间:2015-02-10 10:35:59

标签: php codeigniter pagination

我正在使用codeigniter的分页库,但它对我不起作用.. 这是我的控制器

testimonial.php

<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class testimonial extends CI_Controller {

    public function __construct() {
        parent::__construct();


         $this->load->model('testimonials_model','obj_testimonials',TRUE);
    }
    
    public function index() {
        $row=0;
        $limit='10';
        $this->load->library('pagination');
        $config['base_url'] = site_url() . '/testimonial/index/';
        $config['full_tag_open'] = '<li>';
        $config['full_tag_close'] = '</li>';
        $config['uri_segment'] = 3;
        $config['total_rows'] = $this->obj_testimonials->countrows();
        $config['per_page'] = $limit;
        $this->pagination->initialize($config);
        $data['links'] = $this->pagination->create_links();
        $data['testimonials']=$this->obj_testimonials->get_all_entries($row,$lim=1000,$condition=array(),'testimonials_display_order','');
        $this->load->view('web/testimonials',$data);
    }
}

我的testimonials_model就是这个

<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class testimonials_model extends CI_Model {

    var $table = 'testimonials';

    public function __construct() {
        parent::__construct();
        $this->load->database();
    }

    

    function countrows($condition = array()) {

        $this->db->from($this->table);
        if ($condition) {
            $this->db->where($condition);
        }
        $query = $this->db->get();
        $row = $query->num_rows();
        return $row;
    }

    function get_all_entries($row, $limit, $condition = array(), $order_by_fieled, $order_by_type = "asc") {
        //$this->db->where('blog_status','E');
        if ($condition) {
            $this->db->where($condition);
        }
        if ($order_by_fieled) {
            $this->db->order_by($order_by_fieled, $order_by_type);
        }
        $query = $this->db->get($this->table, $limit, $row);

        if ($query->num_rows() > 0) {
            return $query->result_array();
        } else {
            return array();
        }
    }

    

}

?>

我的观看页面就是这个

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <link type="image/png" rel="shortcut icon" href="<?= base_url() ?>images/fav.png"/>
        <link href='http://fonts.googleapis.com/css?family=Open+Sans:300,400,600' rel='stylesheet' type='text/css'>
            <title>Dream Holidays Cochin</title>
            <link href="<?= base_url() ?>css/style.css" rel="stylesheet" type="text/css" />
            <link rel="stylesheet" href="<?= base_url() ?>fonts/font.css">
                <link rel="stylesheet" type="text/css" href="<?= base_url() ?>css/style_common.css" />
                <link rel="stylesheet" type="text/css" href="<?= base_url() ?>css/style5.css" />
                <link rel="stylesheet" type="text/css" href="<?= base_url() ?>css/jquery.fancybox.css?v=2.1.5" media="screen" />
                <script type="text/javascript" src="<?= base_url() ?>js/jquery-1.10.1.min.js"></script>
                <script type="text/javascript" src="<?= base_url() ?>js/jquery.fancybox.js?v=2.1.5"></script>
                <script type="text/javascript">
                    $(document).ready(function () {

                        $('.fancybox').fancybox();


                    });
                </script>
                </head>
                <body>
<?php include('includes/header.php'); ?>
                    <div id="wrapper">
                        <!--banner starts here-->
                        <div class="sub-banner-wrapper">
                            <div class="sub-banner"> <img src="<?= base_url() ?>images/sub-banner-3.jpg" /> </div>
                            <div class="transperant-layer">
                                <div class="sub-banner-captions">
                                    <h2>Words fuelling us to <span>serve better!</span></h2>
                                     <p>See comments from our esteemed clients that testifies our excellence, professionalism and commitment. </p>
                                </div>
                            </div>
                        </div>
                    </div>
                    <!--banner ends here-->
                    <div class="clear"></div>
                    <div class="contentarea-main">
                        <div class="content-area-inner">
                            <div class="destination-main">
                                <!--destinations introduction starts here-->
                                <div class="destination-introduction">
                                    <div class="destination-heading">
                                        <h1>Our <span> Testimonials </span></h1>
                                    </div>
                                    <div class="clear"></div>
                                </div>
                                <div class="testimonials-page-main">
                                    <ul>
                                        <?php echo $links;?>
                                    </ul>   
                                    <div class="testimonials-box-area">
                                        <?php $i=0;foreach($testimonials as $test){?>
                                        <div class="<?php if($i%2 == 0){echo 'testi-client';}else{echo 'testi-client no-right-margin';}?>">
                                            <?php if(!empty($test['testimonials_image'])){?>
                                            <div class="testi-blue-bg">
                                                <div class="client-round"><img src="<?= base_url()?>uploads/testimonials/thumb/<?= $test['testimonials_image']?>" /></div>
                                            </div>
                                            <div class="client-note">
                                                <p><?= word_limiter($test['testimonials_content'],62);?></p>
                                                <p><span><?= $test['testimonials_name'];?>,</span> <?= $test['testimonials_title'];?></p>
                                            </div>
                                            <?php }else{?>
                                            <div class="client-note">
                                                <p style="width: 440px;"><?= word_limiter($test['testimonials_content'],62);?></p>
                                                <p><span><?= $test['testimonials_name'];?>,</span> <?= $test['testimonials_title'];?></p>
                                            </div>
                                            <?php }?>
                                        </div>
                                            
                                        <?php $i++;}?>
                                    </div>
                                </div>
                            </div>
                            <div class="clear"></div>
                            <div class="clear"></div>
                            <div class="clear"></div>
                        </div>
                    </div>
<?php include('includes/footer.php'); ?>
                    <div class="clear"></div>
                    </div>
                </body>
                </html>

当我运行此代码时,它会显示所有前10个推荐,但是当我点击分页链接时它不起作用并在下一个分页部分中提供相同的内容(前10个推荐)本身。它计算数据的数量和所有来自数据库的数据,但只有前10个显示在所有分页链接中。

如果有人能帮助我,那意味着它会非常有帮助。

谢谢..

3 个答案:

答案 0 :(得分:0)

分页库将帮助您创建分页html小部件。根据指定页面(在URL中)从数据库中检索正确的数据仍然是开发人员的任务。

首先,您需要从请求网址中检索页码。 然后你需要在下一行修复参数:

$data['testimonials']=$this->obj_testimonials->get_all_entries($row,$lim=1000,$condition=array(),'testimonials_display_order','');

答案 1 :(得分:0)

您已设置index()具有您的分页的base_url。 这意味着,您的分页的每个链接都将如下所示:

的.com / index.php的/的 controllername / PAGE_NUMBER

这不起作用,因为CI会在你的控制器 controllername 中寻找一个不存在的函数 page_number()

此外,在index()的开头你重置$ row = 0; $限制= '10' ;所以查询总是一样的。

你能做的是:

1 /在控制器中设置新功能。我们将其命名为page()

public function page($page_number) 
{
    $this->load->library('pagination');
    $pagination_config = $this->get_pagination_config();
    $row= $page_number * $per_page - $per_page;
    $limit = $pagination_config["per_page"];

    $this->pagination->initialize($config);
    $data['links'] = $this->pagination->create_links();
    $data['testimonials']=$this->obj_testimonials->get_all_entries($row,$lim=10,$condition=array(),'testimonials_display_order','');
    $this->load->view('web/testimonials',$data);
}

2 /让我们编写一个构建分页cofig的函数,这样我们就不会多次这样做了

private function getPaginationConfig()
{
    $config['base_url'] = site_url() . '/testimonial/index/page';
    $config['full_tag_open'] = '<li>';
    $config['full_tag_close'] = '</li>';
    $config['uri_segment'] = 3;
    $config['total_rows'] = $this->obj_testimonials->countrows();
    $config['per_page'] = 10;

    return $config;
}

3 /相应地更改索引()

public function index() 
{
        $row=0;
        $limit='10';
        $this->load->library('pagination');
        $config = $this->getPaginationConfig();
        $this->pagination->initialize($config);
        $data['links'] = $this->pagination->create_links();
        $data['testimonials']=$this->obj_testimonials->get_all_entries($row,$lim=1000,$condition=array(),'testimonials_display_order','');
        $this->load->view('web/testimonials',$data);
}

现在您的分页链接将是:

.com / index.php / controllername / page / page_number 。 page()将根据 page_number 参数加载好的条目。

答案 2 :(得分:0)

只需检查网址中的分页细分,然后将其更新为控制器中的$row即可。因此,第一次它将变为0,下次它将以分页限制$limit='10'重置。

// URL: 'http://example.com/testimonial/index/10
$row = ! empty($this->uri->segment(3)) ? $this->uri->segment(3) : 0;

那就是它。