Announcement

Collapse
No announcement yet.

PHP - MySQLi and Bootstrap 3 pagination

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • PHP - MySQLi and Bootstrap 3 pagination

    Hello,

    I would like to show a table and have the bootstrap pagination buttons :
    HTML Code:
    <nav>
    <ul class="pagination">
    <li class="disabled"><a href="#" aria-label="Previous"><span aria-hidden="true">&laquo;</span></a></li>
    <li class="active"><a href="#">1 <span class="sr-only">(current)</span></a></li>
    <li class="active"><a href="#">1 <span class="sr-only">(current)</span></a></li>
    <li class="active"><a href="#">1 <span class="sr-only">(current)</span></a></li>
    <li class="active"><a href="#">1 <span class="sr-only">(current)</span></a></li>
    <li class="active"><a href="#">1 <span class="sr-only">(current)</span></a></li>
    </ul>
    </nav>
    How to put this together by using PHP and mysqli.

    Thank you!
    Last edited by Jesse_S; 24-08-15, 01:21 PM.

  • #2
    Hi Jesse,

    Take a look at this line by line tutorial:

    https://www.developphp.com/video/PHP...sults-Tutorial

    Some of the important parts to look for:

    Count total rows:
    Code:
    // This first query is just to get the total count of rows
    $sql = "SELECT COUNT(id) FROM testimonials WHERE approved='1'";
    $query = mysqli_query($db_conx, $sql);
    $row = mysqli_fetch_row($query);
    Limit query based on the current page:
    Code:
    // This sets the range of rows to query for the chosen $pagenum
    $limit = 'LIMIT ' .($pagenum - 1) * $page_rows .',' .$page_rows;
    // This is your query again, it is for grabbing just one page worth of rows by applying $limit
    $sql = "SELECT id, firstname, lastname, datemade FROM testimonials WHERE approved='1' ORDER BY id DESC $limit";
    Generate the pagination controls html:
    Code:
    // Establish the $paginationCtrls variable$paginationCtrls = '';
    // If there is more than 1 page worth of results
    if($last != 1){
        /* First we check if we are on page one. If we are then we don't need a link to 
           the previous page or the first page so we do nothing. If we aren't then we
           generate links to the first page, and to the previous page. */
        if ($pagenum > 1) {
            $previous = $pagenum - 1;
            $paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$previous.'">Previous</a> &nbsp; &nbsp; ';
            // Render clickable number links that should appear on the left of the target page number
            for($i = $pagenum-4; $i < $pagenum; $i++){
                if($i > 0){
                    $paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a> &nbsp; ';
                }
            }
        }
        // Render the target page number, but without it being a link
        $paginationCtrls .= ''.$pagenum.' &nbsp; ';
        // Render clickable number links that should appear on the right of the target page number
        for($i = $pagenum+1; $i <= $last; $i++){
            $paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a> &nbsp; ';
            if($i >= $pagenum+4){
                break;
            }
        }
        // This does the same as above, only checking if we are on the last page, and then generating the "Next"
        if ($pagenum != $last) {
            $next = $pagenum + 1;
            $paginationCtrls .= ' &nbsp; &nbsp; <a href="'.$_SERVER['PHP_SELF'].'?pn='.$next.'">Next</a> ';
        }
    }
    Hope it works.

    Comment


    • #3
      Doing that will count children comments as parents and cut them at 20 for example. I've tried that first. And if i set in $comments query parent_id = 0 it will remove all childrens and show parents only heh.








      Comment

      Working...
      X