Nicotine

Open source framework for PHP & MySQL

Downloads Page

Let say you have a model /workspace/site/models/ArticlesModel.php:

<?php
declare(strict_types=1);

namespace 
workspace\site\models;

use 
nicotine\Model;

class 
ArticlesModel extends Model {
    public function 
countArticles()
    {
        return 
$this->db->getValue("
            SELECT COUNT(*) FROM `articles`
            -- WHERE...
        "
);
    }
    
    public function 
getArticles($limitStart$limitEnd)
    {
        return 
$this->db->getAll("
            SELECT * FROM `articles` 
            -- WHERE...
            LIMIT "
.intval($limitStart).", ".intval($limitEnd)."
        "
);
    }
}

Also let say you have controller /workspace/site/controllers/Articles.php:

<?php
declare(strict_types=1);

namespace 
workspace\site\controllers;

use 
nicotine\Controller;
use 
nicotine\Pagination;

class 
Articles extends Controller {

    public function list()
    {
        
$totalArticles $this->proxy->site->model('ArticlesModel')->countArticles();
        
$pagination = new Pagination($totalArticles);
        
        
// Optional properties:
        
$pagination->perPage 20;
        
$pagination->info 'Page %1$d from %2$d';
        
$pagination->previousPage 'Previous page';
        
$pagination->nextPage 'Next page';
        
        
$articles $this->proxy->site->model('ArticlesModel')->getArticles($pagination->getLimitStart(), $pagination->getLimitEnd());
        
        
$this->layout 'layout-file';
        
$this->proxy->view('articles/list'compact('articles''pagination'));
    }
}

The layout /workspace/site/layouts/layout-file.php. This layout can hold multiple views, accross HTTP requests.

<!DOCTYPE html>
    ...
    <?php print $this->contentForLayout?>
    ...
...

The view /workspace/site/views/articles/list.php:

<?php foreach ($this->vars->articles as $article) { ?>
    List articles here...
<?php ?>

Show pagination buttons
<?php $this->vars->pagination->showNavigation(); ?>