动态页面上的PHP分页无法正常工作

时间:2015-10-15 12:44:46

标签: php mysql

您好我正在尝试在动态页面上进行分页。 我在我的脚本中使用PDO。我想要包含的分页脚本是mysql而不是PDO所以我将mysql函数更改为PDO函数。但我的问题是使用mysql的脚本和pdo没有的脚本。如果我在PDO脚本中单击下一页或第2页,我也会得到所有前10条记录,但我无法转到上一页。在我的mysql脚本中它可以正常工作!

我在Stackoverflow和互联网上搜索了一个解决方案,但我找不到一个,所以有人知道我做错了什么吗?或者我如何解决这个问题?

我启用了错误处理。我从PDO脚本中得到的唯一错误信息是:

Notice: Undefined index: page on line 22

但是当我点击下一步时,我不再收到此错误消息。

我的PDO脚本:

<?php
    include('php/loginHandlerconnect.php'); 

    $db = loginHandlerconnect();

    ini_set('display_errors',1);
    ini_set('display_startup_errors',1);
    error_reporting(-1);

    $tableName="vacature";      
    $targetpage = "testpaging3.php";
    $limit = 10; 

    $query = "SELECT COUNT(vacatureID) AS num FROM $tableName";
    $stmt = $db->prepare($query);
    $stmt->execute();
    $total_pages = $stmt->fetch(PDO::FETCH_ASSOC);
    $total_pages = $total_pages['num'];
    var_dump($total_pages); // output: string(2) "11" > after clicking on next: string(2) "11" string(1) "2"

    $stages = 3;
    var_dump($_GET['page']); // output: NULL
    $page = (isset($_GET['page']));
    if($page){
        $start = ($page - 1) * $limit; 
    }else{
        $start = 0; 
    }   

    // Get page data
    $query1 = "SELECT vacature.*, werkgever.image, werkgever.plaats, werkgever.provincie, werkgever.naamBedrijf FROM vacature LEFT JOIN werkgever ON (vacature.werkgeverID = werkgever.werkgeverID) ORDER BY vacature.datum DESC LIMIT $start, $limit";
    $result = $db->prepare($query1);
    $result->execute();
    $results = $result->fetchAll();

    // 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'>";
        // Previous
        if ($page > 1){
            $paginate.= "<a href='$targetpage?page=$prev'>previous</a>";
        }else{
            $paginate.= "<span class='disabled'>previous</span>";   }

        // Pages    
        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?page=$counter'>$counter</a>";}                    
            }
        }
        elseif($lastpage > 5 + ($stages * 2))   // Enough pages to hide a few?
        {
            // Beginning only hide later pages
            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?page=$counter'>$counter</a>";}                    
                }
                $paginate.= "...";
                $paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>";
                $paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";       
            }
            // Middle hide some front and some back
            elseif($lastpage - ($stages * 2) > $page && $page > ($stages * 2))
            {
                $paginate.= "<a href='$targetpage?page=1'>1</a>";
                $paginate.= "<a href='$targetpage?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?page=$counter'>$counter</a>";}                    
                }
                $paginate.= "...";
                $paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>";
                $paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";       
            }
            // End only hide early pages
            else
            {
                $paginate.= "<a href='$targetpage?page=1'>1</a>";
                $paginate.= "<a href='$targetpage?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?page=$counter'>$counter</a>";}                    
                }
            }
        }

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

        $paginate.= "</div>";       


}
 echo $total_pages.' Results'; // output: 11 Results



        foreach($results as $row)
        {
            $date = date("d-m-Y", strtotime($row['datum'])); 
            echo "
                <div class='vacatureinfo2'> 
                    <img class='userimg2' src='../../uploads/userimage/". $row['image'] ."' id='imge'/> 
                    <p class='func-br-pl pull-left'>". $row['functie'] ." - ". $row['naamBedrijf'] ." - ". $row['plaats'] ."</p><p class='dtm pull-right'>". $date ."</p> 
                    <div class='omschrijvingkort2'> 
                        ". $row['omschrijvingKort'] ." 
                    </div> 
                    <p class='pull-right'><a href='reactie?id=" . $row['vacatureID'] . "'>Reageer</a> | <a href='informatie-vacature?id=" . $row['vacatureID'] . "'>Meer informatie</a></p> 
                </div> 
                <hr>";
        }
 // pagination
 echo $paginate; // output: previous12next (previous and 1 are not working after clicking on next or 2)
