2016-10-02 109 views
0

我有一些PHP和HTML代碼從我的數據庫加載結果。它顯示每頁五個結果。假設我有1000頁。所有這些頁面的鏈接都會從屏幕上消失。谷歌有這個問題,但他們通過僅顯示當前鏈接以及5個鏈接和5個鏈接來修復它。我想要做這樣的事情。我不想顯示100個鏈接到各個頁面。假裝用戶在第100頁上。我想要顯示第100頁的鏈接以及95到105的鏈接。我該怎麼做?這是我到目前爲止的代碼:HTML和PHP生成分頁鏈接

$page = $_GET["page"]; 
$pagesQuery = mysql_query("SELECT count(id) FROM(`posts`)"); 
$pageNum = ceil(mysql_result($pagesQuery, 0)/5); 
$start = (($page-1)*5); 

$currentname = mysql_query("SELECT * FROM posts LIMIT $start, 5"); 
while ($row = mysql_fetch_array($currentname)) { 
     //recieve relevant data. 
     $title = $row[0]; 
     $desc = $row[13]; 
     $ID = $row[6]; 
     $views = $row[3]; 
     $user = $row[7]; 
     //fetch the last id from accounts table. 
     $fetchlast1 = mysql_query("SELECT * FROM allaccounts WHERE id=(SELECT MAX(id) FROM allaccounts)"); 
     $lastrow1 = mysql_fetch_row($fetchlast1); 
     $lastid1 = $lastrow1[6]; 
     //acquire the username of postee. 
     for ($i1=1; $i1 <= $lastid1; $i1++) { 
      $currentname1 = mysql_query("SELECT * FROM allaccounts WHERE id=$user"); 
      while ($row1 = mysql_fetch_array($currentname1)) { 
       $username1 = $row1[0]; 
      } 
     } 

     //Format Title, description and view count. 
     $title2 = rtrim($title); 
     $donetitle = str_replace(" ", "-", $title2); 
     $url = "articles/".$ID."/".$donetitle.""; 

     $donetitle = strlen($title) > 40 ? substr($title,0,40)."..." : $title; 
     $donedesc = ''; 

     if(strlen($desc) > 150) { 
      $donedesc = explode("\n", wordwrap($desc, 150)); 
      $donedesc1 = $donedesc[0] . '...';       
     }else{ 
      $donedesc1 = $desc;       
     } 
     $finviews = number_format($views, 0, '.', ','); 

     //Give relevant results 
     if(stripos($title, $terms) !== false || stripos($desc, $terms) !== false || stripos($username1, $terms) !== false){ 
       if($row[10] == null){ 
        $SRC = "img/tempsmall.jpg"; 
       }else{ 
        $SRC ="generateThumbnailSmall.php?id=$ID"; 
       } 
       echo "<div id = \"feature\"> 

         <img src=\"$SRC\" alt = \"article thumbnail\" /> 
         </div> 
         <div id = \"feature2\"> 
          <a href= \"$url\" id = \"titletext\" alt = \"article title\">$donetitle</a> 
          <p id=\"resultuser\" >$username1</p> 
          <p id=\"resultp\">$donedesc1</p> 
          <a href = \"sendflag.php?title=$title&url=$url&id=$ID&userid=$user\" id = \"flag\" alt = \"flag\"><img src=\"img/icons/flag.png\"/></a><b id=\"resultview\">$finviews views</b> 

         </div> 
         <div id = \"border\"></div>"; 
      }     
} 

for ($j=1; $j < $pageNum; $j++) { 
    echo "<a id =\"\" href=\"searchresults.php?search=".$terms."&page=".$j."\">".$j."</a>"; 
} 
+0

使用此通用腳本實現分頁,[https://github.com/rajdeeppaul/Pagination](https:/ /github.com/rajdeeppaul/Pagination) –

+0

不要使用'mysql_ *'函數,它們已被棄用PHP 5.5,並在PHP 7.0中完全刪除。改用'MySQLi'或'PDO'。使用上述腳本,您可以使用'MySQLi'或'PDO'驅動程序輕鬆實現分頁。 –

回答

0

你想要的是可以改變:

for ($j=1; $j < $pageNum; $j++) { 
echo "<a id =\"\" href=\"searchresults.php?search=".$terms."&page=".$j."\">".$j."</a>"; 
} 

在這裏,你列出所有環節附加$ J < $頁次,你想這裏僅列出10:附加$ J < = 10開始$ j = $ currentPage或$ j = $ currentPage - 5如果$ currentPage> 5