// CONTROLLER
<?php namespace App\Controllers;
use App\Models\AuthorModel;
use Config\Services;
/**
* Class Home
*
* @package App\Controllers
*/
class Home extends BaseController
{
private $author;
public function index()
{
$pager = Services::pager();
$this->author = new AuthorModel();
// This will get all Authors
$data = [
'authors' => $this->author->getAllAuthors()->paginate(4, 'group1'),
'pager' => $this->author->pager,
];
echo view('test', $data);
// This will get all authors by category id
$dat = [
'authors' => $this->author->getAuthorsByCategory(14)->paginate(4, 'group1'),
'pager' => $this->author->pager,
];
//echo view('test', $dat);
//return view('welcome_message');
}
//--------------------------------------------------------------------
}
// MODEL
// -------------------------------------------------------------------
/**
* getAuthors ()
* -------------------------------------------------------------------
*
* Get all authors
*/
public function getAuthors()
{
$builder = $this->builder();
$builder->orderBy('id', 'ASC');
$builder->get();
return $this;
}
// -------------------------------------------------------------------
/**
* getAuthorsByCategory ()
* -------------------------------------------------------------------
*
* Get all authors by category
*/
public function getAuthorsByCategory(int $id)
{
$builder = $this->builder();
$builder->where('category_id', $id);
return $this;
}
//--------------------------------------------------------------------
// VIEW
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link href="<?= base_url('assets/vendor/bootstrap/css/bootstrap.min.css');?>" rel="stylesheet">
</head>
<body>
<div class="container-fluid">
<?php
foreach ($authors as $key => $list)
{
//echo $key."<br>";
echo $list['id']."<br>";
echo $list['category_id']."<br>";
echo $list['first_name']."<br>";
echo $list['last_name']."<br>";
echo $list['email']."<br>";
echo $list['birthdate']."<br>";
echo $list['added']."<br><br>";
}
echo $pager->simpleLinks('group1', 'bs_simple');
//echo $pager->makeLinks($page, $perPage, $total, 'bs_simple');
?>
</div>
<script href="<?= base_url('assets/vendor/jquery/jquery-3.5.1.min.js');?>"></script>
<script href="<?= base_url('assets/vendor/bootstrap/js/bootstrap.bundle.min.js');?>"></script>
</body>
</html>