php 只有下一个和上一个按钮的简单分页

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21644736/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:19:08  来源:igfitidea点击:

Simple pagination with only next and previous buttons

phphtmlmysqlipagination

提问by zack

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;?>

回答by Jeroen de Lau

<?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;?>

回答by Dasarathi Swain

Check out the below code, shortest php pagination script

查看下面的代码,最短的php分页脚本

<?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;

?>

回答by Riya

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