PHP页面限制页面

时间:2013-07-20 09:13:39

标签: php pagination limit

我想限制页面总页数。

  • 示例:1 2 3 4 5 6 >> out of 30
  • 示例2:<< 5 6 7 8 9 10 >> out of 30

这是我的代码:

$page = !empty($_GET['page']) ? (int) $_GET['page'] : 1;
// records per page
$per_page = 5;
// total records in database
$total_count = Mp3_Model::count_all();
// instantating the $pagination
$pagination = new Pagination($page, $per_page, $total_count);
// find the records for this page
$sql = "SELECT * FROM mp3 ";
$sql .= " LIMIT {$per_page} ";
$sql .= " OFFSET {$pagination->offset()}";
$mp3s = Mp3_Model::find_by_sql($sql);
foreach ($mp3s as $mp3) { 
   echo $mp3->titlu;
}

这就是分页:

<?php
    if ($pagination->total_pages() > 1) {

        if ($pagination->has_previous_page()) {
            echo '<li><a href="?page=' . $pagination->previous_page() . '">&laquo;</a></li>';
        }

        for ($i = 1; $i <= $pagination->total_pages(); $i++) {
            echo '<li';
            if ($_GET['page'] == $i) {
                echo ' class="active"';
            }
            echo '><a href="' . URL . 'mp3?page=' . $i . '">' . $i . '</a></li>';
        }

        if ($pagination->has_next_page()) {
            echo '<li><a href="?page=' . $pagination->next_page() . '">&raquo; </a></li>';
        }
    }
?>

1 个答案:

答案 0 :(得分:-2)

你可以使用jQuery分页,这是一个更好的解决方案。