我为搜索页面创建了分页。 控制器 -
public function action_index()
{
if(!empty($_GET['search'])){
$pagination= Pagination::factory(array(
'total_items' => Model::factory('index')->get_count(),
'items_per_page' => 2,
));
$this->template->content = View::factory('index/query')
->set('query', Model::factory('index')->get_articles_from_query($_GET['search'], $pagination->offset, $pagination->items_per_page))
->set('pagination', $pagination->render());
}
else{
$this->template->content = View::factory('index/error')->set('message', 'Nav rakstu ar šādu atslēgvārdu.');
}
}
查看 -
<?php
foreach ($query as $item):
echo '<h2><a href="/article/' . $item['slug'] . '">' . $item['virsraksts'] . '</a></h2>';
echo '<p>' . nl2br($item['saturs']) . '</p>';
endforeach;
endif;
echo $pagination;
?>
模型 -
public function get_articles_from_query($squery, $offset, $limit){
$query = DB::query(Database::SELECT, 'SELECT * FROM ieraksti WHERE virsraksts like :squery OR slug like :squery OR saturs like :squery ORDER By id DESC LIMIT :offset, :limit ')
->parameters(array(':squery' => "%$squery%", ':offset' => $offset, ':limit' => $limit))->execute()->as_array();
return $query;
}
分页一直出现。我想分页,当文章发现超过2.错误在哪里?这是什么错误?
答案 0 :(得分:1)
我假设您使用的是Kohana 3.x,然后将auto_hide
设置为true
。
$pagination= Pagination::factory(array(
'total_items' => Model::factory('index')->get_count(),
'items_per_page' => 2,
'auto_hide' => true
));
修改强>
Kohana 2.3.x也支持auto_hide
。请参阅docs for Pagination。
编辑2
如果上述内容对您不起作用,请不要将分页分配给视图,如果total_items
items_per_page
更少{。}}。
if(!empty($_GET['search'])){
$items_per_page = 2;
$total_items = Model::factory('index')->get_count();
$pagination= Pagination::factory(array(
'total_items' => $total_items,
'items_per_page' => $items_per_page,
));
$this->template->content = View::factory('index/query')
->set('query', Model::factory('index')->get_articles_from_query($_GET['search'], $pagination->offset, $pagination->items_per_page));
if($items_per_page < $total_items) {
$this->template->content->set('pagination', $pagination->render());
}
else {
$this->template->content->set('pagination', '');
}
}