如何通过 $this->db->query() 在 codeigniter 中使用分页?

2023-11-30

我正在开发一个项目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");

它正在显示您的记录,但在所有页面上都是相同的。


CodeIgniter 分页入门

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

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

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

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

当我们想要获取 foos 当前页面的结果时,我们有一个页码参数,这就是我们如何知道要获取哪一组 foos 的方式,但首先我们还需要 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 );
}

工作全部在模型中完成

请记住,在模型中,我们需要 foo 的总数、分页链接创建,然后是该页面的 foo。让我们从计算 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());
}

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

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;
}

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

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

在视图中,我们现在能够显示分页链接和 foos:

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'] );
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何通过 $this->db->query() 在 codeigniter 中使用分页? 的相关文章

  • 在 Oracle 中使用触发器记录对表的更改

    我的一门课有一个项目 当我们的两个表发生更改时 我们需要创建一个日志 插入 更新 删除 我们需要使用Oracle触发器和PL SQL 在日志文件中 我们需要记录用户ID 日期时间 IP地址和事件 插入 更新 删除 我知道如何设置触发器 但我
  • SQL 大表中的随机行(使用 where 子句)

    我有一个网站 人们可以在其中对汽车进行投票 向用户展示 4 辆汽车 他 她可以投票选出他们最喜欢的汽车 桌子cars有重要的列 car id int 10 not auto increment so has gaps views int 7
  • 如何防止 CakePHP 中重复表单提交?

    我发现 CakePHP 中的安全组件通过将令牌作为隐藏值添加到表单中来帮助防止 CSRF 我想知道是否有办法防止使用此组件或其他组件 帮助器重复表单提交 在之前的项目中 我使用了保存在会话中的唯一哈希值 该哈希值会在提交时读取并删除 重复提
  • 在 Kohana 中,可以触发 404 错误吗?

    我有一个名为articles 它创建从数据库获取相关数据的文章模型 我想 如果我调用的方法返回false 触发 404 错误 这是我到目前为止所拥有的 articleName this gt uri gt segment articles
  • 如何让 shell_exec 在 IIS 6.0 上运行

    问题 我有一个 PHP 脚本 它使用shell exec运行 pdf 到文本转换器 为了简化问题 我创建了一个简短的脚本 使用shell exec只是回显的输出dir命令 当我在 Apache 服务器上运行它时 一切都按预期运行 当我切换到
  • 我如何知道请求是否来自 flash swf?

    我有一个用 flash 开发的应用程序 我需要访问一些 php 文件 因此 如果访问来自 swf 则 php 文件会返回一些数据 如何判断请求是否来自Flash 无需将 get post 变量传递给 php 可能是用户代理 推荐人 请记住
  • 如何比较两个字符串的大小写和变音符号不敏感?

    我有两根弦 字符串 1 塞巴斯蒂安 字符串 2 塞巴斯蒂安 我想通过忽略 重音 字符来比较这两个字符串 谁能知道这个逻辑吗 提前致谢
  • 使用 HTTP-Basic 身份验证发出 HTTP GET 请求

    我需要为我正在开发的 Flash Player 项目构建一个代理 我只需要使用 HTTP Basic 身份验证向另一个 URL 发出 HTTP GET 请求 并提供来自 PHP 的响应 就好像 PHP 文件是原始源一样 我怎样才能做到这一点
  • 如何读取 XML 文件并从中获取值以在 PHP 编码的 HTML 页面中显示

    我有一个 XML 文件 其中有一些重复的标签 其中包含不同的值 我需要获取这些值并显示在我的网页中 请帮助我得到这个 如果您使用 PHP5 可以查看 SimpleXML 您可以在这里找到介绍教程 http www w3schools com
  • 有什么方法可以在不重新加载的情况下更改标头 URL? [复制]

    这个问题在这里已经有答案了 可能的重复 修改URL而不重新加载页面 https stackoverflow com questions 824349 modify the url without reloading the page 使用新
  • 如何在 Laravel 代码中使用 Artisan 命令?

    如何在我的 php caode Ex 中使用 Artisan 命令行 php artisan version to 您可以从控制器调用 Artisan 命令 如下所示 calling of migrate install Artisan c
  • 使用php将文本文件转换为xml?

    data txt ha15rs 250 home2 gif 2 ha36gs 150 home3 gif 1 ha27se 300 home4 gif 4 ha4678 200 home5 gif 5 我想使用 php 使用 simplex
  • 如何将 yii2 Restful api 中两个表的关系数据显示为 json 格式

    我遇到了将两个表中的数据显示为 JSON 格式并在 yii2 Restful api 上工作的问题 这是我的结构数据库 TABLE volunteer volunteer id int 11 NOT NULL auto increment
  • Yii 查询时对相关模型的限制

    我遇到了极限问题 我正在使用的代码如下 model PostCategory model record model gt with array posts gt array order gt posts createTime DESC li
  • 图像创建从jpeg() PHP

    我正在使用 imagecreatefromjpeg 函数合并两张图片 现在我面临的问题是 当我使用服务器中的图片时 它工作正常 而当我使用其他网站的图片时 它不起作用 例如 当我使用这个 PHP 文件时http coolfbapps in
  • 如何使用 PHP 获取列中的所有值?

    我一直在到处寻找这个问题 但仍然找不到解决方案 如何从 mySQL 列中获取所有值并将它们存储在数组中 例如 表名称 客户 列名称 ID 名称 行数 5 我想获取此表中所有 5 个名称的数组 我该如何去做呢 我正在使用 PHP 我试图 SE
  • 无法与站点通信以检查致命错误

    无法与站点通信以检查致命错误 因此 PHP 更改已恢复 您需要通过其他方式上传 PHP 文件更改 例如使用 SFTP 有什么解决办法 我正在 WordPress 中编辑头文件 遇到这个问题 尝试这个 我有同样的问题并决定调查一下 更改 wp
  • 禁用/启用用户访问/下载,但允许 php 编辑 - 使用 chown 和 chmod

    我想 move uploaded files到某个文件夹 比方说http localhost myproject protected 并且 PHP 应该能够rm mv cp 里面的一切protected 例如 启用 禁用用户对任何文件的访问
  • php56 - CentOS - Remi 仓库

    我刚刚在测试盒上安装了 php 5 6 正常的 cli php 解释器似乎不存在 gt php v bash php command not found gt php56 v PHP 5 6 13 cli built Sep 3 2015
  • 如何显示 PHP 对象

    我有这样的代码 dataRecord1 client gt GetRecord token table filter echo pre print r dataRecord1 echo pre foreach dataRecord1 gt

随机推荐