Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
How to Implement Pagination in MVC Architecture?
  • My situation is like... I have method in my model, that selects all articles from the database and then return them as an array. After that I save that array in new variable, then pass that variable to the view (this all happened from controller). In view I make a loop through array to display articles. I want to have pagination below my articles. What's the best way to do it using Pagination class? Where to put what?
  • I do it this way:
    public function action_index($current_page = 1)
    {
     $config = array(
      'pagination_url' => \Uri::create('blog/home/'),
      'total_items' => Model_Article::count(),
      'per_page' => \Settings::get('blog.perpage'),
      'current_page' => (int) $current_page,
     );
    
     \Pagination::set_config($config);
    
     $filters = array(
      'limit' => \Pagination::$per_page,
      'offset' => \Pagination::$offset
     );
     $articles = Model_Article::get_all($filters);
     
     if(!$articles)
     {
      \Request::show_404();
     }
    
     $data = array('articles' => $articles, 'pagination' => \Pagination::create_links());
    
     $this->response->body(\View::factory('home_view', $data));
    }
    
    <div id="content">
    
     <?php foreach($articles as $article) : ?>
    
      <h3><?php echo $article['title']; ?></h3>
    
     <?php endforeach; ?>
    
     <div class="pagination">
      <?php echo $pagination; ?>
     </div>
    
    </div><!-- #content -->
    
  • Oki-doki. Before I did all the pagination in the model.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion