CodeIgniter $ this-> pagination-> create_links();返回空白字符串

时间:2013-07-23 14:18:22

标签: php codeigniter

public function viewconsultant($id1='')
{

    $id1 = $id1 ? $id1 : 1;
    $pagenum=5;
    $start=($id1-1)*$pagenum;
    $users = $this->db->get_where('connections', array('user_id'=>97));
    $pageall = count($users);
    $config['total_rows']=$pageall;
    $config['per_page']=$pagenum;

    $config['num_links']=4;
    $config['base_url']="localhost/~chrisfu/linkedin2/index.php/connections/viewconsultant";
    $config['use_page_numbers']=true;

    $this->load->library('pagination');
    $this->pagination->initialize($config);
    $query = $this->db->get_where('connections', array('user_id'=>97), $pagenum, $start);
    $data['connections'] = $query->result();
    $this->load->view('profile_view',$data);
}

create_links()效果不佳,它只显示一个空白字符串。我尝试了这个localhost/~chrisfu/linkedin2/index.php/connections/viewconsultant/2,它可以跳转到第二页,但不会创建分页链接。请帮助!!

2 个答案:

答案 0 :(得分:3)

问题是你没有创建链接..

$this->load->library('pagination');
$this->pagination->initialize($config);

// add the line below to your code
$data['links'] = $this->pagination->create_links();
// debug
// echo $data['links'];

$query = $this->db->get_where('connections', array('user_id'=>97), $pagenum, $start);
$data['connections'] = $query->result();
$this->load->view('profile_view',$data);

此外,您可以通过执行以下操作来省略方法中的第一行:

public function viewconsultant($id1=1)
{

    // line below no longer necessary
    //$id1 = $id1 ? $id1 : 1;
}

答案 1 :(得分:1)

试试这个:

function viewconsultant($id1=1, $offset = 0){
    $this->load->library('pagination');
    $pagenum=5;
    //$users  = $this->db->where('connections', array('user_id'=>97))->limit('20', $offset);
    $total      = $this->db->where('connections', array('user_id'=>97));
    $pageall    = $total->num_rows();
    //$users = $this->db->get_where('connections', array('user_id'=>97));
    //$start=($id1-1)*$pagenum;
    //$pageall = count($users);
    $config['total_rows']       = $pageall;
    $config['per_page']         = $pagenum;
    $config["uri_segment"]      = 3;
    $config['num_links']        = 4;
    $config['base_url']         = base_url()."/index.php/connections/viewconsultant/";
    $config['use_page_numbers'] = true;
    $this->load->library('pagination');
    $this->pagination->initialize($config);
    $query = $this->db->get_where('connections', array('user_id'=>97), $pagenum, $this->uri->segment(3));
    $data['connections'] = $query->result();
    $data['links']  = $this->pagination->create_links();
    $this->load->view('profile_view',$data);
}

现在,只需在视图文件中回显$links即可获得分页。