2017-05-28 102 views
0

分頁代碼無法正常工作。 在這裏,$ query通過在查詢框中運行給出正確的值,$ per_page = 3,$ page = 1 $ sub是$ query的一部分。 沒有錯誤顯示,頁面鏈接每次發送到空白頁面。分頁不顯示第二頁以後的數據

function pagination($query,$per_page,$page,$sub, $url='?') 
    {   
    $servername = "localhost"; 
    $username = "root"; 
    $password = ""; 
    $dbname = "project"; 
    $conn = mysqli_connect($servername, $username, $password, $dbname); 
    if (!$conn) 
    { 
     die("Connection failed: " . mysqli_connect_error()); 
    } 
      $subject=$sub; 
      $query = "SELECT COUNT(*) as `num` FROM {$query}"; 

      $x1 = mysqli_query($conn,$query); 
      $row = mysqli_fetch_array($x1,MYSQLI_ASSOC); 
      $total = $row['num']; 
      $adjacents = "2"; 

      //$page = ($page == 0 ? 1 : $page); 
      $page = isset($_GET['page']) ? max((int)$_GET['page'], 1) : 1; 
      $start = ($page - 1) * $per_page;        

      $prev = $page - 1;       
      $next = $page + 1; 
      $lastpage = ceil($total/$per_page); 
      $lpm1 = $lastpage - 1; 

      $pagination = ""; 
      if($lastpage > 1) 
      { 
       $pagination .= "<ul class='pagination'>"; 
         $pagination .= "<li class='details' style='margin-top:2px'>Page $page of $lastpage</li>"; 
       if ($lastpage < 7 + ($adjacents * 2)) 
       { 
        for ($counter = 1; $counter <= $lastpage; $counter++) 
        { 
         if ($counter == $page) 
          $pagination.= "<li><a class='current'>$counter</a></li>"; 
         else 
          $pagination.= "<li><a href='{$url}page=$counter'>$counter</a></li>";      
        } 
       } 
       elseif($lastpage > 5 + ($adjacents * 2)) 
       { 
        if($page < 1 + ($adjacents * 2))   
        { 
         for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++) 
         { 
          if ($counter == $page) 
           $pagination.= "<li><a class='current'>$counter</a></li>"; 
          else 
           $pagination.= "<li><a href='{$url}page=$counter'>$counter</a></li>";      
         } 
         $pagination.= "<li class='dot'>...</li>"; 
         $pagination.= "<li><a href='{$url}page=$lpm1'>$lpm1</a></li>"; 
         $pagination.= "<li><a href='{$url}page=$lastpage'>$lastpage</a></li>";  
        } 
        elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2)) 
        { 
         $pagination.= "<li><a href='{$url}page=1'>1</a></li>"; 
         $pagination.= "<li><a href='{$url}page=2'>2</a></li>"; 
         $pagination.= "<li class='dot'>...</li>"; 
         for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++) 
         { 
          if ($counter == $page) 
           $pagination.= "<li><a class='current'>$counter</a></li>"; 
          else 
           $pagination.= "<li><a href='{$url}page=$counter'>$counter</a></li>";      
         } 
         $pagination.= "<li class='dot'>..</li>"; 
         $pagination.= "<li><a href='{$url}page=$lpm1'>$lpm1</a></li>"; 
         $pagination.= "<li><a href='{$url}page=$lastpage'>$lastpage</a></li>";  
        } 
        else 
        { 
         $pagination.= "<li><a href='{$url}page=1'>1</a></li>"; 
         $pagination.= "<li><a href='{$url}page=2'>2</a></li>"; 
         $pagination.= "<li class='dot'>..</li>"; 
         for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++) 
         { 
          if ($counter == $page) 
           $pagination.= "<li><a class='current'>$counter</a></li>"; 
          else 
           $pagination.= "<li><a href='{$url}page=$counter'>$counter</a></li>";      
         } 
        } 
       } 

       if ($page < $counter - 1){ 
        $pagination.= "<li><a href='{$url}page=$next'>Next</a></li>"; 
        $pagination.= "<li><a href='{$url}page=$lastpage'>Last</a></li>"; 
       }else{ 
        $pagination.= "<li><a class='current'>Next</a></li>"; 
        $pagination.= "<li><a class='current'>Last</a></li>"; 
       } 
       $pagination.= "</ul>\n";  

      } 

      return $pagination; 
     } 
    ?> 

回答

0

只需幾個問題開始:

  • 是絕對不會去第二頁? IE瀏覽器。當您點擊進入第二頁時,地址欄中的鏈接是什麼?

  • 或者它會進入第二頁並且不顯示任何數據?

  • 如果它是一個WordPress的網站,你可以打開wp-config文件中的錯誤,看看發生了什麼。

+0

鏈接爲localhost/search.php中?頁= 2的第二頁等等,因爲這是要到第二頁,但不顯示數據進一步頁 。 我正在使用本地xampp服務器,並嘗試使用Mozilla中的Firebug解決它,但沒有顯示錯誤。 –