I want return ALL posts in the table Mysql and each page show only 6 posts and in bottom show pagination.
I can return all posts, but I dont know how work with pagination:
<?php
$query = "SELECT * FROM myTable";
$variable = $crud->viewdatas($query);
foreach ($variable as $key => $value) {
?>
<div class="col-md-1">
<div class="thumbnail">
<a href="/category/<?php echo $value['post_slug'] ?>" >
<img src="http://example.com/uploads/images/<?php echo $value['post_slug'] ?>.jpg">
</a>
</div>
</div>
<?php
}
?>
I created Friendly urls with post_slug column in mysql and in htacess
RewriteRule ^category/?$ example.com/my_category.php [NC,L]
RewriteRule ^category\/(.*)\/?$ example.com/posts.php?slug=$1 [NC,L]
Pagination:
<nav style="text-align:center">
<ul class="pagination">
<li>
<a href="#" aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
</li>
<li><a href="#">1</a></li>
<li><a href="#">2</a></li>
<li><a href="#">3</a></li>
<li><a href="#">4</a></li>
<li><a href="#">5</a></li>
<li>
<a href="#" aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</li>
</ul>
</nav>
When click a number in the pagination link, direct url:
site.com/category/1
site.com/category/2
site.com/category/3
...
But I dont know how start for insert php pagination.
Thanks for any help
Are you using some sort of incrementing row in your table? If so, you could do something like this
URL would be site.com/category/yourpage.php?page=2
$page = $_GET['page'];
$resultsLimit = 6;
$upperLimit = 6 * $page;
$lowerLimit = (6*page) - $resultsLimit;
$query = "SELECT * FROM myTable WHERE id <= {$upperLimit} AND id >= {$lowerLimit}";
Adapt to your project of course;