如何在codeigniter分页中使用where子句

时间:2017-08-02 15:12:20

标签: php codeigniter pagination

当我单击CodeIgniter分页的第2页链接时,它将显示第2页并显示数据,但分页链接会自动删除。你能帮我找到我的错误吗?

我的观点:

public function search()
{

    $this->load->library('pagination');
    $list= $this->input->post('list');
    $category=$this->input->post('category');
    $area=$this->input->post('area');
    $config = array();
    $config["base_url"] = base_url() . "index.php/Main_control/search";
    $this->db->where('subcategory',$category);        
    $config['total_rows'] = $this->db->get('ad_listing')->num_rows();
    $config['use_page_numbers'] = TRUE;
    $config["per_page"] = 3;
    $config["uri_segment"] = 3;

    $this->pagination->initialize($config);
    $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
    $data["links"] = $this->pagination->create_links();


    if( !$data["results"] = $this->Main_model->fetch_countries($category,$config["per_page"], $page))
    {
        echo "error";
    }
    else
    {
        $this->load->view("test",$data);
    } 
}

我的控制器:

public function fetch_countries($category,$limit, $start) {

        $this->db->order_by('rand()');
        $this->db->limit($limit, $start);
        $query = $this->db->get("ad_listing");

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
}

我的模特:

class="mydate"

1 个答案:

答案 0 :(得分:0)

模型中,您必须添加

public function fetch_countries($category,$limit, $start) {

    $this->db->order_by('rand()');
    $this->db->limit($limit, $start);
  

$这 - > DB-化合物其中( '子类别',$类);

    $query = $this->db->get("ad_listing");

    if ($query->num_rows() > 0) {
        foreach ($query->result() as $row) {
            $data[] = $row;
        }
        return $data;
    }
    return false;
}
相关问题