我在Code Igniter中比较新,我第一次尝试使用Code Igniter分页类。我正在尝试在我的数据库表中获取一些行,具体取决于用户选择的状态,lga和类别。这三个作为参数传递给函数。我已经按照我在SO和其他一些网站上找到的流程来实现页面上的分页,它似乎有效,除了它不会返回具有指定参数的所有行。
这就是我所拥有的:
模型 - Posts_model.php
public function count_lga_cat_posts($state, $lga, $category) {
return $this->db->get_where('lga_posts', array('state' => $state, 'lga' => $lga, 'category' => $category, 'display' => 'true'))->num_rows();
}
public function fetch_lga_cat_data($limit, $start) {
$this->db->limit($limit, $start);
$this->db->order_by("id", "desc");
$query = $this->db->get("lga_posts");
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
控制器 - Lga_posts.php
public function selected_state_lga_posts_category($state, $lga, $category)
{
//config for pagination
$config = array();
$uri_seg = 5;
$config["base_url"] = base_url('state2/'.$state.'/'.$lga.'/'.$category); //as specified in routes
$config["total_rows"] = $this->posts_model->count_lga_cat_posts($state, $lga, $category);
$config["per_page"] = 3;
$config["uri_segment"] = $uri_seg;
$config['use_page_numbers'] = TRUE;
$config['cur_tag_open'] = ' <a class="current">';
$config['cur_tag_close'] = '</a>';
$config['first_link'] = 'Back to first page';
$config['next_link'] = 'View more posts';
$config['prev_link'] = 'Previous posts';
$config['last_link'] = 'Jump to last page';
$config['display_pages'] = FALSE;
$this->pagination->initialize($config);
$page = ($this->uri->segment($uri_seg))? $this->uri->segment($uri_seg) : 0;
$this->db->where(array('state' => $state, 'lga' => $lga, 'category' => $category, 'display' => 'true'));
$data["results"] = $this->posts_model->fetch_lga_cat_data($config["per_page"], $page);
$str_links = $this->pagination->create_links();
$data["links"] = explode(' ',$str_links );
$data['state'] = $state;
$data['lga'] = $lga;
$data['category'] = $category;
$this->load->view('posts/lga/selected_state_lga_posts_category', $data);
当我加载此页面时,一切似乎都正常工作,分页链接按预期工作,但表中的某些行不会被拉,即使它们具有与拉出和显示的参数相同的参数。如果我将per_page配置更改为1,则最后一页会给出错误“foreach中的无效参数”或类似内容。我想知道它是否与我的查询有关,因为我不想获取表中的所有行,而是那些具有所选参数的行,即state,lga和category。
有人可以指出我做错了吗?