为什么分页不能正常工作?

发布于 2024-12-05 16:51:40 字数 1482 浏览 1 评论 0原文

我有一个问题。我的 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;


    }
}

I have a problem. My Kohana pagination script not working correctly. Must have as follows : [http://127.0.0.1/?page=1], but I have the following - [http://127.0.0.1/index.php/?page=1] and records from database in home page are 2 (total records is 2, i set items_per_page to 1), but must have a 1 record only. Where is the problem?

Controller:

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';
    }

View

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

And model

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;


    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迟月 2024-12-12 16:51:40

有 2 个问题:

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

分页对象仅用于呈现分页控件,不执行数据库记录的实际过滤。

There are 2 problems:

  1. You're not using url rewrite rules to rewrite /index.php?page=2 into /?page=2
  2. You're not actually using the page query parameter to filter your records returned from the db, so it'll display them all.

The pagination object is only used to render a pagination control, not perform the actual filtering of db records.

乖不如嘢 2024-12-12 16:51:40

你可以设置 bootstrap.php:

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

you maybe set bootstrap.php:

Route::set('index_page','yourcontroller/index(/<page>)', array('page' => '[0-9]+'))
    ->defaults(array(
        'controller'    => 'yourcontroller',
        'action'    => 'index',
    ));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文