本文章是一个自己的对CI框架的学习笔记,用一个完整的搜索,查找并且实现分页的程序给大家参考参考。
举个按关键词搜索结果分页的例子,
1.视图HTML
代码如下 | 复制代码 |
|
即表单提交到名叫search的controller和名叫index的方法, 其中包含了一些动态参数,不是纯列表,故相对比较复杂,后面会提到。
代码如下 | 复制代码 |
public function index() { if (empty ( $offset )) { if (! empty ( $keyword )) { $per_page = 10; $this->pagination->initialize ( $config ); |
因为config.php中默认的enable_query_strings是false, 起始位置始终在最后,这样出来的结果类似/index.php/search/index/?s=中国/10,页码取不到,需要将此配置改为false;
3.模型
代码如下 | 复制代码 |
public function find_by_name($name, $per_page=0, $offset = 0, $is_total = false) { if ($is_total) {//总数 $query = $this->db->query ( "select count(id) as cnt from {$this->_book} where book_name like '%{$name}%'" ); if ($query->num_rows () > 0) { $row = $query->row (); $ret = $row->cnt; } }else{//列表 $query = $this->db->query ("select * from {$this->_book} where book_name like '%{$name}%' limit {$offset}, {$per_page}"); $ret = $query->result (); } return $ret; } |

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver CS6
Visual web development tools
