Hey everyone, I have a MYSQLI statement that i would like to add pagination to. I would like to have it set to 5 per page, and not have a negative value if you are on page 1 (disables back link) Every tutorial i have been seeing allows for a negative back and i cannot figure it out. My code is as follows and I know that i have to introduce the Pagination before the while statement and it needs to finish after the while statement..

<?php 

$query  = "SELECT * FROM `blog_posts` ORDER BY postID DESC ";
$result = mysqli_query($bd, $query);

while ($row = mysqli_fetch_assoc($result)){

                    echo '<div>';
                        echo '<img src="../mini_logo.png" height="25" width="50">';
                        echo '<h3><a href="viewpost.php?id='.$row['postID'].'">'.$row['postTitle'].'</a></h3>';
                        echo '<p>Posted by '.$row['postBy']. ' on '.date('M jS Y H:i:s', strtotime($row['postDate'])).'</p>';
                        echo '<p>'.$row['postDesc'].'</p>';             
                        echo '<p align="right"><a class="btn btn-primary btn-md active" role="button" href="viewpost.php?id='.$row['postID'].'">Read More <span class="glyphicon glyphicon-arrow-right"> </span></a></p>';              
                    echo '</div><hr>';

                }
?>

Any help would be awesome!

Recommended Answers

All 6 Replies

I am not using PDO connection and i dont have any PDO scripting in my site. Its all simple things, I am not looking for extravigance, I am looking for practicality. I would have to change alot of code just for this to work.

Okay guys, i finally added a pagination Script see below

<?php 

    $start=0;
    $limit=3;

    if(isset($_GET['id'])){
    $id=$_GET['id'];
    $start=($id-1)*$limit;
    }
$query  = "SELECT * FROM `blog_posts` ORDER BY postID DESC LIMIT $start, $limit";
$result = mysqli_query($bd, $query);
while ($row = mysqli_fetch_assoc($result)){

                    echo '<div>';
                        echo '<img src="../mini_logo.png" height="25" width="50">';
                        echo '<h3><a href="viewpost.php?id='.$row['postID'].'">'.$row['postTitle'].'</a></h3>';
                        echo '<p>Posted by '.$row['postBy']. ' on '.date('M jS Y H:i:s', strtotime($row['postDate'])).'</p>';
                        echo '<p>'.$row['postDesc'].'</p>';             
                        echo '<p align="right"><a class="btn btn-primary btn-md active" role="button" href="viewpost.php?id='.$row['postID'].'">Read More <span class="glyphicon glyphicon-arrow-right"> </span></a></p>';              

But for some weird reason it outputting like this:

      <div>
        <img height="25" src="../mini_logo.png" width="50">
        <h3><a href="viewpost.php?id=25">Pagination Test</a></h3>
        <p>Posted by Patrick Kershner on Apr 19th 2017 21:44:43</p>
        <p></p>
        <p>test Pagin</p>
        <p></p>
        <p align="right"><a class="btn btn-primary btn-md active" href="viewpost.php?id=25" role="button">Read More <span class="glyphicon glyphicon-arrow-right"></span></a></p>
    </div>
                    echo '</div><hr>';
    <hr>
    <div>

        <img height="25" src="../mini_logo.png" width="50">
        <h3><a href="viewpost.php?id=24">This is legit!</a></h3>
                }
        <p>Posted by Patrick Kershner on Apr 19th 2017 21:35:28</p>
        <p></p>
                echo '<nav aria-label="Page navigation">';
        <p>This is actually how we do it!</p>
        <p></p>
                $rows=mysqli_num_rows(mysqli_query($bd,"select * from blog_posts"));
        <p align="right"><a class="btn btn-primary btn-md active" href="viewpost.php?id=24" role="button">Read More <span class="glyphicon glyphicon-arrow-right"></span></a></p>
    </div>
                $total=ceil($rows/$limit);
    <hr>
    <div>

        <img height="25" src="../mini_logo.png" width="50">
        <h3><a href="viewpost.php?id=22">Do we work on iMac?</a></h3>
if($id>1)
        <p>Posted by Patrick Kershner on Apr 5th 2017 21:31:01</p>
        <p></p>
{
        <p>This should answer that question</p>
        <p></p>
echo "<li><a href='?id=".($id-1)."' ><span aria-hidden=\"true\">&laquo;</span></a></li>";           <p align="right"><a class="btn btn-primary btn-md active" href="viewpost.php?id=22" role="button">Read More <span class="glyphicon glyphicon-arrow-right"></span></a></p>
    </div>
    <hr>
    <nav aria-label="Page navigation">
        <ul class="pagination">
            <li>
                <a href='?id=1'>1</a>
            </li>
            <li>
                <a href='?id=2'>2</a>
            </li>
            <li>
                <a href='?id=1'><span aria-hidden="true">&raquo;</span></a>
            </li>
        </ul>
    </nav>

}
echo "<ul class=\"pagination\">";
for($i=1;$i<=$total;$i++)
{
if($i==$id) { echo "<li>".$i."</li>"; }

else { echo "<li><a href='?id=".$i."'>".$i."</a></li>"; }
}
if($id!=$total)
{
echo "<li><a href='?id=".($id+1)."'><span aria-hidden=\"true\">&raquo;</span></a></li>";
}
echo "</ul>";

echo '</nav>';
?>

But for some weird reason it outputting like above

and its throwing off the format, you can see a test of it at https://www.tekcomsolutions.com

I am not understanding what is going on since i have the CSS and everything like its supposted to be on http://getbootstrap.com/components/#pagination

thanks in advance.

Nevermind, I fixed it, I didnt add the LI tags to the number elements, so it was messing it all up. this discussion is now solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.