2011-06-13 3 views
-1

* 편집 *** 나는 매김 쿼리에서 일부 변수 방송 전파를 가져옵니다 마지막으로 작동하도록 페이지 매김을 입수했습니다 주요 쿼리를 결합하여 큰 진전했습니다매김 도움말

. 유일한 문제는 이제 첫 번째 페이지가 비어 있다는 것입니다 페이지 두 사람은 한계가 쿼리 결과 다음 41-80

함께 시작 정확하고 공중파

 $rowsperpage = 40; 
     $currentpage = (int) $_GET['currentpage']; 
     $offset = ($currentpage - 1) * $rowsperpage; 
     $query = "SELECT * FROM `CysticAirwaves` WHERE `FromUserID` = `ToUserID` AND `status` = 'active' ORDER BY `date` DESC, `time` DESC LIMIT $offset, $rowsperpage" ; 
     $request = mysql_query($query,$connection); 
     $counter = 0; 
     while($result = mysql_fetch_array($request)) { 

를 가져옵니다 여기 매김 코드입니다 :

$query = "SELECT COUNT(*) FROM `CysticAirwaves`"; 
         $result = mysql_query($query, $connection) or trigger_error("SQL", E_USER_ERROR); 
         $r = mysql_fetch_row($result); 
         $numrows = $r[0]; 

         // number of rows to show per page 
         $rowsperpage = 40; 
         // find out total pages 
         $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; 

         // get the info from the db 
         $query2 = "SELECT `id` FROM `CysticAirwaves` LIMIT $offset, $rowsperpage"; 
         $result = mysql_query($query2, $connection) or trigger_error("SQL", E_USER_ERROR); 

         // while there are rows to be fetched... 
         while ($list = mysql_fetch_assoc($result)) { 
          // echo data 
          echo $list['id'] . " : " . $list['number'] . "<br />"; 
         } // end while 

         /****** build the pagination links ******/ 
         // range of num links to show 
         $range = 3; 

         // if not on page 1, don't show back links 
         if ($currentpage > 1) { 
          // show << link to go back to page 1 
          echo " <a href='http://www.cysticlife.org/Airwave_build.php?currentpage=1'><<</a> "; 
          // get previous page num 
          $prevpage = $currentpage - 1; 
          // show < link to go back to 1 page 
          echo " <a href='http://www.cysticlife.org/Airwave_build.php?currentpage=$prevpage'><</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='http://www.cysticlife.org/Airwave_build.php?currentpage=$x'>$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='http://www.cysticlife.org/Airwave_build.php?currentpage=$nextpage'>></a> "; 
          // echo forward link for lastpage 
          echo " <a href='http://www.cysticlife.org/Airwave_build.php?currentpage=$totalpages'>>></a> "; 
         } // end if 
         /****** end build pagination links ******/ 
         ?> 

짧은, 난 그냥 처음 40 개 결과를 당길 수 모르지만, 그래서 긴 이야기 후 자사의 미세

+3

무엇이 작동하지 않습니까? –

+1

* 정확한 문제 *는 무엇입니까? "[아직도] (http://stackoverflow.com/questions/6287209/how-to-implement-final-steps-in-pagination-project) 할 수 없습니다"는 당신을 도울 너무 애매합니다. – deceze

+0

당신이 쓰는 또 다른 방법입니다. 이전의 것은 무엇 이었습니까? 그리고 실제로 당신의 문제는 무엇입니까? 당신은 방금 코드를 작성하는 데 문제가 있다고 말했기 때문에 코더를 찾으십니까? – hakre

답변

2

이 paginat에 대해 갈 수있는 더 좋은 방법입니다 이온 :

SELECT FOUND_ROWS() 

이 :

SELECT SQL_CALC_FOUND_ROWS id FROM `CysticAirwaves` LIMIT $offset, $rowsperpage 

이있는 이상 다른 SQL 쿼리를

그런 다음 쿼리 1과 같은 결과를 반환 실행합니다 예제 쿼리를 사용하여 표시 할 데이터를 선택합니다 한도를 사용하지 않고 발견 된 행 수를 반환합니다. 자세한 내용은 여기를 참조하십시오 :이 페이지 매김을 관리 할 수있는 가장 빠른 방법이 될 것입니다

http://dev.mysql.com/doc/refman/5.0/en/limit-optimization.html

.

그러면 첫 번째 쿼리의 결과를 반복하여 표시합니다. 추적 할 필요가있는 것은 페이지의 현재 페이지 수와 페이지 당 표시 할 행 수입니다.

페이지 번호 * $ rowsperpage> 두 번째 선택 결과 (FOUND_ROWS)의 경우 '다음'페이지 옵션/링크를 계산하거나 표시 할 필요가 없습니다.

'다음'페이지의 경우 페이지 번호를 증가 시키거나 다음 페이지 * $ rowsperpage> 총 행을 3 번 반복하십시오.

'이전'페이지의 경우 페이지 번호 또는 이전 페이지를 3 번 ​​감 소하면 1 (이 경우 루프 분리)입니다.

그 트릭을해야합니다.

0

아마도 많은 코드를 작성 했으므로 한 번 작성한 내용을 읽을 수 없습니다. 코드의 각 줄 옆에 주석을 달아주는 것으로 보입니다. 따라서이 문제를 줄이기 위해 코드를 줄여야합니다.

이 그냥 제안, 당신이 쓴된다

 // 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 

이 모든 당신이 처리 할 필요 조항 경우! 이 모든 코멘트는 읽어야합니다! 그게 짐이 아닌가?

 // currentpage must be within 1 and total of pages. 
     $currentpage = max(1,min($totalpages, $currentpage)); 

어때요?이해하는 데 문제가 있다면, 코드 에서처럼 코드를 설명하는 몇 가지 설명을 덧붙일 수 있습니다. 내 의견이 맞지 않는다면, 그것은 단지 제안 일뿐입니다.