Reputation: 41
I have one field in admin panel name PostDisplay,when i enter 3 in postDisplay then on post page (frontside) it will display 3 post, my question is when i enter 20 in postDisplay it display 20 post but i want a pagination on page thus page 1 display 5 post 2nd page next 5 and so on, this is my php page which get data and display post.
<?php
include_once ('class/user_posts.php');
$user_posts = new user_posts;
$slug = "post";
$svs = $user_posts->select(" where A.type='" . $slug . "' ", "", "0", "$postcount");
$blogArray = array();
if (mysql_num_rows($svs) != 0) {
while ($row = mysql_fetch_array($svs)) {
$blogArray[] = $row;
}
}
?>
<div class="center margin-bottom-lg">
<div class="container">
<div class="row padding-bottom-lg">
<div class="col-md-12">
<div class="gray-box">
<h1 class="header">
<?php echo $user_options->getvalue('blogtitle', 'Blog...'); ?>
</h1>
<div class="row our-services margin-bottom-lg padding-lg">
<?php
header('Cotent-type: text/plain'); // Just here for formatting
foreach ($blogArray as $array) {
echo '<div class="col-md-4">';
echo '<div class="row">';
echo '<div class="col-xs-12">';
echo '<a class="title-link" href="#">';
echo '<h3>' . $array['slug'] . '</h3>';
echo '</a>';
echo '<p>' . $array['description'] . '</p>';
echo '<p class=""><a href="#" class="btn-link">Read More...</a></p>';
echo '</div>';
echo '</div>';
echo '</div>';
}
?>
</div>
</div>
</div>
</div>
</div>
</div>
Upvotes: 3
Views: 810
Reputation: 61
It seems $svs is the variable that contains the data you want to paginate but we don't have enough info like, ok, PostDisplay is the variable that manipulate how many posts will show up on your page but your code doesn't mention it once. So I will go with global advices on how to paginate:
To paginate, you need to use the LIMIT clause in the SQL statement that will search your data. LIMIT takes to arguments, "the first argument specifies the offset of the first row to return, and the second specifies the maximum number of rows to return". (From https://dev.mysql.com/doc/refman/5.5/en/select.html)
So, let says postDisplay is equal to 3, by using LIMIT 0, 3 (<=postDisplay), your SELECT statement will return the first three 3 results of your statement. With postDisplay=20 (LIMIT 0, 20), first 20 results.
Now, you have 20 user posts and you want to make pagination to render 4 pages with 5 posts each. You need to set postDisplay to 5 and use another variable to calculate the first argument of the LIMIT clause, because, on page 1, you will display results from 0 to 4, page 2, 5 to 9, page 3, 10 to 14, last page 15 to 19. That first argument will be 0 then 5 > 10 > 15. It is where it starts.
To calculate it, you need to know on what page you are, if it's page 1, 2, 3 or 4. So you set a $_GET['page'] and with it you will calculate based on the postDisplay, if your first argument will be 0, 5, 10 or 15.
You also need to make a statement to know how many user posts you have in your database to start the calculation on how to cut that number in slices.
//here you get how many user posts total there is
$req_nbr_user_posts=mysqli_query($yourdbconnexion,"SELECT COUNT(*) AS nbr FROM user_posts");
$nbr_user_posts=mysqli_fetch_assoc($req_nbr_user_posts);
$postDisplay=5;
$current_page=$_GET['page'];
//here you have the total of pages for your pagination with $pages_total
$number_of_pages=floor($nbr_user_posts['nbr']/$postDisplay);
if($nbr_user_posts['nbr']%$postDisplay==0) $rest=0;
else $rest=1;
$pages_total=$number_of_pages+$rest;
//here is how to calculate that first argument of the LIMIT clause
$first_argument=($current_page-1)*$postDisplay;
$what_to_display=mysqli_query($yourdbconnexion,"SELECT * FROM user_posts LIMIT ".$first_argument.", ".$postDisplay);
That's it!
Upvotes: 1