我是PHP的新手,并且通过开发一个CRUD博客来学习。我现在面临的挑战是如何在用户打开文章后获取标题并链接到上一个和下一个博客文章。这是为了确保轻松导航到下一篇和上一篇文章,而无需单击后退按钮来选择下一篇文章。
下面的图片说明了我的写作。 enter image description here
我尝试查询数据库,并使用> than id或小于id链接到无效的下一篇和上一篇文章。
<?php
$previous = "SELECT * FROM posts WHERE `posts`.`id` < $id";
$next = "SELECT * FROM posts WHERE `posts`.`id` > $id";
echo "<li><a href='{{url('post.php/' . $previous)}}'> Previous</a></li>";
echo "<li><a href='{{url('post.php/' . $next)}}'> Next</a></li>";
?>
我实际上想要显示下一篇文章的上一篇文章和下一篇文章的链接。
答案 0 :(得分:-1)
/**
* Rows per page
*/
$limit = 1;
// Find out how many items are in the table
$total = $db->get_total_count('cat_id', 'category', array('show_home_page' => 1, 'type_id' => 5, 'status' => 1));
// How many pages will there be
$pages = ceil($total / $limit);
// What page are we currently on?
$page = min($pages, filter_input(INPUT_GET, 'page', FILTER_VALIDATE_INT, array(
'options' => array(
'default' => 1,
'min_range' => 1,
),
)));
// Calculate the offset for the query
$offset = ($page - 1) * $limit;
// Some information to display to the user
$start = $offset + 1;
$end = min(($offset + $limit), $total);
// The "back" link
$prevlink = ($page > 1) ? '<a href="#prev" class="program_nav" data-id="'.($page - 1).'" title="Back"><i class="fa fa-chevron-left"></i></a>' : '<a href="#prev" class="disabled" title="Back"><i class="fa fa-chevron-left"></i></a>';
// The "forward" link
$nextlink = ($page < $pages) ? '<a href="#next" class="program_nav" data-id="'.($page + 1).'" title="Next"><i class="fa fa-chevron-right"></i></a>' : '<a href="#next" class="disabled" title="Next"><i class="fa fa-chevron-right"></i></a>';
$total_query = 'SELECT COUNT(cat_id) FROM category WHERE show_home_page = 1 AND type_id = 5 AND status = 1';
$query = 'SELECT c.name, c.url FROM category as c WHERE c.show_home_page = 1 AND c.type_id = 5 AND c.status = 1 ORDER BY c.order ASC';
$programs = $db->get_table_custom($total_query, $query, true, $limit, $page);