您的位置:首页 > 其它

CI伪静态分页

2016-04-08 18:17 211 查看
首先在 httpd.conf 开启重写规则

控制器 welcome.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Welcome extends CI_Controller {

/**
* Index Page for this controller.
*
* Maps to the following URL
* 	 http://example.com/index.php/welcome *	- or -
* 	 http://example.com/index.php/welcome/index *	- or -
* Since this controller is set as the default controller in
* config/routes.php, it's displayed at http://example.com/ *
* So any other public methods not prefixed with an underscore will
* map to /index.php/welcome/<method_name>
* @see https://codeigniter.com/user_guide/general/urls.html */
public function index($page=1)
{
//求出总记录数
$count = $this->db->count_all('collect10');
//设置每页条数
$page_num = 3;
//求出总页数
$num = ceil($count/$page_num);
//求出偏移量
$start = ($page-1)*$page_num;
//求出数组
$arr = $this->db->get('collect10',$page_num,$start)->result_array();
//获取分页数据
$last = $page==1?1:$page-1;
$next = $page==$num?$num:$page+1;
$list = "<a href='http://localhost/index_".$last.".html'>上一页</a>";
for($i=1;$i<=$num;$i++){
$list.="<a href='http://localhost/index_".$i.".html'>".$i."</a>";
}
$list.="<a href='http://localhost/index_".$next.".html'>下一页</a>";
$data['arr'] = $arr;
$data['list'] = $list;
$this->load->view('show',$data);
}
}


展示模板 show.php
<table>
<?php foreach ($arr as $k => $v): ?>
<tr>
<?php foreach ($v as $key => $value): ?>
<td>
<?php echo $value ?>
</td>
<?php endforeach ?>
</tr>
<?php endforeach ?>
</table>
<?php echo $list; ?>


.htaccess

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: