5

这是一个非常令人恼火的问题。我已经设置了我的 codeigniter 分页,所以我认为可以工作,但仔细观察,似乎在最后一页上它正在拉入以前的结果来填充页面。

所以说我想要每页十个并有十四个结果。第一页有十个结果,第二页也是如此。什么时候应该是第一个有十个,第二个有四个。如果它只是重​​复一个结果会很好,但是必须滚动浏览之前的六个结果很烦人。任何帮助将非常感激。

在我的控制器中,我有分页代码

$config = array();
$config["base_url"] = base_url()."myStories/".$id;
$config["total_rows"] = $this->data_model->my_count();
$config["per_page"] = 10;
$config["uri_segment"] = 3;
$config['num_links'] = 2;
$choice = $config["total_rows"] / $config["per_page"];
//$config["num_links"] = round($choice);

$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;

$this->load->view('userStory_view', array(
    'query' => $this->data_model->pullMyStories($config['per_page'], $page),
    'links' => $this->pagination->create_links(),
    'user' => $this->users_model->getUser($this->user->user_id),
)); 

然后在我的模型中我有计数,然后返回实际结果

public function my_count() {
        //This counts all the stories that belong to that author
        $author = $this->uri->segment(2);
        $this->db->where('author', $author);
        $this->db->where(array('approved !=' => 'd'));
        $query = $this->db->get('story_tbl');
        return $query->num_rows();
       }

    public function pullMyStories($limit, $start){
        //This pulls back all the stories that belong to that author
        $this->db->limit($limit, $start);
        $this->db->order_by("date", "desc");
        $author = $this->uri->segment(2);
        $this->db->where(array('approved !=' => 'd'));
        $this->db->where('author', $author);
        $story = $this->db->get('story_tbl');
        return $story->result();    
    }

我设置的路线确实有效

$route['myStories/(:any)'] = "story/viewStories/$1";

我最初认为我的计数是错误的,但即使计数为 14,也会返回 20 个结果。

有关更多信息,我非常肯定我的 baseUrl 是正确的。我已经修改了我的 .htaccess 以摆脱 index.php 并编辑了我的路由文件以使控制器从 url 中消失。尝试让用户更容易记住。

我也很确定 uri 段是正确的。如果它们不正确,那么我的页面根本不会出现。

我已经尝试了所有正常的解决方案,但没有任何效果。这就是为什么我在这里问以及为什么我在这个问题上悬赏。

4

8 回答 8

2
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         = 'First';
var $next_link          = '->';
var $prev_link          = '<-';
var $last_link          = 'Last';
var $uri_segment        = 2;
var $full_tag_open      = '';
var $full_tag_close     = '';
var $first_tag_open     = '';
var $first_tag_close    = ' ';
var $last_tag_open      = ' ';
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       = '';
于 2013-07-22T17:29:21.023 回答
1

您的问题是您将错误的参数传递给您的pullMyStories方法。在第一页上,您将对查询应用以下限制

LIMIT 0,10

然后在第二页

LIMIT 1,10

然后在第三

LIMIT 2,10

因此,您的分页一次只能向前移动一项,而不是十项。 所以你需要改变这个

'query' => $this->data_model->pullMyStories($config['per_page'], $page),

对此

'query' => $this->data_model->pullMyStories($config['per_page'], ($page * $config['per_page'])),
于 2013-07-19T16:15:54.980 回答
0

试试它是否适合你:

$config = array();
$config["base_url"]     = base_url()."myStories/";         #change in base url
$config["total_rows"]   = $this->data_model->my_count();
$config["per_page"]     = 10;
$config["uri_segment"]  = 3;
$config['num_links']    = 2;
#$choice = $config["total_rows"] / $config["per_page"];
//$config["num_links"] = round($choice);

$this->pagination->initialize($config);
#$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;  #no need of calculation here

$this->load->view('userStory_view', array(
    'query' => $this->data_model->pullMyStories($this->uri->segment(3)),   #change here send the offset directly
    'links' => $this->pagination->create_links(),
    'user' => $this->users_model->getUser($this->user->user_id),
)); 

function pullMyStories($offset = 0){
    $story = $this->db->where('author', $this->uri->segment(2))->where('approved != d')->order_by("date", "desc")->get('story_tbl', 10, $offset);
    return $story->result();
}
于 2013-07-16T06:26:08.993 回答
0

我最近非常努力地尝试了 ci 分页。

我想,你的代码是对的。

第二页上的 uri 字符串到底是什么?.. 这个列表函数是 index() 吗?

于 2013-07-19T07:15:54.860 回答
0

我只是要把它扔进去,希望它在某种程度上有所帮助,因为我已经测试过了,它对我有用。我为测试做了以下假设:

  • id(如控制器中的“myStories/”.$id)取自$this->uri->segment(2)
  • 我创建的story_tbl字段是id、作者、日期

