PHP分頁類 Zebra_Pagination

fmms 13年前發布 | 9K 次閱讀 PHP

Zebra_Pagination 是一個通用的 PHP 類,用來根據記錄數和每頁顯示數自動生成分頁鏈接。

示例代碼:

<?php
// let's paginate data from an array...
$countries = array(
    // array of countries
);

// how many records should be displayed on a page?
$records_per_page = 10;

// include the pagination class
require 'path/to/Zebra_Pagination.php';

// instantiate the pagination object
$pagination = new Zebra_Pagination();

// the number of total records is the number of records in the array
$pagination->records(count($countries));

// records per page
$pagination->records_per_page($records_per_page);

// here's the magick: we need to display *only* the records for the current page
$countries = array_slice(
    $countries,
    (($pagination->get_page() - 1) * $records_per_page),
    $records_per_page
);

?>

<table>

    <tr><th>Country</th></tr>

    <?php foreach ($countries as $index => $country):?>

    <tr<?php echo $index % 2 ? ' class="even"' : '')?>>
        <td><?php echo $country?></td>
    </tr>

    <?php endforeach?>

</table>

<?php

// render the pagination links
$pagination->render();

?>
在 Zebra_Pagination 1.2 中,首頁鏈接不再包含 page 參數,為了避免重復的內容,另外對代碼做了一些優化。

項目地址: http://stefangabos.ro/php-libraries/zebra-pagination/

 本文由用戶 fmms 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!