请,我是PHP新手。并且我构建了一个Web应用程序,其中包含指向5个以上页面的页面内容的大量链接。数据库工作正常,但是一旦我填充页面,它们就无法出现在单个页面上,因此我尝试了页面分页。 现在要注意的是:我的代码使我只能进入一个页面,而不是指向一个页面的10个页面链接。我已经检查了网络上的可用解决方案,而我的解决方案似乎很特殊,因此很难使代码正常工作。
这是一个代码段:
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>
答案 0 :(得分:0)
$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';
您是否打算将$ _GET ['id']用于页码和category_id?这不是两个单独的变量吗? 就像:$ _GET ['id']代表类别ID,$ _GET ['page']代表页码