我的测试文件如下:

控制器MyStories.php

    public function index()
    {
            //loading library, model, and assumptions
        $this->load->library('pagination');
        $this->load->model('m_page', 'data_model');
        $id = $this->uri->segment(2);
        //
        //
        //Your code from here...
        //
        //
        $config = array();
        $config["base_url"] = $this->config->site_url("myStories/".$id);
        $config["total_rows"] = $this->data_model->my_count();
        $config["per_page"] = 3;
        $config["uri_segment"] = 3;
        $config["num_links"] = round($config["total_rows"] / $config["per_page"]);

        $this->pagination->initialize($config);
        $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;

        $data = array();
        $data['query'] = $this->data_model->pullMyStories($config['per_page'], $page);
        $data['links'] = $this->pagination->create_links();
        $data['config'] = $config;
        //$data['user'] = $this->users_model->getUser($this->user->user_id);
        $this->load->view('userStory_view', $data);     
    }

顺便说一句,在定义分页 base_url 时,您真的想使用site_url而不是 base_url。

我还注释掉了控制器中的“用户”数据,只是因为您从未提供任何相关信息。

我的测试视图userStory_view.php

<?php echo $links;?>
<!-- -->
<hr>
total rows: <?php echo $config['total_rows'];?>
<hr>
<?php echo $this->db->last_query();?>
<hr>
<!-- -->
<?php foreach ($query as $row):?>
<?php echo $row->author.'<hr>';?>
<?php endforeach;?>

我没有对您的模型进行任何更改,因此无需在此处显示。

我将以下行添加到routes.php

$route['myStories/(:any)'] = "myStories";

正如我所说,一切都对我有用。除了我的格式,我真正做的唯一改变是使用 site_url() 而不是 base_url(),以及对用户数据的评论。我很害怕你为什么会遇到问题。

于 2013-07-22T03:47:18.550 回答
0

As far as it seems your overall pagination functionality seems to correct. what i want check would the sql query returned in each function and parameters passed to those function coz $author variable has to global and try

$this->db->last_query();

on your both functions my_count() and pullMyStories($limit, $start) and check those function returning right results.

U might also can try writing direct sql query with changing parameters like.

$sql = "SELECT * FROM some_table WHERE author = ? LIMIT ?, ?";

$this->db->query($sql, array($author, $start, $limit)); 

As i am seeing this would be mostly of query might have been wrong . Hope this helps.

于 2013-07-22T07:21:41.417 回答
0

在您的控制器中:

$page = $this->uri->segment(3,0);

然后:

public function pullMyStories($limit, $start){

$author = $this->uri->segment(2);

$this->db
  ->select('*')
  ->where('approved !=', 'd')
  ->where('author', $author)
  ->order_by('date', 'DESC')
  ->limit($limit, $start);

$story = $this->db->get('story_tbl');

return $story->result(); 

另外,您在哪里使用下面的行加载库?

$this->load->library('pagination');   
于 2013-07-21T21:34:22.007 回答
0

尝试这个。您只需要以正确的方式更改 base_url。还要小心并检查您是否获得了正确的 uri_segment 编号。如果没有,您可以更改数字,并获得正确的数字。

    // $config = array();
    // MUST CHNGE IT I just tell the with simple example. If you have index.php, c name and method name.
    $config["base_url"] = base_url()."index.php/controller_name/function_name/"; 
    $config["total_rows"] = $this->data_model->my_count();
    $config["per_page"] = 10;
    $config["uri_segment"] = 3; // BE CARFULL with uri_segment. You need to print it, and be shre that you are getting the right number
    $config['num_links'] = 2;


    $this->pagination->initialize($config);
    $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;

    $this->load->view('userStory_view', array(
        'query' => $this->data_model->pullMyStories($config['per_page'], $page),
        'links' => $this->pagination->create_links(),
        'user' => $this->users_model->getUser($this->user->user_id),
    ));

我更新我的代码,我评论和 $config = array(); 我今天在我的电脑上做了这个,它可以工作。我知道您可能检查过它一百次,但请再次检查详细信息。

更新我的例子:

function index() {

        $data['page_title'] = "Items";

        $config['base_url']         = base_url() .'index.php/items/index/';
        $config['total_rows']       = $this->items_model->count_items(); // Count items from DB
        $config['per_page']         = 10; 
        $config['uri_segment']      = 3;
        // Customize 
        $config['next_link']        = FALSE;
        $config['prev_link']        = FALSE;
        $config['first_link']       = 'first';
        $config['last_link']        = 'last';
        $config['cur_tag_open']     = '<a class="current">';
        $config['cur_tag_close']    = '</a>';

        $this->pagination->initialize($config); 

        $page                       = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
        $data['items']              = $this->items_model->get_items($config["per_page"], $page);


        $this->load->view('invoice-items', $data);

    }
于 2013-07-19T17:00:12.847 回答