?>

我的mysql脚本:

<?php
    include('connect.php'); 

    ini_set('display_errors',1);
    ini_set('display_startup_errors',1);
    error_reporting(-1);

    $tableName="vacature";      
    $targetpage = "testpaging2.php";    
    $limit = 10; 

    $query = "SELECT COUNT(vacatureID) as num FROM $tableName";
    $total_pages = mysql_fetch_array(mysql_query($query));
    $total_pages = $total_pages['num'];
    var_dump($total_pages); // output: string(2) "11" > after clicking on next: string(2) "11" string(1) "2"

    $stages = 3;
    var_dump($_GET['page']); // output: NULL
    $page = mysql_escape_string($_GET['page']);
    if($page){
        $start = ($page - 1) * $limit; 
    }else{
        $start = 0; 
    }   

    // Get page data
    $query1 = "SELECT vacature.*, werkgever.image, werkgever.plaats, werkgever.provincie, werkgever.naamBedrijf FROM vacature LEFT JOIN werkgever ON (vacature.werkgeverID = werkgever.werkgeverID) ORDER BY vacature.datum DESC LIMIT $start, $limit";
    $result = mysql_query($query1);

    // 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'>";
        // Previous
        if ($page > 1){
            $paginate.= "<a href='$targetpage?page=$prev'>previous</a>";
        }else{
            $paginate.= "<span class='disabled'>previous</span>";   }

        // Pages    
        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?page=$counter'>$counter</a>";}                    
            }
        }
        elseif($lastpage > 5 + ($stages * 2))   // Enough pages to hide a few?
        {
            // Beginning only hide later pages
            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?page=$counter'>$counter</a>";}                    
                }
                $paginate.= "...";
                $paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>";
                $paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";       
            }
            // Middle hide some front and some back
            elseif($lastpage - ($stages * 2) > $page && $page > ($stages * 2))
            {
                $paginate.= "<a href='$targetpage?page=1'>1</a>";
                $paginate.= "<a href='$targetpage?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?page=$counter'>$counter</a>";}                    
                }
                $paginate.= "...";
                $paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>";
                $paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";       
            }
            // End only hide early pages
            else
            {
                $paginate.= "<a href='$targetpage?page=1'>1</a>";
                $paginate.= "<a href='$targetpage?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?page=$counter'>$counter</a>";}                    
                }
            }
        }

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

        $paginate.= "</div>";       


}
 echo $total_pages.' Results'; // output: 11 Results



        while($row = mysql_fetch_array($result))
        {
        $date = date("d-m-Y", strtotime($row['datum'])); 
        echo "
            <div class='vacatureinfo2'> 
                <img class='userimg2' src='../../uploads/userimage/". $row['image'] ."' id='imge'/> 
                <p class='func-br-pl pull-left'>". $row['functie'] ." - ". $row['naamBedrijf'] ." - ". $row['plaats'] ."</p><p class='dtm pull-right'>". $date ."</p> 
                <div class='omschrijvingkort2'> 
                    ". $row['omschrijvingKort'] ." 
                </div> 
                <p class='pull-right'><a href='reactie?id=" . $row['vacatureID'] . "'>Reageer</a> | <a href='informatie-vacature?id=" . $row['vacatureID'] . "'>Meer informatie</a></p> 
            </div> 
            <hr>";

        }
 // pagination
 echo $paginate; // output: previous12next (previous and 1 are working after clicking on next or 2)
?>

2 个答案:

答案 0 :(得分:2)

如果这些问题在SO中几十次都没有得到解答,我会非常惊讶。

首先,这不是错误,这只是一个通知。这意味着$ _GET数组中没有'page'元素,但是你想在var_dump($ _ GET ['page'])函数调用中访问它。

由于此行仅用于调试目的,因此您可以安全地将其注释掉。如果您是第一次加载页面,那么页面参数将不会被设置,因此它不存在就不足为奇了。

在尝试在以下行中使用它之前,您应该使用isset()检查它的存在。

答案 1 :(得分:0)

更改行:

$page = (isset($_GET['page'])); 

以下内容:

$page = $_GET['page']; 

比脚本有效!