Home > Blockchain >  Paging in codeigniter doesn't work - could not be converted to string
Paging in codeigniter doesn't work - could not be converted to string

Time:08-12

I'm not able to insert the pager function inside the readHunters() method so that the records in my table are not all on the same page (I want to put 10 records per page).

I know that in app->Config->Pager.php there is public $perPage = 10; which helps to define the number of records in the table with pagination.

  • HunterController.php

    public function readHunters()
    {
       try {
           $hunter = new HunterModel();
           $data['hunter'] = $hunter->findAll();
           $data['pager'] = $this->$hunter->page;
           return view('read_hunters', $data);
       } catch (\Exception $e) {
           exit($e->getMessage());
       }
    }

  • read_hunters.php

    <table >
        <thead>
            <tr>
                <th>ID</th>
                <th>Name</th>
                <th>Year</th>
                <th>Height</th>
                <th>Weight</th>
                <th>Type hunter</th>
                <th>Type nen</th>
                <th>Type blood</th>
                <th>Date registration</th>
                <th>Date upgrade</th>
                <th>Action</th>        
            </tr>
        </thead>
        <tbody>
            <?php foreach($hunter as $hxh): ?>
                <tr>
                    <td><?= $hxh['id_hunter']?></td>
                    <td><?= $hxh['name_hunter']?></td>
                    <td><?= $hxh['year_hunter']?></td>
                    <td><?= $hxh['height_hunter']?> m</td>
                    <td><?= $hxh['weight_hunter']?> kg</td>
                    <td><?= $hxh['type_hunter']?></td>
                    <td><?= $hxh['type_nen']?></td>
                    <td><?= $hxh['type_blood']?></td>
                    <td><?= date('d/m/Y H:i:s', strtotime($hxh['date_registration']))?></td>
                    <td><?= $hxh['date_update'] == $hxh['date_registration'] ? '' : date('d/m/Y H:i:s', strtotime($hxh['date_update'])) ?></td>
                    <td>
                        <a href="<?= site_url('form_update_hunter/'.$hxh['id_hunter']);?>" >"><i ></i>&nbsp;Update</a>
                        &nbsp;
                        <a href="<?= site_url('delete_hunter/'.$hxh['id_hunter']);?>" ><i ></i>&nbsp;Delete</a>
                    </td>
                </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
    <?= $pager->links(); ?>

CodePudding user response:

To provide a paginated list in the app, your controller's method looks like this:

$data = [
     'hunter' => $hunter->paginate(10),
     'pager' => $hunter->pager,
];

return view('read_hunters', $data);
  • Related