PHP分页集成脚本在输出中不显示任何页面

时间:2018-08-01 06:20:33

标签: php mysql pagination

问候Stackoverflow,

关于执行有效的分页脚本,我是一个完全的新手。

在这里,我有一个php脚本,该脚本从mysql表中取出数据并将其显示在网页上,该网页一直运行良好,直到我决定对其添加分页。

除未定义的's'和'page'外,没有其他错误,但是我认为更大的问题是$ limit,因为大于71的数字会使未定义的错误消失(因为sqltable包含70个条目)。因此,基本上,我很乐意从您的专家那里获得一些帮助,以找出脚本中的错误是什么,以至于它在输出中不显示“页面”。

这可能是您需要的一切:

    $sql_conn = mysqli_connect( $sql_host, $sql_user, $sql_pass,$sql_db);
$zone="-14400"; //USA Time Zone
$targetpage = "advanced_bans.php";
$limit = 40;  // Entries Showed per page

// Start Pagination
$sql_query = "SELECT COUNT(*) as num FROM $stable";
$sql_res=mysqli_query($sql_conn,$sql_query);
$total_pages = mysqli_fetch_array($sql_res,MYSQLI_NUM);
#the number of pages
foreach ($sql_res as $res) {
    $num=$res['num'];
}
$total_pages=$num;

$stages = 3;
$start = 0;
$page = mysqli_real_escape_string($sql_conn , $_GET['page']);
if($page){
    $start = ($page - 1) * $limit;
}else{
    $start = 0;
}  
// Get page data

// Initial page num setup
if ($page == 0){$page = 1;}
$prev = $page - 1;
$next = $page + 1;                        
$lastpage = ceil($total_pages/$limit);    
$LastPagem1 = $lastpage - 1;                  


$paginate = '';
if($lastpage > 1)
{  



    $paginate .= "<div class='paginate'>";
    if ($page > 1){
        $paginate.= "<a href='$targetpage?s=".$_GET['s']."&page=$prev'>previous</a>";
    }else{
        $paginate.= "<span class='disabled'>previous</span>";   }


    if ($lastpage < 7 + ($stages * 2))  // Not enough pages to breaking it up
    {  
        for ($counter = 1; $counter <= $lastpage; $counter++)
        {
            if ($counter == $page){
                $paginate.= "<span class='current'>$counter</span>";
            }else{
                $paginate.= "<a href='$targetpage?s=".$_GET['s']."&page=$counter'>$counter</a>";}                  
        }
    }
    elseif($lastpage > 5 + ($stages * 2))   // Enough pages to hide a few?
    {
        if($page < 1 + ($stages * 2))      
        {
            for ($counter = 1; $counter < 4 + ($stages * 2); $counter++)
            {
                if ($counter == $page){
                    $paginate.= "<span class='current'>$counter</span>";
                }else{
                    $paginate.= "<a href='$targetpage?s=".$_GET['s']."&page=$counter'>$counter</a>";}                  
            }
            $paginate.= "...";
            $paginate.= "<a href='$targetpage?s=".$_GET['s']."&page=$LastPagem1'>$LastPagem1</a>";
            $paginate.= "<a href='$targetpage?s=".$_GET['s']."&page=$lastpage'>$lastpage</a>";    
        }
        elseif($lastpage - ($stages * 2) > $page && $page > ($stages * 2))
        {
            $paginate.= "<a href='$targetpage?s=".$_GET['s']."&page=1'>1</a>";
            $paginate.= "<a href='$targetpage?s=".$_GET['s']."&page=2'>2</a>";
            $paginate.= "...";
            for ($counter = $page - $stages; $counter <= $page + $stages; $counter++)
            {
                if ($counter == $page){
                    $paginate.= "<span class='current'>$counter</span>";
                }else{
                    $paginate.= "<a href='$targetpage?s=".$_GET['s']."&page=$counter'>$counter</a>";}                  
            }
            $paginate.= "...";
            $paginate.= "<a href='$targetpage?s=".$_GET['s']."&page=$LastPagem1'>$LastPagem1</a>";
            $paginate.= "<a href='$targetpage?s=".$_GET['s']."&page=$lastpage'>$lastpage</a>";    
        }
        else
        {
            $paginate.= "<a href='$targetpage?s=".$_GET['s']."&page=1'>1</a>";
            $paginate.= "<a href='$targetpage?s=".$_GET['s']."&page=2'>2</a>";
            $paginate.= "...";
            for ($counter = $lastpage - (2 + ($stages * 2)); $counter <= $lastpage; $counter++)
            {
                if ($counter == $page){
                    $paginate.= "<span class='current'>$counter</span>";
                }else{
                    $paginate.= "<a href='$targetpage?s=".$_GET['s']."&page=$counter'>$counter</a>";}                  
            }
        }
    }

    // Next
    if ($page < $counter - 1){
        $paginate.= "<a href='$targetpage?s=".$_GET['s']."&page=$next'>next</a>";
    }else{
        $paginate.= "<span class='disabled'>next</span>";
    }

    $paginate.= "</div>";   } 

希望,我要从中学到一些要记住的东西,谢谢和问候, ZEDD

0 个答案:

没有答案