这是一个非常恼人的问题。我已经设置了我的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细分是正确的。如果他们不正确,那么我的页面就不会出现了。
我已经尝试了所有正常的解决方案,但没有任何效果。这就是为什么我在这里问,为什么我在这个问题上放了一笔赏金。
答案 0 :(得分: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'])),
答案 1 :(得分:1)
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 = ' <strong>';
var $cur_tag_close = '</strong>';
var $next_tag_open = ' ';
var $next_tag_close = ' ';
var $prev_tag_open = ' ';
var $prev_tag_close = '';
var $num_tag_open = ' ';
var $num_tag_close = '';
var $page_query_string = FALSE;
var $query_string_segment = 'per_page';
var $display_pages = TRUE;
var $anchor_class = '';
答案 2 :(得分: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();
}
答案 3 :(得分:0)
我最近对ci分页这么努力。
我认为,你的代码是正确的。
第二页上究竟是什么字符串?这个列表函数是index()?
答案 4 :(得分: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);
}
答案 5 :(得分: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');
答案 6 :(得分:0)
我只是想把它扔进去并希望它在某种程度上有所帮助,因为我已经测试过,它对我有用。我做了以下测试假设:
我的测试文件如下:
控制器 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);
}
BTW,你真的想在定义分页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(),以及用户数据的注释。所以我很担心为什么你会遇到问题。
答案 7 :(得分:0)
就你的整体分页功能而言似乎是正确的。我想要检查的是每个函数返回的sql查询和传递给那些函数的参数coz $ author 变量必须全局并尝试
$this->db->last_query();
在你的两个函数my_count()和pullMyStories($ limit,$ start)上并检查那些返回正确结果的函数。
U也可以尝试使用更改参数来编写直接sql查询,如。
$sql = "SELECT * FROM some_table WHERE author = ? LIMIT ?, ?";
$this->db->query($sql, array($author, $start, $limit));
正如我所看到的,这主要是查询可能是错误的。 希望这可以帮助。