【问题标题】:How to use pagination in codeigniter with $this->db->query()?如何使用 $this->db->query() 在 codeigniter 中使用分页?
【发布时间】:2018-02-19 09:16:14
【问题描述】:

我正在codeigniter 上开发一个项目,我在创建pagination 的记录时遇到了困难。

实际上,当我使用 $this->db->get() 时,没关系,分页工作正常。但是当使用$this->db->query() 进行分页时,分页不起作用,下面是我的controller 中的代码。

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

    $pagination_config['base_url'] = base_url().'welcome/index';
    $pagination_config['per_page'] = 3;
    $pagination_config['num_links'] = 3;
    $pagination_config['total_rows'] = $this->db->get('properties')->num_rows();

    $this->pagination->initialize($pagination_config);

    $q = $this->db->query("SELECT title FROM properties", $pagination_config['per_page']);

    $r = $q->result();

    foreach($r as $p){
        echo $p->title.'<br />';
    }

    echo '<br />';
    echo '<br />';
    echo $this->pagination->create_links();

我还尝试了LIMIT 的查询,如下所示,

$q = $this->db->query("SELECT title FROM properties LIMIT 3");

它显示你的记录,但在所有页面上都是一样的。

【问题讨论】:

  • 你的当前页面在哪里?
  • 当前页面..??
  • 当前页码
  • 默认情况下,它将是在 xampp http://project.local 上运行的第一页,单击分页按钮后,用户将转到某个页码。一开始,它将是http://project.local
  • 您似乎缺少偏移量。看我的回答。

标签: php codeigniter codeigniter-3


【解决方案1】:

CodeIgniter 分页入门

无论您要分页什么,通常都需要一个 URI 段来指定页面,除非您希望 URI 段指定偏移量。在我的示例中,我将假设需要页面。

所以,假设我们要对一些 foos 进行分页,并且我们的数据库包含一个名为 foos 的表。假装我们有很多 foos (65)。在我们的 Foos 控制器中,我们有一些方法:

public function index()
{
    $this->page();
}

public function page( $page = 1 )
{
    // Here we get and show our foos
}

当我们想要获取当前页的 foo 的结果时,我们有一个页码参数,这就是我们知道要获取哪组 foo 的方法,但首先我们还需要我们的 foo 的总数。所以在我们的页面方法中,我们可以调用模型上的一个方法来完成所有的工作:

public function page( $page = 1 )
{
    // Here we get and show our foos
    $this->load->model('foos_model');
    $view_data['foos'] = $this->foos_model->get_foos( $page );
}

所有工作都在模型中完成

请记住,在模型中,我们需要 foos 的总数、分页链接创建以及此页面的 foos。让我们从数 foos 开始:

public function get_foos( $page )
{
    // First count all foos
    $count = $this->db->count_all('foos');
}

接下来,创建分页链接。假设我们希望每页 10 个 foo:

public function get_foos( $page )
{
    // First count all foos
    $count = $this->db->count_all('foos');

    // Create the pagination links
    $this->load->library('pagination');
    $this->load->helper('url');

    $paging_conf = [
        'uri_segment'      => 3,
        'per_page'         => 10,
        'total_rows'       => $count,
        'base_url'         => site_url('foos/page'),
        'first_url'        => site_url('foos'),
        'use_page_numbers' => TRUE
    ];
    $this->pagination->initialize($paging_conf);

    // Create the paging buttons for the view
    $this->load->vars('pagination_links', $this->pagination->create_links());
}

现在是时候获取我们将显示的实际 foo 集了。注意偏移量的计算:

public function get_foos( $page )
{
    // First count all foos
    $count = $this->db->count_all('foos');

    // Create the pagination links
    $this->load->library('pagination');
    $this->load->helper('url');

    $paging_conf = [
        'uri_segment'      => 3,
        'per_page'         => 10,
        'total_rows'       => $count,
        'base_url'         => site_url('foos/page'),
        'first_url'        => site_url('foos'),
        'use_page_numbers' => TRUE
    ];
    $this->pagination->initialize($paging_conf);

    // Create the paging buttons for the view
    $this->load->vars('pagination_links', $this->pagination->create_links());

    // The pagination offset
    $offset = $page * $paging_conf['per_page'] - $paging_conf['per_page'];

    // Get our set of foos
    $query = $this->db->get('foos', $paging_conf['per_page'], $offset);
}

最后,在将 foos 传递回控制器之前确保它们存在:

public function get_foos( $page )
{
    // First count all foos
    $count = $this->db->count_all('foos');

    // Create the pagination links
    $this->load->library('pagination');
    $this->load->helper('url');

    $paging_conf = [
        'uri_segment'      => 3,
        'per_page'         => 10,
        'total_rows'       => $count,
        'base_url'         => site_url('foos/page'),
        'first_url'        => site_url('foos'),
        'use_page_numbers' => TRUE
    ];
    $this->pagination->initialize($paging_conf);

    // Create the paging buttons for the view
    $this->load->vars('pagination_links', $this->pagination->create_links());

    // The pagination offset
    $offset = $page * $paging_conf['per_page'] - $paging_conf['per_page'];

    // Get our set of foos
    $query = $this->db->get('foos', $paging_conf['per_page'], $offset);

    // Make sure we have foos
    if( $query->num_rows() > 0 )
        return $query->result();

    // Else return default
    return NULL;
}

回到我们的控制器,我们可以将 foos 传递给视图:

public function page( $page = 1 )
{
    // Here we get and show our foos
    $this->load->model('foos_model');
    $view_data['foos'] = $this->foos_model->get_foos( $page );

    // Load the view and pass in the foos
    $this->load->view('foos_view', $view_data);
}

显示分页链接和foos

在视图中,我们现在可以显示分页链接和 foo:

echo $pagination_links;

if( ! empty( $foos ) )
{
    foreach( $foos as $foo )
        echo $foo->name . '<br />';
}

结论

使用 CodeIgniter 对事物进行分页非常简单。如果您有疑问或问题,请阅读 CodeIgniter 的文档:https://www.codeigniter.com/userguide3/libraries/pagination.html?highlight=pagination

关于使用 $this->db->query() 与 $this->db->get() 的额外信息

从功能上讲,只要不出错,使用 $this->db->query() 将产生与 $this->db->get() 相同的结果。在我的示例中,它看起来像这样:

// $query = $this->db->get('foos', $paging_conf['per_page'], $offset);

$query = $this->db->query(
    'SELECT *
    FROM foos
    LIMIT ' . $offset . ', ' . $paging_conf['per_page'] );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-08
    • 2016-07-14
    • 2013-05-31
    • 1970-01-01
    • 2017-04-10
    • 2016-12-29
    • 1970-01-01
    • 2011-12-15
    相关资源
    最近更新 更多