在CodeIgniter

时间:2016-12-11 17:21:16

标签: php codeigniter

我的控制器:

function category() {
        $config = array();
        $category = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
        $config["base_url"] = base_url() . "forums/category/".$category."/";
        $total_row = $this->forums_model->forums_count_by_category($category);
        $config["total_rows"] = $total_row;
        $config["per_page"] = 2;
        $config['use_page_numbers'] = TRUE;
        $config['num_links'] = $total_row;
        $config['cur_tag_open'] = '<li><a class="page active">';
        $config['cur_tag_close'] = '</a></li>';
        $config['next_link'] = 'Next >>';
        $config['prev_link'] = '<< Prev';
        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li>';
        $config['attributes'] = array('class' => 'page');
        $config['prev_tag_open'] = '<li>';
        $config['prev_tag_close'] = '</li>';
        $config['next_tag_open'] = '<li>';
        $config['next_tag_close'] = '</li>';
        $this->pagination->initialize($config);
        $page = ($this->uri->segment(4)) ? $this->uri->segment(4) : 1;
        if($page == 1) {
            $start = 0;
        } else {
            $start = ($page-1)*$config["per_page"];
        }
        $data["results"] = $this->forums_model->get_all_forums_by_category($config["per_page"], $start, $category);
        $str_links = $this->pagination->create_links();
        $data["links"] = explode('&nbsp;',$str_links );
        $data["category"] = $page;
        $random["results"] = $this->forums_model->get_random_forums("3");
        $this->load->view('header');
        $this->load->view('forums/category', $data);
        $this->load->view('sidebar');
        $this->load->view('footer');
    }

我的模特:

public function get_all_forums_by_category($limit, $start, $category_id) {
        $this->db->select("forums.id,forums.name,forums.description,forums.category_id,forums.user_id,forums.url,users.username");
        $this->db->from("forums");
        $this->db->where("category_id", $category_id);
        $this->db->join('users', 'users.id = forums.user_id');
        $this->db->order_by("forums.rating", "desc"); 
        $this->db->limit($limit, $start);
        $query = $this->db->get();
        foreach ($query->result() as $row) {
            $data[] = $row;
        }        
        return $data;
    }

    public function forums_count_by_category($category_id) {
        $query = $this->db->where('category_id', $category_id)->get('forums');
        return $query->num_rows();
    }

当打开页面site.com/forums/category/1/1时,所有内容都正确显示,但是如果您删除了页码:site.com/forums/category/1/(或site.com/forums/category / 1)显示最后一页,而不是第一页。尝试转到第一页(其链接是相同的site.com/forums/category/1)时显示相同的最后一页。

1 个答案:

答案 0 :(得分:0)

public function get_all_forums_by_category($limit, $start, $category_id) {
        $this->db->select("forums.id,forums.name,forums.description,forums.category_id,forums.user_id,forums.url,users.username");
        $this->db->from("forums");
        $this->db->where("category_id", $category_id);
        $this->db->join('users', 'users.id = forums.user_id');
        $this->db->order_by("forums.rating", "desc"); 

这是错误。

$this->db->limit($limit, $start);
...
            $query = $this->db->get();
            foreach ($query->result() as $row) {
                $data[] = $row;
            }        
            return $data;
        }
    ...

限制设置时出错。 你必须写下这个。

$this->db->limit($start, $limit)