2012-03-14 15 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

它看起來像你只傳遞一個參數來限制 - >對於你想要的分頁,「限制開始,長度」,例如第一頁的「限制0,10」。參考文檔在http://dev.mysql.com/doc/refman/5.5/en/select.html – 2012-03-14 23:28:20

+0

我在那裏有一個限制,它很好地縮短了我的數據庫輸出,但因爲我不知道在哪裏插入分頁的東西,它沒有頁面。所以我把它放回原來的位置。 – Sliloh 2012-03-15 00:02:31

回答

1

如果第1頁和每頁10項您的限制類將是LIMIT 0, 10。這是偏移量0和長度10.

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

此處的模式是偏移量=(page-1)* items_per_page。

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

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); 

然後修改您的來電getBooks -

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