lpwd-php-portfolio/lib/project.php

65 lines
1.5 KiB
PHP

<?php
function getProjects($page = 1, $limit = 5)
{
if (!is_integer($page)) {
throw new InvalidArgumentException('The argument "page" must be an interger.');
}
if (!is_integer($limit)) {
throw new InvalidArgumentException('The argument "limit" must be an interger.');
}
$from = ($page - 1) * $limit;
$pdo = getDatabaseConnection();
$query = $pdo->prepare('select id, title, illustration, description, date from project limit :from, :limit');
$query->bindParam(':from', $from, PDO::PARAM_INT);
$query->bindParam(':limit', $limit, PDO::PARAM_INT);
$query->execute();
return $query->fetchAll();
}
function getProject($id)
{
if (!is_integer($id)) {
throw new InvalidArgumentException('The argument "id" must be an interger.');
}
$pdo = getDatabaseConnection();
$query = $pdo->prepare('select id, title, illustration, description, date from project where id=:id');
$query->bindParam(':id', $id, PDO::PARAM_INT);
$query->execute();
return $query->fetch();
}
function getProjectNumberOfPages($maxPerPage)
{
if (!is_integer($maxPerPage)) {
throw new InvalidArgumentException('The argument "maxPerPage" must be an interger.');
}
$pdo = getDatabaseConnection();
$query = $pdo->prepare('select count(*) as total from project');
$query->execute();
$result = $query->fetch();
return ceil($result['total'] / $maxPerPage);
}
function getCommentsByProject($id)
{
// À réaliser
}