2009-06-23 3 views
0

아래 코드는 페이지 매김을 시도한 것입니다. 그것은 효과가 없습니다. 나는 49 개의 행을 가진 결과로 놀고있다. $rowsperpage = 10;을 사용하는 경우 아래 코드는 하단의 4 페이지에 대한 하이퍼 링크를 올바르게 갖습니다. 그러나 49 페이지의 모든 행을 1 페이지에 계속 표시하고 후속 페이지를 클릭하면 빈 페이지가 반환됩니다.PHP - 페이지 매기기는 페이지 수를 수정하지만 행을 나누지 않습니다.

제 질문은 다음과 같습니다. 페이지 1 다음 페이지 사이에 행을 정확하게 나누고 할당하려면 아래 코드를 어떻게 얻습니까? 사전에

감사합니다,

<? 
    //This is only displayed if they have submitted the form 
    if ($searching =="yes") 
    { 

     //If they did not enter a search term we give them an error 
     if ($find == "") 
     { 
      session_write_close(); 
      header("Location:http://www.example.com/index.php"); 
      exit; 
      unset($_SESSION['find']); 

     } 

     // Otherwise we connect to our Database 
     mysql_connect("mysqlv10", "username", "password") or die(mysql_error()); 
     mysql_select_db("database") or die(mysql_error()); 

     // We preform a bit of filtering 

     $find = strip_tags($find); 
     $find = trim ($find); 
     $find = strtolower($find); 
     $find = mysql_real_escape_string($find); 

     $result=mysql_query("SHOW TABLES FROM database LIKE '$find'") 
     or die(mysql_error()); 

     $_GET['result'] = $result; 

     if(mysql_num_rows($result)>0){ 
      while($table=mysql_fetch_row($result)){ 

       $_GET[`$table[0]`] = $table; 
       $_SESSION['table']= $table; 

       $presult = mysql_query("SELECT COUNT(*) FROM `$table[0]`") or die(mysql_error()); 

       $rr = mysql_fetch_row($presult); 
       $numrows = $rr[0]; 
       $rowsperpage = 50; 
       $totalpages = ceil($numrows/$rowsperpage); 

       // get the current page or set a default 
       if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) { 
        // cast var as int 
        $currentpage = (int) $_GET['currentpage']; 
       } else { 
        // default page num 
        $currentpage = 1; 
       } // end if 

       // if current page is greater than total pages... 
       if ($currentpage > $totalpages) { 
        // set current page to last page 
        $currentpage = $totalpages; 
       } // end if 
       // if current page is less than first page... 
       if ($currentpage < 1) { 
        // set current page to first page 
        $currentpage = 1; 
       } // end if 

       // the offset of the list, based on current page 
       $offset = ($currentpage - 1) * $rowsperpage; 

       print "<p class=\"topic\">$table[0]</p>\n"; 
       $r=mysql_query("SELECT * , votes_up - votes_down AS effective_vote FROM `$table[0]` ORDER BY effective_vote DESC"); 

       $_GET[`$r[0]`] = $r; 

       print "<table class=\"navbar\">\n"; 
       while($row=mysql_fetch_array($r)){ 

        $_GET[`$row[0]`] = $row; 
        $_SESSION['row']= $row; 

        $effective_vote = $row['votes_up'] - $row['votes_down']; 

        print "<tr>"; 

        print "<td class='sitename'>".'<a href="http://'.$row['site'].'" class="links2">'.$row['site'].'</a>'."</td>"; 
        print "<td class='votes'>".'<span class="selection_count" id="selection_count'.$row['id'].'">'.number_format($effective_vote).'</span>'."</td>"; 
        print "<td class='ballot'>".'<span class="button" id="button'.$row['id'].'">'.'<a href="javascript:;" class="cell1" id="'.$row['id'].'">'.Select.'</a>'.'</span>'."</td>"; 
       } 
       print "</tr>\n"; 
      } 
      print "</table>\n"; 

      $range = 3; 

      /****** build the pagination links ******/ 
      // range of num links to show  

      // if not on page 1, don't show back links 
      if ($currentpage > 1) { 
       // show << link to go back to page 1 
       echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1&find={$_SESSION['find']}'><<</a> "; 
       // get previous page num 
       $prevpage = $currentpage - 1; 
       // show < link to go back to 1 page 
       echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage&find={$_SESSION['find']}'><</a> "; 
      } // end if 

      // loop to show links to range of pages around current page 
      for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) { 
       // if it's a valid page number... 
       if (($x > 0) && ($x <= $totalpages)) { 
        // if we're on current page... 
        if ($x == $currentpage) { 
        // 'highlight' it but don't make a link 
        echo " [<b>$x</b>] "; 
        // if not current page... 
        } else { 
        // make it a link 
       echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x&find={$_SESSION['find']}'>$x</a> "; 
        } // end else 
       } // end if 
      } // end for 

      // if not on last page, show forward and last page links  
      if ($currentpage != $totalpages) { 
       // get next page 
       $nextpage = $currentpage + 1; 
       // echo forward link for next page 
       echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage&find={$_SESSION['find']}'>></a> "; 
       // echo forward link for lastpage 
       echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages&find={$_SESSION['find']}'>>></a> "; 
      } // end if 
      /****** end build pagination links ******/ 

     } 
     else{ 
      print ""; 
     } 

     //This counts the number or results - and if there wasn't any it gives them a little message explaining that 
     $anymatches=mysql_num_rows($result); 
     if ($anymatches == 0) 
     { 

      $find = urlencode($find); 

      session_write_close(); 
      header("Location:http://www.example.com/search2.php?find=$find"); 
      exit; 

     } 

    } 
    ?> 
+0

같은 것을하고 싶어, 나는 "LIMIT $으로, 오프셋 $의 rowsperpage"추가하고, 이제 페이지 1은 정확한 수의 행과 함께 완벽하게 보입니다. 그러나 다음 페이지를 클릭하면 빈 페이지가됩니다. 왜 그런가? 감사. –

+0

확인에 제한을 사용하는 것입니다

mysql's documentation

+0

내 문제는 $ find가 후속 링크로 전달되지 않는다고 생각합니다. 그러나 나는 이유를 알 수 없다. 어떤 아이디어? –

답변

0

당신은 당신의 테이블에서 특정 항목을 얻을 수 제한을 사용해야합니다.

select * form table limit 5, 10 

쿼리에서 요소 5부터 시작하여 다음 10 개 항목을 반환합니다.

보통 매김을 위해 당신이

select * from table limit (PAGE-1)*ITEMS_PER_PAGE, ITEMS_PER_PAGE 
중요한 것은 당신의 MySQL의 쿼리