2016-10-02 92 views
2

我有一些代碼可以從數據庫中提取所有結果並顯示與用戶搜索相關的結果。我還有一些代碼可以計算項目數量,並根據與用戶搜索相關的項目數量生成一定數量的頁面。問題如下。如果我全部搜索,我的代碼會在11個頁面上顯示數據庫中的所有內容。如果我搜索汽車,它仍然會顯示11頁,但只有2個結果在標題中包含汽車。問題是這些結果顯示在第八頁上,而其他所有頁面都是空白的。在第八頁顯示的標題中搜索汽車所有的兩個結果。搜索全部基於數據庫中項目的順序。這裏是我當前的代碼:HTML和PHP分頁工作不正確

   $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>"; 
         } 






       } 



        $totalPages = $pageNum; 
        $currentPage = $page; 
        $numPagesToShow = 10; 

        if($currentPage > $totalPages) { 
         $currentPage = $totalPages; 
        } 


        if($numPagesToShow >= $totalPages) { 
         $numMaxPageLeft = 1; 
         $numMaxPageRight = $totalPages; 
        } else { 
         $pagesToShow = ceil($numPagesToShow/2); 
         $numMaxPageLeft = $currentPage - $pagesToShow; 
         $numMaxPageRight = $currentPage + $pagesToShow; 

         if($numMaxPageLeft <= 0) { 
          $numMaxPageRight = $numMaxPageRight - $numMaxPageLeft +1; 
          $numMaxPageLeft = 1; 
         } elseif($numMaxPageRight >= $totalPages) { 
          $numMaxPageLeft -= ($numMaxPageRight - $totalPages); 
          $numMaxPageRight = $totalPages; 
         } 
        } 

        for ($i=$numMaxPageLeft; $i<=$numMaxPageRight; $i++) { 
         echo "<a id =\"pagenationlink\" href=\"searchresults.php?search=".$terms."&page=".$i."\">".$i."</a>"; 
        } 

如何,我只與它的兩個結果,而不是與第八頁上的兩個相關結果11頁顯示一個網頁?謝謝

+0

不要取所有記錄。只能獲取相關記錄並與搜索匹配。 –

+0

請勿使用不推薦使用的'mysql_ *'函數。從PHP 5.5開始它們被棄用,並在PHP 7中完全刪除。它們也不安全。改用MySQLi或PDO。 –

+0

解釋如何向我請求 – jack

回答

0

請更新您的代碼如下。
但嘗試使用mysqli_ ()作爲MySQL的()的depricted

$cond = ""; 
if(!empty($_POST["search"])) 
{  
    $cond = " write your search condition " ; 
} 
$start = (($page-1)*5); 
$query = mysql_query("SELECT SQL_CALC_FOUND_ROWS * FROM posts where $cond LIMIT $start, 5"); 
$TotalDataQuery = mysql_query("SELECT FOUND_ROWS() tot;"); 
$rsVal = mysql_fetch_array($pagesQuery); 
$pagesQuery = $rsVal['tot']; 
$pageNum = ceil($pagesQuery/5); 

while ($row = mysql_fetch_array($query)) { 
//continue your code 
}