【发布时间】:2013-09-24 05:08:53
【问题描述】:
我在实现 CI 基础安装附带的 CodeIgniter 分页类时遇到问题。
我有一个显示新闻文章并在页面上显示它们的页面。我希望每页最多显示 5 篇文章,并有分页浏览这些页面。
我已阅读 CI 文档,但我不确定我应该将 config['base_url'] 变量指向什么。这是我的代码(请记住,我已从示例中删除了与此问题无关的代码):
控制器:
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();
模型函数只是一个返回新闻文章的函数,在这里可以正常工作。
我是否需要将 base_url 变量指向一个将数据库结果直接返回到分页类的函数?我不确定它是如何实现的。
如果有人能指出我正确的方向或建议我哪里出错了,那就太好了。
【问题讨论】:
-
您好,CI 分页非常棒且易于使用,请先去看一些教程(并非所有教程都只是那些您感兴趣的教程),它的解释很好。这是来源net.tutsplus.com/sessions/codeigniter-from-scratch。
-
非常感谢。我按照本教程设法让它工作。
-
还请注意
$offset可以是您的控制器方法的最后一个参数;) 像这样public function method($arg1 = '', $arg2 = '', $offset = 0) { },还请注意它不适用于index()方法,您必须使用_remap()。祝你有美好的一天。 -
你应该加载 url_helper 而不是到处写 $this->config->base_url,你可以使用 base_url() 代替。并且请不要使用 base_url 进行分页使用 site_url 函数,例如:site_url("news/index")
标签: php codeigniter pagination