Pagination with MVC

Please, could someone help me? How to do pagination? follow my MVC system

Controller

// Load All Posts
    public function index(){
      $posts = $this->postModel->getPosts();

      $data = [
        'posts' => $posts
      ];
      
      $this->view('posts/index', $data);
    }

Model

// Get All Posts
    public function getPosts(){
      $this->db->query("SELECT *, 
                        posts.id as postId, 
                        users.id as userId
                        FROM posts 
                        INNER JOIN users 
                        ON posts.user_id = users.id
                        ORDER BY posts.created_at DESC;");

      $results = $this->db->resultset();

      return $results;
    }

View

  <?php foreach($data['posts'] as $post) : ?>
    <div class="card card-body mb-3">
      <h4 class="card-title"><?php echo $post->title; ?></h4>
      <div class="bg-light p-2 mb-3">
        Written by <?php echo $post->name; ?> on <?php echo $post->created_at; ?>
      </div>
      <p class="card-text"><?php echo $post->body; ?></p>
      <a class="btn btn-dark" href="<?php echo URLROOT; ?>/posts/show/<?php echo $post->postId; ?>">More</a>
    </div>
  <?php endforeach; ?>

so where do you want the pagination and how to you provide the current page to your controller?

Hello, thanks for responding, a pagination should appear in the view, the data I search for in the database within the model and I play in the driver which in turn plays in the view, but I don’t know how to implement the pagination, I know how I should use the limit and displacement in the query of the data, but I don’t know how to return.

if you’ve done the pagination within the statement then you should already have the data returned in return $results;.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.