CI伪静态分页

首先在 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]


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值