I also have problem with pagination. I'm new to codeigniter and just started learning last 2 weeks and I'm stuck on how to make the pagination work on my project. I have also read the documentation and still can't understand how it works for paginating a query where I joined a table with other tables. I also tried paginating first the main table but how could i print out the data of a specific user from other tables? Please can someone help me.
This is my Controller:
Code:
class SubjectCode extends BaseController
{
public function index()
{ ...}
public function users()
{
helper(['form']);
$db = db_connect();
$model = new CustomModel($db);
$data = [
'users' => $model->getUsers()->get()->getResultArray(), //I got an error if I paginate this line: Call to a member function paginate() on array
'pager' => $model->pager
];
return view('/subj_students', $data);
}
Then this is my Model
Code:
function getUsers(){
$builder = $this->db->table('users')
->orderBy('users.usr_lname', 'ASC')
->join('classes', 'usrs.class_id = classes.class_id')
->join('subjectCode', 'users.usr_id = subjectCode.subjectCode_id');
return $builder;
}
and here is my View
Code:
<tbody>
<?php if($students): ?>
<?php $i = 1; ?>
<?php foreach($users as $student): ?>
<tr>
<td align="center"><?php echo $i; ?></td>
<td><?php echo $student['usr_lname'].", ".$student['usr_fname']; ?></td>
<td><?php echo $student['usr_id_no']; ?></td>
<td><?php echo $student['course'].$student['year'].$student['section']; ?></td>
<td><?php echo $student['subjectCode_name']; ?></td>
</tr>
<?php $i++;
endforeach; ?>
<?php else: ?>
<tr>
<td colspan='5'>No record found.</td>
</tr>
<?php endif; ?>
</tbody>
</table>
<div class="row">
<div class="col-md-12">
<div class="row">
<?php if ($pager) :?>
<?php $pagi_path= '/subj/subj_students'; ?>
<?php $pager->setPath($pagi_path); ?>
<?= $pager->links() ?>
<?php endif ?>
</div>
</div>
</div>
Please send help.