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

それはあなたのように見えます最初のページで 'LIMIT start、length'、例えば' LIMIT 0,10'としたいページングのために、limit - >に1つのパラメータを渡すだけです。リファレンスドキュメントはhttp://dev.mysql.com/doc/refman/5.5/en/select.html –

+0

私はそこに限界があり、データベースの出力をうまく短縮しましたが、どこにページ区切りのものは、ページがありませんでした。だからオリジナルに戻しました。 – Sliloh

答えて

1

ページあたり1ページと10ページのアイテムがある場合、リミットクラスはLIMIT 0, 10となります。 * items_per_page - それは、ここではパターンは=(1ページ)にオフセットされて0と長さ10

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

オフセットされます。

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