分页在codeigniter中不起作用

时间:2016-08-23 06:02:32

标签: php codeigniter pagination

我在CodeIgniter工作。我想为动态记录创建分页。所以我写了如下代码:

    $config = array();
    $config["base_url"] = base_url() . "employer/search_user";

    $config["per_page"] = 3;
    $config['use_page_numbers'] = TRUE;

    $config['cur_tag_open'] = '&nbsp;<a class="current">';
    $config['cur_tag_close'] = '</a>';
    $config['next_link'] = 'Next';
    $config['prev_link'] = 'Previous';


    if($this->uri->segment(3)){
        $page = ($this->uri->segment(3)) ;
    }
    else{
           $page = 1;
    }

    $query = "select sn.*,u.first_name,u.last_name,u.email,u.phone,group_concat(DISTINCT s.skill) as sk,group_concat(DISTINCT c.name) as ct from 
                users as u
                left join snapshot as sn on sn.user_id = u.user_id
                left join industry as ind on ind.ind_id = sn.industry
                left join city c ON(FIND_IN_SET(c.city_id, sn.current_location) > 0)
                left join skill s ON(FIND_IN_SET(s.skill_id, sn.skill) > 0)
                where ".$where." u.group_id = 3 group by u.user_id limit ".$config["per_page"];
    $data['users'] = $this->admin_model->query($query);

    $row = count($data['users']);
    $config["total_rows"] = $row;
    $config['num_links'] = $row;
    $this->pagination->initialize($config);
    $data["links"] = $this->pagination->create_links();

    $data['main_content']="employer/search_user";
    $this->load->view('employer/template', $data);

在这里,我在页面中有3条记录但是没有显示分页链接。我在构造函数中包含了分页库。我没有在$data["links"]这个变量中进行调查。

那么我必须更正我的代码?我的代码出了什么问题?

2 个答案:

答案 0 :(得分:1)

您需要在查询中使用LIMITOFFSET。例如

$config['per_page'] = 3;
$config['uri_segment'] = 4; // This is the current page

$offset = 0;
if ($this->uri->segment($config['uri_segment']) != "") {
    $offset = $this->uri->segment($config['uri_segment']);
}

$limit = $config['per_page'];

$sql = "SELECT `field_name` FROM `table_name` WHERE `table_name`.field_name = ?";

if($limit != "") {
    $sql .= "LIMIT $limit ";
}

if($offset != "") {
    $sql .= "OFFSET $offset";
}

$result = $this->db->query($sql, array($field_data));

希望这有帮助

答案 1 :(得分:0)

对于$ config [&#34; total_rows&#34;] = $ row; 您只获得3.因为您在查询中设置了限制。 请写一个没有限制的总行的另一个查询。我认为这将解决你的问题。

以下是我的分页代码

renderImage()