CodeIgniter分页.不确定如何实施 [英] CodeIgniter pagination. Unsure how to implement
问题描述
我无法实现CI基础安装随附的CodeIgniter分页类.
I am having trouble implementing the CodeIgniter pagination class that comes with the CI base install.
我有一个页面,其中显示新闻文章并将其显示在页面上.我希望每页最多显示5条文章,并具有分页浏览这些页面的功能.
I have a page which shows news articles and shows them on a page. I want to show a maximum of 5 articles per page and have the pagination to browse through these pages.
我已经阅读了CI文档,但是我不确定应该将config ['base_url']变量指向什么.这是我的代码(请记住,我已从示例中删除了与该问题无关的代码):
I have read the CI documentation but I am unsure as to what I am supposed to be pointing the config['base_url'] variable to. Here is my code (please bear in mind I have removed code from the examples which is not relevant with this issue):
控制器:
class News extends CI_Controller {
function __construct() {
parent::__construct();
}
public function index() {
//Get News Articles
$options = array(
'orderBy' => 'DESC'
);
$newsArticles = $this->admin_model->getNewsArticles($options);
$data['newsArticles'] = array();
foreach($newsArticles as $newsArticle) {
$date = date('F j, Y, g:i a', strtotime($newsArticle['date_created']));
$active = ($newsArticle['active'] == 1 ? 'tick.png' : 'cross.png');
$data['newsArticles'][] = array(
'articleId' => $newsArticle['news_id'],
'title' => $newsArticle['title'],
'content' => htmlspecialchars_decode(stripslashes($newsArticle['content'])),
'date' => $date,
'author' => $newsArticle['author'],
'active' => $this->config->base_url() . "/images/" . $active,
'link' => $this->config->base_url() . "/index.php/admin/newsPage?articleId=" . $newsArticle['news_id']
);
}
//Pagination
$config['base_url'] = $this->config->base_url() . "index.php/news/getNewsArticles";
$config['total_rows'] = 200;
$config['per_page'] = 20;
$this->pagination->initialize($config);
echo $this->pagination->create_links();
模型函数只是一个返回新闻文章的函数,在这里工作正常.
The model function is simply a function which returns the news articles and is working in fine here.
是否需要将base_url变量指向一个将数据库结果直接返回到分页类的函数?我不确定如何实现.
Do I need to point the base_url variable to a function that returns the database results straight to the pagination class? I am unsure as tho how it is implemented.
如果有人能指出我正确的方向或建议我要去哪里,那将很棒.
If anyone could point me in the right direction or advise as to where I am going wrong that would be great.
推荐答案
好,在查看Ellislab教程后,我设法使其正常运行.这是修改后的控制器.请注意,在我的模型函数中,我还添加了一个子句来捕获偏移量.
Ok I managed to get it working after checking the Ellislab tutorials. Here is the revised controller. Please note that in my model function I also added a clause to catch the offset.
//Pagination
$config['base_url'] = $this->config->base_url() . "index.php/news/news/index";
$config['total_rows'] = $this->admin_model->getMaxNewsRows();
$config['per_page'] = 5;
$config['uri_segment'] = 4;
$config['num_links'] = 5;
$config['next_link'] = 'Next';
$config['next_tag_open'] = '<span class="nextPage">';
$config['next_tag_close'] = '</span>';
$config['prev_link'] = 'Prev';
$config['prev_tag_open'] = '<span class="prevPage">';
$config['prev_tag_close'] = '</span>';
$config['cur_tag_open'] = '<span class="active_page">';
$config['cur_tag_close'] = '</span>';
$this->pagination->initialize($config);
//Get News Articles
$offset = $this->uri->segment(4);
$options = array(
'orderBy' => 'DESC',
'limit' => $config['per_page']
);
if($offset != 0) {
$options['offset'] = $offset;
}
$newsArticles = $this->admin_model->getNewsArticles($options);
//$newsArticles = $this->db->get('news', $config['per_page'], $this->uri->segment(4));
$data['newsArticles'] = array();
foreach($newsArticles as $newsArticle) {
$date = date('F j, Y, g:i a', strtotime($newsArticle['date_created']));
$active = ($newsArticle['active'] == 1 ? 'tick.png' : 'cross.png');
$data['newsArticles'][] = array(
'articleId' => $newsArticle['news_id'],
'title' => $newsArticle['title'],
'content' => htmlspecialchars_decode(stripslashes($newsArticle['content'])),
'date' => $date,
'author' => $newsArticle['author'],
'active' => $this->config->base_url() . "/images/" . $active,
'link' => $this->config->base_url() . "/index.php/admin/newsPage?articleId=" . $newsArticle['news_id']
);
}
$data['pagLinks'] = $this->pagination->create_links();
这篇关于CodeIgniter分页.不确定如何实施的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!