2012-03-14 2 views
0

나는이 작업을하기 위해 며칠을 보냈습니다. 책 데이터베이스가 있습니다. getBooks라는 함수 파일이 있습니다.복잡한 쿼리에 페이지 매기기 추가

function getBooks($limit = 10000, $sortBy = 'tblAuthor.LastName' , $direction='ASC', $where=1){ 
$db = connect(); 
$fields = array('tblBook.Title', 'tblAuthor.FirstName', 'tblAuthor.LastName', 'tblCategory.Category'); 
$format = 'Select '. implode(', ', $fields) .' FROM tblAuthor INNER JOIN (tblBook INNER JOIN tblCategory ON tblBook.CatID=tblCategory.CatID) ON tblBook.AuthorID=tblAuthor.AuthorID where %1$s ORDER BY %2$s %3$s LIMIT %4$s '; 
$query = sprintf($format, $where, $sortBy, $direction, $limit); 
$escapedQuery = stripslashes($db->realEscape($query)); 
$db->runQuery($escapedQuery); 
if($db->hasErrors()){ 
    print_r($db->getErrors()); 
    echo('<br /><br />Exiting the script.'); 
    die(); 
} 
$results = $db->fillResultset(); 
close($db); 
return $results;  

} // 끝 getbooks ?> 다음의 나머지 부분을 처리하는 라이브러리라고

.

<?php 
    // include header 
    include ("header.php"); 
    include_once('bookFunctions.php'); 
    $books=getBooks(); 
    $myName = "My Books"; 
    ?> 
    <div id="content"> 
    <?php if (count($books)){ 
    ?> 
    <table class="books"> 
    <tr> 
    <th>Book Title</th> 
    <th>Author's Last Name</th> 
    <th>Author's First Name</th> 
    <th>Genre</th> 
    </tr> 
    <?php 
    foreach($books as $book) 
     { 
      echo "<tr>";//start the row 

      //echo out each cell  
      echo "<td>" . $book['Title'] . "</td>"; 
      echo "<td>" . $book['LastName'] . "</td>"; 
      echo "<td>" . $book['FirstName'] . "</td>";    
      echo "<td>" . $book['Category'] . "</td>"; 

      echo "</tr>";//end the row 
     } 
    ?> 

나는 매김 스크립트와 tuts의 모든 종류를 시도했지만 난 그냥 내 쿼리에 삽입 할 위치를 알아낼 수 없습니다. 내가 고민하고있는 최신 하나는 다음과 같습니다. http://stefangabos.ro/php-libraries/zebra-pagination/ 이 작업을 수행하는 방법이 있어야합니다. 모든 조언을 부탁드립니다.

+0

그것은 당신처럼 보인다

<?php $num_per_page = 10; $page = intval($REQUEST['page']); $offset = ($page - 1) * $num_per_page; $sql_limit = "LIMIT $offset, $num_per_page"; 

한 가지 방법은 페이지와 num_per 페이지 PARAMS를 포함하도록 함수 프로토 타입을 수정하는 것입니다 첫 번째 페이지에 대해 'LIMIT start, length', 예를 들어'LIMIT 0,10'과 같은 페이지 매김의 경우에만 한 개의 매개 변수를 제한 -> 전달합니다. 참조 문서는 http://dev.mysql.com/doc/refman/5.5/en/select.html에 있습니다. –

+0

거기에는 제한이 있었으며 데이터베이스 출력을 줄였습니다. 페이지 매김, 페이지가 없습니다. 그래서 원래대로 되돌려 놓았습니다. – Sliloh

답변

1

페이지 당 1과 10 개의 항목이있는 경우 한도는 LIMIT 0, 10이됩니다. 오프셋 0과 길이 10입니다.

+------+--------+ 
| Page | Offset | 
+------+--------+ 
| 1 | 0 | 
| 2 | 10 | 
| 3 | 20 | 
+------+--------+ 

여기 패턴은 offset = (page - 1) * items_per_page입니다. getBooks에 전화를 수정 한 후

function getBooks($sortBy = 'tblAuthor.LastName', $direction='ASC', $where = 1, $page = 1, $num_per_page = 10) { 
    $offset = ($page - 1) * $num_per_page; 
    $db = connect(); 
    $fields = array('tblBook.Title', 'tblAuthor.FirstName', 'tblAuthor.LastName', 'tblCategory.Category'); 
    $format = 'Select '. implode(', ', $fields) .' FROM tblAuthor INNER JOIN (tblBook INNER JOIN tblCategory ON tblBook.CatID=tblCategory.CatID) ON tblBook.AuthorID=tblAuthor.AuthorID where %1$s ORDER BY %2$s %3$s LIMIT %4$d, %5$d '; 
    $query = sprintf($format, $where, $sortBy, $direction, $offset, $num_per_page); 

을 그리고 - -

$books=getBooks('tblAuthor.LastName', 'ASC', null, intval($_REQUEST['page']), 10);