Viewing file: blog.php (6.9 KB) -rw-rw-r-- Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
<?php
include 'header.php';
$adjacents = 3;
/*
First get total number of rows in data table.
If you have a WHERE clause in your query, make sure you mirror it here.
*/
// $total_pages_sql = $con->query("SELECT COUNT(*) as total FROM blog order by status desc,id desc");
$total_pages_sql = $con->query("SELECT COUNT(*) as total FROM blog order by id desc");
$result = mysqli_fetch_assoc($total_pages_sql);
$total_pages = $result['total'];
if (isset($_GET['page'])) {
$page = $_GET['page'];
} else {
$page = 1;
}
/* Setup vars for query. */
$limit = 5; //how many items to show per page
if ($page)
$start = ($page - 1) * $limit; //first item to display on this page
else
$start = 0; //if no page var is given, set start to 0
/* Get data. */
// $query = "SELECT * FROM blog order by status desc,id desc LIMIT $start, $limit";
$query = "SELECT * FROM blog order by id desc LIMIT $start, $limit";
$blogResult = $con->query($query);
/* Setup page vars for display. */
if ($page == 0)
$page = 1; //if no page var is given, default to 1.
$prev = $page - 1; //previous page is page - 1
$next = $page + 1; //next page is page + 1
$lastpage = ceil($total_pages / $limit); //lastpage is = total pages / items per page, rounded up.
$lpm1 = $lastpage - 1; //last page minus 1
/*
Now we apply our rules and draw the pagination object.
We're actually saving the code to a variable in case we want to draw it more than once.
*/
$pagination = "";
if ($lastpage > 1) {
$pagination .= "<div class=\"pagination\" style='float: right;'>";
//previous button
if ($page > 1)
$pagination .= "<a href=\"?page=$prev\">Previous</a>";
else
$pagination .= "<a class=\"disabled\">Previous</a>";
//pages
if ($lastpage < 7 + ($adjacents * 2)) { //not enough pages to bother breaking it up
for ($counter = 1; $counter <= $lastpage; $counter++) {
if ($counter == $page)
$pagination .= "<a href='#' class=\"current\">$counter</a>";
else
$pagination .= "<a href=\"?page=$counter\">$counter</a>";
}
}
elseif ($lastpage > 5 + ($adjacents * 2)) { //enough pages to hide some
//close to beginning; only hide later pages
if ($page < 1 + ($adjacents * 2)) {
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++) {
if ($counter == $page)
$pagination .= "<a href='#' class=\"current\">$counter</a>";
else
$pagination .= "<a href=\"?page=$counter\">$counter</a>";
}
$pagination .= "...";
$pagination .= "<a href=\"?page=$lpm1\">$lpm1</a>";
$pagination .= "<a href=\"?page=$lastpage\">$lastpage</a>";
}
//in middle; hide some front and some back
elseif ($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2)) {
$pagination .= "<a href=\"?page=1\">1</a>";
$pagination .= "<a href=\"?page=2\">2</a>";
$pagination .= "...";
for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++) {
if ($counter == $page)
$pagination .= "<a href='#' class=\"current\">$counter</a>";
else
$pagination .= "<a href=\"?page=$counter\">$counter</a>";
}
$pagination .= "...";
$pagination .= "<a href=\"?page=$lpm1\">$lpm1</a>";
$pagination .= "<a href=\"?page=$lastpage\">$lastpage</a>";
}
//close to end; only hide early pages
else {
$pagination .= "<a href=\"?page=1\">1</a>";
$pagination .= "<a href=\"?page=2\">2</a>";
$pagination .= "...";
for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++) {
if ($counter == $page)
$pagination .= "<a href='#' class=\"current\">$counter</a>";
else
$pagination .= "<a href=\"?page=$counter\">$counter</a>";
}
}
}
//next button
if ($page < $counter - 1)
$pagination .= "<a href=\"?page=$next\">Next</a>";
else
$pagination .= "<a class=\"disabled\">Next</a>";
$pagination .= "</div>\n";
}
?>
<section class="bnner_sec-rs gallery">
<div class="container">
<div class="row">
<div class="col-12">
<div class="banner-sec">
<div class="hdng gall">
<h1 style="text-transform: uppercase;">Blog</h1>
<p></p>
</div>
</div>
</div>
</div>
</div>
</section>
<section>
<div class="container">
<div class="row">
<div class="col-lg-12">
<div class="sics-abt">
<div class="blog-main">
<?php
while ($blogs = mysqli_fetch_assoc($blogResult)) {
?>
<div class="blog-card">
<h2><?php echo $blogs['blog_title']; ?></h2>
<h5><?php echo date("D d M Y", strtotime($blogs['created'])); ?></h5>
<div class="blog-fflex">
<div>
<div class="blog-img" >
<?php
if ($blogs['blog_image']) {
echo '<img src="blog/' . $blogs['blog_image'] . '" alt="srishti campus ' . $blogs['blog_title'] . ' trivandrum" Title="' . $blogs['blog_title'] . ' " /> ';
}
?>
</div>
</div>
<div>
<div class="blog-desc main">
<?php echo $blogs['blog_content']; ?>
</div>
<a href="blog_details.php?id=<?php echo $blogs['id']; ?>">
<button class="readmore">READ MORE</button>
</a>
</div>
</div>
</div>
<?php
}
?>
<br>
<?= $pagination ?>
<br><br><br>
</div>
</div>
</div>
</div>
</section>
<?php include 'footer.php'; ?>
|