【问题标题】:Codeigniter pagination doesn't show page relevant data, but the whole data setCodeigniter 分页不显示页面相关数据,而是显示整个数据集
【发布时间】:2017-10-29 18:36:23
【问题描述】:

这是我的控制器:

function index()
{
    $data['title']="Post";
    $this->load->library('pagination');
    $config['base_url'] = base_url().'Post/index';
    $config['total_rows'] = $this->post_model->get_total_count();
    $config['per_page'] = 1;
    $config['uri_segment'] = 1;
    $this->pagination->initialize($config);
    $data['posts'] = $this->post_model->get_single_post($config['per_page'],$this->uri->segment(1));

    $this->load->view('frontend/layouts/header',$data);
    $this->load->view('frontend/view_post',$data);
    $this->load->view('frontend/layouts/footer');
}

型号

function get_single_post(){
$query= $this->db->select('*')
    ->from('tbl_post,tbl_users')
    ->where('tbl_post.post_created=tbl_users.user_id')
    ->where('tbl_post.post_status',1)
    ->order_by('tbl_post.post_id','asc')
    ->get();
    $data = $query->result_array();
    return $data;
    }



function get_total_count(){
    $this->db->select('*');
    $this->db->from('tbl_post');
    $query = $this->db->get();
    return $query->num_rows();
    }

相关页面数据未显示。它在一页中显示所有数据!

【问题讨论】:

  • 您所说的无法显示每页数据是什么意思。 Cz 它在您的代码中$config['per_page'] = 1;
  • 它显示了所有的数据库数据。但我想显示每页 1 的数据,但它不能
  • 缺少$data['links'] = $this->pagination->create_links(); ??
  • 我显示它查看页面如下:<?php echo $this->pagination->create_links();?>

标签: php codeigniter pagination codeigniter-3


【解决方案1】:

$config['uri_segment'] = 1你确定吗? Check this CI example

正如你提到的$config['base_url'] = base_url().'Post/index'; 问题$config['uri_segment'] = 1 应该是$config['uri_segment'] = 2


也这样做

在控制器中

$config['uri_segment'] = 1;
$this->pagination->initialize($config);

$data['links'] = $this->pagination->create_links();
$data['posts'] = $this->post_model->get_single_post($config['per_page'],$this->uri->segment(1));

可见

echo $links;

编辑 01

在模型中添加这个

function get_single_post($page,$limit) # Changed
{
    $query= $this->db->select('*')
        ->from('tbl_post,tbl_users')
        ->where('tbl_post.post_created=tbl_users.user_id')
        ->where('tbl_post.post_status',1)
        ->limit($page,$limit) # Changed
        ->order_by('tbl_post.post_id','asc')
        ->get();
    $data = $query->result_array();
    return $data;
}

【讨论】:

  • 我在 uri 段中输入了多少。我的基本网址是 base_url().'Post/index' 。我加了 1 但所有页面都显示相同的数据
  • 我添加 uri 段 = 2 ;但我的页面再次显示所有数据。我只显示每个页码:比如 2 个数据。
【解决方案2】:

有很多问题。

  1. $config['uri_segment'] 不能是 1,应该是 3 所以把它改成

    $config['uri_segment']=3;

segment 1 表示控制器名称。 2 表示函数名。 3 表示页码。 如果您使用 1,它将用页码替换控制器名称。

  1. 你将参数传递给你的模型函数,但你的模型没有收到参数。所以你的函数应该是这样的

    function get_single_post($limit,$page_no){

  2. 您的模型返回所有​​记录而不是当前页面记录。所以添加限制

    ->limit($page_no,$limit)//remember page_no should be 0 for 1,1 for 2.

【讨论】:

  • 是的,它解决了。我在 uri 段和模型中出错:限制选项.. 谢谢大家
猜你喜欢
  • 1970-01-01
  • 2018-08-23
  • 2014-07-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-17
  • 2019-02-08
  • 1970-01-01
相关资源
最近更新 更多