简体   繁体   English

PHP中的页面分页

[英]Page Pagination in PHP

Please, I'm new to PHP. 拜托,我是PHP新手。 and I've built a webapp with numerous links to page contents that span more than 5 pages. 并且我构建了一个Web应用程序,其中包含指向5个以上页面的页面内容的大量链接。 The database works fine, but once I populate the pages, they cannot appear on a single page so i tried the Page Pagination. 数据库工作正常,但是一旦我填充页面,它们就无法出现在单个页面上,因此我尝试了页面分页。 Now here's the catch: my code throws me to just a single page as opposed to, say, 10 page links to a page. 现在要注意的是:我的代码使我只能进入一个页面,而不是指向一个页面的10个页面链接。 I've checked the available solutions on the net and mine seems quite peculiar so it's difficult to get the code to work. 我已经检查了网上可用的解决方案,而我的解决方案似乎很特殊,因此很难使代码正常工作。

Here is a snippet: 这是一个片段:

if (isset($_GET['id']) && filter_var($_GET['id'], FILTER_VALIDATE_INT, array('min_range' => 1))) {

    // Get the category title:
    $sql = 'SELECT category FROM categories WHERE id = ' . $_GET['id'];
    $result = $dbc->query($sql); // Use prepared statement instead
    if ($result->num_rows != 1) { // Problem!
        $page_title = 'Error!';

    $page_no = $_GET['id'];
    $no_of_records_per_page = 2;
    $offset = ($page_no - 1) * $no_of_records_per_page;
    // Get the number of total pages
    $sql = 'SELECT COUNT(*) FROM pages';
    $result = $dbc->query($sql);
    $total_rows = $result->fetch_array()[0];
    $total_pages = ceil($total_rows / $no_of_records_per_page);
    // Get the pages associated with this category:
    $query = 'SELECT id, title, description FROM pages WHERE category_id = ' . $_GET['id'] . ' ORDER BY date_created DESC LIMIT 5';
 $result = $dbc->query($query);
    if ($result->num_rows > 0) { // Pages available!

        // Fetch each record:
        while ($row = $result->fetch_assoc()) {

            // Display each record:
            echo "<div><h4><a href=\"page.php?id={$row['id']}\">{$row['title']}</a></h4><p>{$row['description']}</p></div>\n";

        } // End of WHILE loop.
} else { // No pages available.
        echo '<p>There are currently no pages of content associated with this category. Please check back again!</p>';
    }

} else { // No valid ID.
    $page_title = 'Error!';
    include ('./includes/header.html');
    echo '<p class="error">Sorry, this page does not exist.</p>';
} // End of primary IF.


?>

<ul class="pagination">
    <li><a href="page.php?id=1">First</a></li>
    <li class="<?php if($page_no <= 1){ echo 'disabled'; } ?>">
        <a href="page.php<?php if($page_no <= 1){ echo '#'; } else { echo "?id=".($page_no - 1); } ?>">Prev</a>
    </li>
    <li class="<?php if($page_no >= $total_pages){ echo 'disabled'; } ?>">
        <a href="page.php<?php if($page_no >= $total_pages){ echo '#'; } else { echo "?id=".($page_no + 1); } ?>">Next</a>
    </li>
    <li><a href="page.php?id=<?php echo $total_pages; ?>">Last</a></li>
</ul>
$sql = 'SELECT category FROM categories WHERE id = ' . $_GET['id'];

$page_no = $_GET['id'];

$query = 'SELECT id, title, description FROM pages WHERE category_id = ' . $_GET['id'] . ' ORDER BY date_created DESC LIMIT 5';

Do you intend to use $_GET['id'] for both the page number and category_id? 您是否打算将$ _GET ['id']用于页码和category_id? Shouldn't this be 2 separate variables? 这不是两个单独的变量吗? Like: $_GET['id'] for the category id, and $_GET['page'] for the page number 像:$ _GET ['id']代表类别ID,$ _GET ['page']代表页码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM