pagination

This commit is contained in:
Jenkings
2024-07-22 10:56:03 +00:00
parent ac3ce7bca4
commit 52c3de418b
2 changed files with 45 additions and 6 deletions

View File

@@ -13,6 +13,8 @@ $menu = new Menu($db);
$article->id = 0;
$numPages = $article->getPagesCount();
$result = $article->getArticles();
include('template/header.php');
@@ -46,4 +48,20 @@ include('template/header.php');
<?php } ?>
</div>
</div>
<div class="pagination">
<ul>
<?php
$curpage = (isset($_GET['page']) ? $_GET['page'] : 1);
/* pagination */
for($p = 1; $p <= $numPages;$p++){
echo " <li><a href='.?page=".$p."' class='" . ($p == $curpage ? "selectedPage" : "") . "'>" . $p . "</a></li>";
}
?>
</ul>
</div>
<?php include('template/footer.php');?>

View File

@@ -10,7 +10,7 @@ class Articles {
$this->conn = $db;
}
public function getArticles(){
public function getArticles($cnt = null,$offset = null){
$query = '';
if($this->id) {
$query = " AND p.id ='".$this->id."'";
@@ -21,13 +21,28 @@ class Articles {
LEFT JOIN ".$this->categoryTable." c ON c.id = p.category_id
LEFT JOIN ".$this->userTable." u ON u.id = p.userid
WHERE p.status ='published' $query ORDER BY p.id DESC";
if(is_int($cnt)){
$sqlQuery .= " LIMIT " . $cnt;
}
if(is_int($offset)){
$sqlQuery .= " OFFSET " . $offset;
}
$stmt = $this->conn->prepare($sqlQuery);
$stmt->execute();
$result = $stmt->fetchAll();
$result = $stmt->fetchAll();
return $result;
}
public function getPageArticles($page = 1){
if(!is_int($page)){
$page = 1;
}
return $this->getArticles(Config::ARTICLES_PER_PAGE,(($page - 1) * Config::ARTICLES_PER_PAGE) );
}
function formatMessage($string, $wordsreturned) {
$retval = $string; // Just in case of a problem
$array = explode(" ", $string);
@@ -39,13 +54,19 @@ class Articles {
}
return $retval;
}
public function totalPost(){
$sqlQuery = "SELECT * FROM ".$this->postTable;
$sqlQuery = "SELECT COUNT(*) as count FROM ".$this->postTable;
$stmt = $this->conn->prepare($sqlQuery);
$stmt->execute();
$result = $stmt->fetchAll();
return $result->num_rows;
}
$result = $stmt->fetch(PDO::FETCH_ASSOC);
return $result['count'];
}
public function getPagesCount(){
return ceil($this->totalPost() / Config::ARTICLES_PER_PAGE);
}
}
?>