3
votes

Just have a simple question. This is a pagination code that use in one of my websites. i just want to make this more simple. Remove all page numbers and just keep "Next" and "Previous" links.

Can anyone point me out what part of the script that i need to remove to get rid of page numbers. Really appreciate your help.

<?php
error_reporting(E_ALL ^ E_NOTICE);
// How many adjacent pages should be shown on each side?
    $adjacents = 5;

    $query = $mysqli->query("select COUNT(*) as num from posts order by id  desc");
    $total_pages = mysqli_fetch_array($query);
    $total_pages = $total_pages['num'];

    $limit = 12;                                //how many items to show per page
    $page = $_GET['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. */
    $result = $mysqli->query("select * from posts order by id  desc LIMIT $start, $limit");

    /* 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

    $pagination = "";
    if($lastpage > 1)
    {   
        $pagination .= "<div class=\"pagination\">";
        //previous button
        if ($page > 1) 
            $pagination.= "<a href=\"videos-$prev.html\">« previous</a>";
        else
            $pagination.= "<span class=\"disabled\">« previous</span>"; 

        //pages 
        if ($lastpage < 7 + ($adjacents * 2))   //not enough pages to bother breaking it up
        {   
            for ($counter = 1; $counter <= $lastpage; $counter++)
            {
                if ($counter == $page)
                    $pagination.= "<span class=\"current\">$counter</span>";
                else
                    $pagination.= "<a href=\"videos-$counter.html\">$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.= "<span class=\"current\">$counter</span>";
                    else
                        $pagination.= "<a href=\"videos-$counter.html\">$counter</a>";                  
                }
                $pagination.= "...";
                $pagination.= "<a href=\"videos-$lpm1.html\">$lpm1</a>";
                $pagination.= "<a href=\"videos-$lastpage.html\">$lastpage</a>";        
            }
            //in middle; hide some front and some back
            elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
            {
                $pagination.= "<a href=\"videos-1.html\">1</a>";
                $pagination.= "<a href=\"videos-2.html\">2</a>";
                $pagination.= "...";
                for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
                {
                    if ($counter == $page)
                        $pagination.= "<span class=\"current\">$counter</span>";
                    else
                        $pagination.= "<a href=\"videos-$counter.html\">$counter</a>";                  
                }
                $pagination.= "...";
                $pagination.= "<a href=\"videos-$lpm1.html\">$lpm1</a>";
                $pagination.= "<a href=\"videos-$lastpage.html\">$lastpage</a>";        
            }
            //close to end; only hide early pages
            else
            {
                $pagination.= "<a href=\"videos-1.html\">1</a>";
                $pagination.= "<a href=\"videos-2.html\">2</a>";
                $pagination.= "...";
                for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
                {
                    if ($counter == $page)
                        $pagination.= "<span class=\"current\">$counter</span>";
                    else
                        $pagination.= "<a href=\"videos-$counter.html\">$counter</a>";                  
                }
            }
        }

        //next button
        if ($page < $counter - 1) 
            $pagination.= "<a href=\"videos-$next.html\">next »</a>";
        else
            $pagination.= "<span class=\"disabled\">next »</span>";
        $pagination.= "</div>\n";       
    }

    $q = $mysqli->query("select * from posts order by id desc limit $start,$limit");


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


// LOOP Code


}  echo $pagination;?>
3

3 Answers

6
votes
<?php
error_reporting(E_ALL ^ E_NOTICE);
// How many adjacent pages should be shown on each side?
    $adjacents = 5;

    $query = $mysqli->query("select COUNT(*) as num from posts order by id  desc");
    $total_pages = mysqli_fetch_array($query);
    $total_pages = $total_pages['num'];

    $limit = 12;                                //how many items to show per page
    $page = $_GET['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 
    /* Get data. */
    $result = $mysqli->query("select * from posts order by id  desc LIMIT $start, $limit");

    /* 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

    $pagination = "";
    if($lastpage > 1)
    {   
        $pagination .= "<div class=\"pagination\">";
        //previous button
        if ($page > 1) 
            $pagination.= "<a href=\"videos-$prev.html\">« previous</a>";
        else
            $pagination.= "<span class=\"disabled\">« previous</span>"; 

        //next button
        if ($page < $lastpage) 
            $pagination.= "<a href=\"videos-$next.html\">next »</a>";
        else
            $pagination.= "<span class=\"disabled\">next »</span>";
        $pagination.= "</div>\n";       
    }

    $q = $mysqli->query("select * from posts order by id desc limit $start,$limit");


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


// LOOP Code


}  echo $pagination;?>
5
votes

Check out the below code, shortest php pagination script

<?php
$totalpage      = 10;
$currentpage    = (isset($_GET['page']) ? $_GET['page'] : 1);
$firstpage      = 1;
$lastpage       = $totalpage;
$loopcounter = ( ( ( $currentpage + 2 ) <= $lastpage ) ? ( $currentpage + 2 ) : $lastpage );
$startCounter =  ( ( ( $currentpage - 2 ) >= 3 ) ? ( $currentpage - 2 ) : 1 );

if($totalpage > 1)
                {
                    $pagination .= '<ul class="paginate" id="paginate">';
                    $pagination .= '<li><a  href="http://magento13.localhost.com/pagination.php?page=1" class="paginate_click" id="1-page">First</a></li>';
                    for($i = $startCounter; $i <= $loopcounter; $i++)
                    {

                        $pagination .= '<li><a  href="http://magento13.localhost.com/pagination.php?page='.$i.'">'.$i.'</a></li>';
                    }
                    $pagination .= '<li><a href="http://magento13.localhost.com/pagination.php?page='.$totalpage.'" class="paginate_click" id="'.$totalpage.'-page">Last</a></li>';
                    $pagination .= '</ul>';
                }
echo $pagination;

?>
0
votes
<table id="datatable" class="table table-striped table-bordered">
<thead>
    <tr >
        <th>Si No</th>                       
        <th>Staff Name</th>
        <!-- <th>Department</th> -->
        <th>Course</th>
        <!--<th>Email</th>-->
        <th width="15%">Actions</th>
    </tr>
</thead>
<tbody>
    <?php
    if (isset($_POST["skip"])) {
        $skip = $_POST["skip"];
        $limit = 10;
        $query = mysqli_query($con, "SELECT * from staff_info order by id asc LIMIT $skip,$limit");
        $count = mysqli_num_rows($query);
        if ($count > 0) {
            $i = 1;
            while ($rows = mysqli_fetch_array($query)) {

                // Department Info
                $depart_sel_str = "SELECT * from departments where id = '" . $rows['staff_department_id'] . "' ";
                $depart_sel_qry = mysqli_query($con, $depart_sel_str);
                $depart_res = mysqli_fetch_array($depart_sel_qry);

                // Subject Info
                $course_sel_str = "SELECT * from course_details where id = '" . $rows['staff_course_id'] . "' ";
                $course_sel_qry = mysqli_query($con, $course_sel_str);
                $course_res = mysqli_fetch_array($course_sel_qry);
                ?>
                <tr>
                    <th><?php echo $i; ?></th>
                    <th><?php echo $rows['staff_name']; ?></th>
                    <!-- <th> <?php echo $depart_res['department_name']; ?></th> -->
                    <th><?php echo $course_res['course_name']; ?></th>
                    <th class="text-center"><a href="add_staff.php?edit_id=<?php echo $rows['id']; ?>" class="btn action_icon"><img src="images/edit_icon.png" alt="Edit" title="Edit" /></a> <a href="staff_management.php?del_id=<?php echo $rows['id']; ?>" class="btn action_icon"><img src="images/del_icon.png" alt="Delete" title="Delete" /></a></th>
                </tr>
                <?php
                $i++;
            }
        } else {
            echo '<th colspan="7">No records found.</th>';
        }
    } else {
        $skip = 0;
        $limit = 10;
        $query = mysqli_query($con, "SELECT * from staff_info order by id asc LIMIT $skip,$limit");
        $count = mysqli_num_rows($query);
        if ($count > 0) {
            $i = 1;
            while ($rows = mysqli_fetch_array($query)) {

                // Department Info
                $depart_sel_str = "SELECT * from departments where id = '" . $rows['staff_department_id'] . "' ";
                $depart_sel_qry = mysqli_query($con, $depart_sel_str);
                $depart_res = mysqli_fetch_array($depart_sel_qry);

                // Subject Info
                $course_sel_str = "SELECT * from course_details where id = '" . $rows['staff_course_id'] . "' ";
                $course_sel_qry = mysqli_query($con, $course_sel_str);
                $course_res = mysqli_fetch_array($course_sel_qry);

                //echo "<tr><th>" . $i . "</th><th>" . $rows['staff_name'] . "</th><th>" . $depart_res['department_name'] . "</th><th>" . $course_res['course_name'] . "</th><th class='text-center'><a href='add_staff.php?edit_id=" . $rows['id'] . "' class='btn action_icon'><img src='images/edit_icon.png' alt='Edit' title='Edit' /></a> <a href='staff_management.php?del_id=" . $rows['id'] . "' class='btn action_icon'><img src='images/del_icon.png' alt='Delete' title='Delete' /></a></th></tr>";
                echo "<tr><th>" . $i . "</th><th>" . $rows['staff_name'] . "</th><th>" . $course_res['course_name'] . "</th><th class='text-center'><a href='add_staff.php?edit_id=" . $rows['id'] . "' class='btn action_icon'><img src='images/edit_icon.png' alt='Edit' title='Edit' /></a> <a href='staff_management.php?del_id=" . $rows['id'] . "' class='btn action_icon'><img src='images/del_icon.png' alt='Delete' title='Delete' /></a></th></tr>";
                $i++;
            }
        } else {
            echo '<th colspan="7">No records found.</th>';
        }
    }
    ?>
</tbody>

<ul class="pagination staff_details_Pagination ">
    <?php
    $page_count = 10;
    $pagination_limit = 10;
    $leftCenter = $pagination_limit / 2;
    $rightCenter = $leftCenter - 1;
    $limit_start = 1;
    $current_page_number = $skip / $page_count;
    $current_page_number = $current_page_number + 1;
    if ($current_page_number >= $pagination_limit) {
        if ($count > (($current_page_number + $rightCenter) * $page_count) || ($current_page_number > $leftCenter && $current_page_number > $rightCenter)) {
            $limit_start = $current_page_number - $leftCenter;
        } else {
            $limit_start = ($current_page_number - $pagination_limit) + 1;
        }
        $pagination_limit = $limit_start + ($pagination_limit - 1);
    }
    $initial_display_records = ($page_count * $pagination_limit);
    ?>
    <?php if ($skip != 0) { ?>
        <li class="pages">
            <a href="#" aria-label="Previous" data-skip="<?php echo ((int) $skip - $page_count) ?>" >
                <span aria-hidden="true">&laquo;</span>
            </a>
        </li>
    <?php } ?>
    <?php for ($i = ($limit_start - 1) * $page_count; $i < $count && $limit_start <= $pagination_limit; $i += $page_count) { ?>


        <li class="pages  <?php if ($skip == $i) { ?>  active <?php } ?>"><a href="#" class="page " data-skip="<?php echo $i ?>" > <?php echo $limit_start ?> </a></li>
        <?php $limit_start += 1; ?>
    <?php } ?>

    <?php if ($count > ($page_count * $current_page_number)) { ?>
        <li class="pages">
            <a href="#" aria-label="Next" data-skip="<?php echo ((int) $skip + $page_count) ?>">
                <span aria-hidden="true">&raquo;</span>
            </a>
        </li>
    <?php } ?>
</ul>