Php 分页并不完美

Php 分页并不完美,php,pagination,Php,Pagination,我有以下代码: $max = 4; $page = isset($_GET['page']) ? ($_GET['page']) : '1'; $init = $page - 1; $init= $max * $init; $strCount = "SELECT COUNT(*) AS 'total_mytable' FROM mytable"; $varstrCount = $crud->viewdatas($strCount); $total = 0; if(cou

我有以下代码:

$max = 4;
$page = isset($_GET['page']) ? ($_GET['page']) : '1'; 
$init = $page - 1;
$init= $max * $init; 
$strCount = "SELECT COUNT(*) AS 'total_mytable' FROM mytable";   
$varstrCount = $crud->viewdatas($strCount);

$total = 0;
    if(count($varstrCount)){
        foreach ($varstrCount as $row) {
            $total = $row["total_mytable"]; 
        }
    }

    $result = "SELECT * FROM mytable ORDER BY id_mytable LIMIT $init,$max";
    $varresult = $crud->viewdatas($result);
网页内容:

    <?php

        if(count($varresult)){
            foreach ($varresult as $res) {                      
    ?>

      <h5><?php echo $res['title'] ?></h5>  

    <?php

            }
        }
    ?>

    <?php
                $max_links = 10;
                $previous = $page - 1; 
                $next = $page + 1; 
                $pgs = ceil($total / $max); 
                if($pgs > 1 ){   

                    if($previous > 0){
                        echo "<li><a href='".BASE_URL."/category/$previous' aria-label='Previous'><span aria-hidden='true'>&laquo;</span></a></li>";
                    } else{

                    }   

                        for($i=$page-$max_links; $i <= $pgs-1; $i++) {
                            if ($i <= 0){
                                }else{
                                                                    if($i != $page{
                                    if($i == $pgs){ //if end insert 3 dots
                                        echo "<li><a href='".BASE_URL."/category/".($i)."'>".$i."</a></li> ..."; 
                                    }else{
                                        echo "<li><a href='".BASE_URL."/category/".($i)."'>".$i."</a></li>"; 
                                    }
                                } else{
                                    if($i == $pgs){ //if end insert 3 dots
                                        echo "<li>".$i."</li> ..."; 
                                    }else{
                                        echo "<li>".$i."</li>";
                                    }
                                } 
                            }
                        }
                if($next <= $pgs){


                        echo "<li><a href='".BASE_URL."/category/$next' aria-label='Next'><span aria-hidden='true'>&raquo;</span></a></li>";


                    }else{

                    }

                }

                ?>  


谢谢@ameenulla0007是如此简单,我插入
  • 它解决了按钮的问题。但是最后一个数字在我得到它之前还没有出现<代码>($i=$page-$max\u links;$i
    echo "<li>".$i."</li>";
    
    echo "<li><a>".$i."</a></li>"; //in your else part