2011-05-24 151 views
0

所以出於某種原因,我試圖找出爲什麼我的分頁無法正常工作。我將它從第1頁移動到第2頁,但由於某種原因它不會轉到第3頁。我檢查了數據庫中的查詢是否正確,並且不確定哪裏出錯。PHP分頁問題

$per_page = '4'; 

$tenure_sql = 'SELECT COUNT(id) as count 
       FROM people.bywu 
       WHERE type <> 0 
       AND status = "approved"'; 

$tenure_query = mysql_query($tenure_sql, DB); 

$tenure_count = mysql_fetch_object($tenure_query); 
$tenure_count = $tenure_count -> count; 
$tenure_pages = ceil($tenure_count/$per_page); 

<div class="pagination" id="tenure_pages"> 
<a href="" class="lt grayed">&lt;</a> 
Stories <span id="tenure_low" class="current_low"><?= $tenure_count ? '1':'0' ?></span>-<span id="tenure_high" class="current_high"><?= $tenure_count > 4 ? $per_page : $tenure_count ?></span> of <span class="total"><?= $tenure_count ?></span> 
<a href="" class="gt<?= $tenure_count < 5 ? ' grayed':'' ?>">&gt;</a> 
<span class="pages" style="display:none;"><?= $tenure_pages ?></span> 
<? 
    for($i = 1; $i < $tenure_pages + 1; $i++) 
    { 
    echo '<a href="">' . $i . '</a> '; 
    } // for 
?> 
+3

「工作不正常」 是不是一個好的錯誤描述。發生什麼或沒有發生? – 2011-05-24 17:45:33

+0

我得到它導航到顯示4篇文章的第二頁,然後當我嘗試去第3頁,這是9-12>只是沒有移動到下一頁就什麼都不做。 – 2011-05-24 17:53:05

+0

什麼不移到下一頁?我看到的只是一個>符號。 – Dave 2011-05-24 17:54:31

回答

0

除非count()你從數據庫9或更大,你永遠不會看到一個3

ceil(0/4) -> 0 
    ... 
    ceil(8/4) -> 2 
    ceil(9/4) -> 3 

因此......數據庫中有多少條符合查詢條件的文章?


你不告訴你是如何通過「當前頁」繞算在你的代碼,那麼如何代碼知道你在此刻哪一頁?

$total_pages = 8; 
$current_page = $_GET['curPage']; 

for ($i = 1; $i <= $total_pages; $i++) { 
    $class = ($i == $current_page) ? ' class="current"' : ''; 
    echo <<<EOL 
<a href="page.php?curPage=$i"$class>$i</a> 

EOL; 
} 
+0

我得到了從數據庫回來的21行結果 – 2011-05-24 17:51:51

1

從kohana分頁類廢棄,我想你會發現它有用,如果你知道任何有關PHP類的基礎知識。

用法:

$pager = Pagination::factory(array('current_page' => $_GET['page'], 'total_items' => $total_items, 'items_per_page' => 20)); 

if ($pager->next_page) { /* etc.......*/ } 

<?php 
class Pagination { 


    protected $config = array(
      'current_page'  => 1, 
     'total_items'  => 0, 
     'items_per_page' => 10 
    ); 

    // Current page number 
    protected $current_page; 

    // Total item count 
    protected $total_items; 

    // How many items to show per page 
    protected $items_per_page; 

    // Total page count 
    protected $total_pages; 

    // Item offset for the first item displayed on the current page 
    protected $current_first_item; 

    // Item offset for the last item displayed on the current page 
    protected $current_last_item; 

    // Previous page number; FALSE if the current page is the first one 
    protected $previous_page; 

    // Next page number; FALSE if the current page is the last one 
    protected $next_page; 

    // First page number; FALSE if the current page is the first one 
    protected $first_page; 

    // Last page number; FALSE if the current page is the last one 
    protected $last_page; 

    // Query offset 
    protected $offset; 

    /** 
    * Creates a new Pagination object. 
    * 
    * @param array configuration 
    * @return Pagination 
    */ 
    public static function factory(array $config = array()) 
    { 
     return new Pagination($config); 
    } 

    /** 
    * Creates a new Pagination object. 
    * 
    * @param array configuration 
    * @return void 
    */ 
    public function __construct(array $config = array()) 
    { 


     // Pagination setup 
     $this->setup($config); 

    } 



    /** 
    * Loads configuration settings into the object and (re)calculates pagination if needed. 
    * Allows you to update config settings after a Pagination object has been constructed. 
    * 
    * @param array configuration 
    * @return object Pagination 
    */ 
    public function setup(array $config = array()) 
    { 


     // Only (re)calculate pagination when needed 
     if ($this->current_page === NULL 
      OR isset($config['current_page']) 
      OR isset($config['total_items']) 
      OR isset($config['items_per_page'])) 
     { 

      // Calculate and clean all pagination variables 
      $this->current_page = (int) $this->config['current_page']; 
      $this->total_items  = (int) max(0, $this->config['total_items']); 
      $this->items_per_page  = (int) max(1, $this->config['items_per_page']); 
      $this->total_pages  = (int) ceil($this->total_items/$this->items_per_page); 
      $this->current_page  = (int) min(max(1, $this->current_page), max(1, $this->total_pages)); 
      $this->current_first_item = (int) min((($this->current_page - 1) * $this->items_per_page) + 1, $this->total_items); 
      $this->current_last_item = (int) min($this->current_first_item + $this->items_per_page - 1, $this->total_items); 
      $this->previous_page  = ($this->current_page > 1) ? $this->current_page - 1 : FALSE; 
      $this->next_page   = ($this->current_page < $this->total_pages) ? $this->current_page + 1 : FALSE; 
      $this->first_page   = ($this->current_page === 1) ? FALSE : 1; 
      $this->last_page   = ($this->current_page >= $this->total_pages) ? FALSE : $this->total_pages; 
      $this->offset    = (int) (($this->current_page - 1) * $this->items_per_page); 
     } 

     // Chainable method 
     return $this; 
    } 


    /** 
    * Returns a Pagination property. 
    * 
    * @param string property name 
    * @return mixed Pagination property; NULL if not found 
    */ 
    public function __get($key) 
    { 
     return isset($this->$key) ? $this->$key : NULL; 
    } 

    /** 
    * Updates a single config setting, and recalculates pagination if needed. 
    * 
    * @param string config key 
    * @param mixed config value 
    * @return void 
    */ 
    public function __set($key, $value) 
    { 
     $this->setup(array($key => $value)); 
    } 

} // End Pagination 

?>