|
我做了一个分页,出现的问题是,分页调用的数据正确,但分页的页面导航(页码)显示错误,麻烦大家帮忙看看!
首先model中的代码:
PHP复制代码
//传入3个参数,查询的条件,每页的数量,和offset
function get_category_dtl($condition,$num,$offset) {
if($offset!=0){
$limit="$offset,$num";
}else{
$limit="$num";
}
$condition="where category_id=$condition";
$category_article_query=$this->db->query("select
article.id,
category.id as category_id,
category.category,
article.title,
article.addtime,
user.username,
article.hit,
article.content,
(select count(*) from comment where comment.article_id=article.id)as comment_qty
from article
left join category on article.category_id=category.id
left join user on article.user_id=user.id
$condition
order by article.id DESC
Limit $limit
");
if($category_article_query->num_rows()>0){
return $category_article_query->result();
} else {
return false;
}
}
} 复制代码
在控制器中,接受链接传来的category_id,
PHP复制代码
function index() {
$this->load->library('pagination');
$config['base_url'] = base_url().'index.php/category/index/'.$this->uri->segment(3).'/';
//总行数
$this->db->where('category_id',$this->uri->segment(3));
$config['total_rows'] = $this->db->get('article')->num_rows();
//每页行数
$config['per_page'] = '5';
$config['full_tag_open'] = '<div><p>';
$config['full_tag_close'] = '</p></div>';
$config['first_link'] = '首页';
$config['last_link'] = '尾页';
$config['next_link'] = '下页';
$config['prev_link'] = '上页';
$this->pagination->initialize($config);
//标题
$data['title']="老谭的Blog";
$this->load->model('Blog_model');
$data['article_query']=$this->Blog_model->get_category_dtl($this->uri->segment(3),$config['per_page'],$this->uri->segment(4));
$this->load->view('article_dtl',$data);
} 复制代码
View中代码:
PHP复制代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?=$title?></title>
</head>
<body>
<!-- 如果查询的结果返回值不等于false -->
<?php if($article_query!="false"):?>
<div>
<table>
<!-- 表格标题 -->
<tr>
<td>类别</td>
<td>标题</td>
<td>日期</td>
<td>评论</td>
</tr>
<!-- 列出文章列表 -->
<?php foreach($article_query as $article):?>
<tr>
<td><?=anchor('category/index/'.$article->category_id, $article->category)?></td>
<td><?=anchor('article/details/'.$article->id.'/'.$article->hit, $article->title." (人气:".$article->hit.")")?></td> <!-- 标题 -->
<td><?php echo $article->addtime;?></td> <!-- 时间 -->
<td><?php echo "评论(".$article->comment_qty.")";?></td>
</tr>
<?php endforeach;?>
</table>
</div>
<!-- 创建分页连接 -->
<?php echo $this->pagination->create_links(); ?>
<? else:?>
<div>当前没有文章</div>
<?php endif;?>
</body>
</html>
复制代码 |
|