php - 让分页正常工作

标签 php mysql codeigniter pagination

我正在开发一个基于 MVC codeigniter 的项目。我当前的设置是,根据以下查询 (../models/mode.php),它在一个页面中最多显示 10 个表

 $function getlist($time) 
   {
  $query = $this->db->query('select * from (select job, username,test from jobs  by    time desc  limit 10 ) as a order by time desc');
 .....
 }

查询工作正常并在页面中显示 10 个表。虽然数据库有超过 10 个条目,但我想在每个页面中放置 10 个表并合并分页,以便我可以在 10 个不同的页面中看到所有 100 个结果。

在 Controller 中,我有一个部分正在尝试使用分页库,但没有任何帮助。

            $this->load->library('pagination');
            $config['base_url']= 'http://localhost/pc/index.php/proj/submit/';
            $config['uri_segment'] =5;
            $config['total_rows']= 100;
            $config['per_page'] = 10;

            $this->pagination->initialize($config);
            echo $this->pagination->create_links();

它所做的只是创建链接,但不允许我遍历不同的页面(当然它会,因为 Controller 不知道它)。我认为 limit 需要有一个开始和结束值,比如 0,100 或开始、结束,但是我怎样才能让 Controller 知道这一点。有任何反馈/建议吗?我是 MVC 世界以及 php/mysql 的新手。

最佳答案

您必须处理模型中的分页:

$function getlist($time, $start, $limit) 
{
  $query = $this->db->query('select * from (select job, username,test from jobs  by    time desc  limit 10 ) as a order by time desc limit '. $limit .', '. $start);
 .....
}

在你的 Controller 中:

// Get pagination page    
$page = ($this->uri->segment(5))? $this->uri->segment(5) : 0;

$data = $this->mode_model->getlist($time, $config["per_page"], $page);

关于php - 让分页正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23558537/

相关文章:

javascript - 获取文本字段中的选择值以及更改时的值

mysql - 导入向导仅部分上传 MySQL 中的 csv 表

mysql - 将 SQL 中的上下文数据的所有行分组为单行

php - 查询以在后端使用以检索最近发送日期小于或等于某个日期的所有联系人

jQuery 和 CodeIgniter AJAX 与 JSON 不起作用

php - Codeigniter 无法在 IIS 中运行

php - Zend Framework 2 - 表单元素装饰器

PHP 如何先按键然后按值对关联数组进行排序?

php - 如何从 php 中的对象/数组中获取基于另一个值的值

php - mysql_real_escape 字符串错误