Welcome Guest, Not a member yet? Register   Sign In
Using Pagination Library
#1

Hi all, I want to use the default pagination library coming with CI4 for paginating posts in a page. So, I created the following controller. The result of a query builder is stored in the protected variables $posts. In the index page I have inserted the corresponding code for links creation (<?= $pager->links() ?>). Regardless the number of posts in the page, the pager always creates one link (page). See image below. How I can tell to the pager to create more links (pages)? FYI. I have set $perPage public variable equal to 3 in app/Config/Pager.php.


PHP Code:
<?php

namespace App\Controllers;

class 
Blog extends BaseController
{
    protected $posts;

    public function __construct()
    {
        $db db_connect();
        $query_posts $db->query("SELECT * FROM posts ORDER BY created_at DESC");
        $this->posts $query_posts->getResult();
    }

 public function 
index()
 {
     $data = array(
          'posts'        => $this->posts,
          'pager'        => \Config\Services::pager()
     );

     return view('blog'$data);
    }

Reply
#2

I just use pagination via model ; what i can't see in yours anywhere is including

Code:
->paginate(10);
CMS CI4     I use Arch Linux by the way 

Reply
#3

(01-04-2022, 08:19 AM)captain-sensible Wrote: I just use pagination via model ; what i can't see in yours anywhere is including 

Code:
->paginate(10);

I'm aware of this. But the problem is that you can't paginate results of a query builder. If you do something like this:

PHP Code:
$query_posts $db->query("SELECT * FROM posts ORDER BY created_at DESC")->paginate(3); 

the following error will be raised: Call to undefined method CodeIgniter\Database\SQLite3\Result::paginate().

I don't want to use the <post> model to retrieve records from database. In other words: Is it possible to paginate the results of a query builder?
Reply
#4

You have only two ways to solve your problem.
1. Use pagination of the model.
2. Write your own implementation of pagination.

I recommend the second way. Only then will you understand how pagination works.
And then, looking at the original code, you will feel ashamed.

A comment about your code.
$db->query() returns the query result.
Services::pager() returns an instance of the pagination class.
But they are not related to each other.
What kind of magic do you expect?
Reply
#5

(This post was last modified: 01-05-2022, 05:36 AM by captain-sensible. Edit Reason: just remembered something )

yeah you can write your own pagination code I did it on another framework called fat free . The basics are get results, do a count on results , split number of results into arbitrary amount ; i remember i used ceil() function of php so as to get whole numbers and modulo ; i borked by old external hard drive where the code was otherwise i would just paste it for you

I think i also used urls like domain.com/page/$number

then using that variable $number to get the right segment of the posts

and i think i also used the total amount of results, divided by arbitrary number to know the number of view link pages to generate .
if i can remember more i will post it
CMS CI4     I use Arch Linux by the way 

Reply
#6

(This post was last modified: 01-05-2022, 12:44 PM by BilltheCat. Edit Reason: typo )

Here's a simple example controller I use for paging through my logs table.  Hopefully, it will help you get started.

PHP Code:
<?php

namespace App\Controllers\Admin;
use 
App\Controllers\BaseController;
use 
App\Models\LoggingModel;
class 
Logs extends BaseController
{

    public function index()
    {
        $this->data['meta']['title'] = 'Logs';
        $model = new LoggingModel();
        $request service('request');
        $searchData $request->getGet();


        $search = [];
        $search['search'] = '';
        $search['column'] = '';
        $search['perpage'] = 20;
        if (isset($searchData)) {
            if (isset($searchData['search'])) {
                $search['search'] = trim($searchData['search']);
            }
            if (isset($searchData['perpage'])) {
                $search['perpage'] = trim($searchData['perpage']);
            }
        }

        $search['column'] = 'date';
        if (isset($searchData['column'])) {
            switch ($searchData['column']) {
                case 'id':
                    $search['column'] = 'id';
                    break;
                case 'ip':
                    $search['column'] = 'ip';
                    break;
                    case 'host_name':
                        $search['column'] = 'host_name';
                        break;
                    case 'user_agent':
                        $search['column'] = 'user_agent';
                        break;
                        case 'referer':
                            $search['column'] = 'http_referer';
                            break;
                case 'date':
                default:
                    $search['column'] = 'date';
                    break;
            }
        }
        $search['order'] = (isset($searchData['order']) && $searchData['order'] === strtolower('asc')) ? 'asc' 'desc';

        if ($search == '') {
            $paginateData $model->paginate($search['perpage']);
        } else {
            $paginateData $model->select('*')
                ->orLike('ip'$search['search'])
                ->orLike('host_name'$search['search'])
                ->orLike('date'$search['search'])
                ->orderBy($search['column'], $search['order'])
                ->paginate($search['perpage']);
        }

        $this->data['data'] = $paginateData;
        $this->data['pager'] = $model->pager;
        $this->data['search'] = $search;

        echo view('Admin/Logs/index'$this->data);
    }


Reply




Theme © iAndrew 2016 - Forum software by © MyBB