【问题标题】:Why pagination not working correctly?为什么分页不能正常工作?
【发布时间】:2011-09-20 16:06:55
【问题描述】:

我有问题。我的 Kohana 分页脚本无法正常工作。必须有以下内容:[http://127.0.0.1/?page=1],但我有以下内容 - [http://127.0.0.1/index.php/?page=1] 和来自主页数据库的记录页为 2(总记录为 2,我将 items_per_page 设置为 1),但必须只有 1 条记录。问题出在哪里?

控制器:

public function action_index()
    { 
      $pagination = Pagination::factory(array(
        'total_items'=> Model::factory('index')->get_count(),
        'items_per_page' => 1,));

      $articles_ = Model::factory('index')->do_magic();
      $this->template->page_title = 'Sākums';
      $this->template->site_name = Kohana::$config->load('common')->get('site_name'); 
      $this->template->content = View::factory('index/index')
              ->set('query', $articles_)
              ->set('pagjinaacija', $pagination->render()); 
      $this->template->styles[] = 'index/index';
    }

查看

<?php 
foreach($query as $row) {
    echo '<h2>'.$row['title'].'</h2>';
    echo '<p style="margin:0">'.$row['content'].'</p>';
}
echo $pagjinaacija;
?>

和模型

Class Model_Index Extends Model {
    public function get_count() {
    return  DB::query(Database::SELECT, 'SELECT COUNT(*) AS count FROM posts')->execute()->get('count');

}
    public function do_magic() {
        $query = DB::query(Database::SELECT, 'SELECT * FROM posts ORDER By id DESC')->execute()->as_array();
        return $query;


    }
}

【问题讨论】:

  • 删除您的问题对花时间回答问题的人不公平。

标签: php frameworks kohana


【解决方案1】:

有2个问题:

  1. 您没有使用 url 重写规则将 /index.php?page=2 重写为 /?page=2
  2. 您实际上并没有使用页面查询参数来过滤从数据库返回的记录,因此它将全部显示。

分页对象仅用于渲染分页控件,不执行对db记录的实际过滤。

【讨论】:

    【解决方案2】:

    你可能会设置 bootstrap.php:

    Route::set('index_page','yourcontroller/index(/<page>)', array('page' => '[0-9]+'))
        ->defaults(array(
            'controller'    => 'yourcontroller',
            'action'    => 'index',
        ));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-28
      • 2016-07-16
      • 2019-01-04
      • 2020-09-03
      • 2016-10-10
      • 2016-10-24
      • 2017-02-27
      • 2017-07-08
      相关资源
      最近更新 更多