如何在php中获取标题并链接到上一篇和下一篇文章

问题描述 投票:0回答:1

我是PHP的新手,通过开发一个小的CRUD博客来学习。我现在的挑战是如何在用户打开文章后获取标题并链接到上一篇和下一篇博文。这是为了确保轻松导航到下一个和上一个帖子而无需按下后退按钮以选择下一篇文章。

下图解释了我的写作。 enter image description here

我试图查询数据库并使用>而不是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>";
?>    

我实际上想要显示其标题的上一篇和下一篇文章的链接。

php post permalinks next
1个回答
-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);
© www.soinside.com 2019 - 2024. All rights reserved.