简体   繁体   中英

How to get the title and link to the previous and next post in php

I'm new to PHP and learning by developing a little CRUD blog. My challenge now is how to get the title and link to the previous and next blog post after a user opens an article. This is to ensure easy navigation to the next and previous post without hitting the back button in order to select the next article.

The image below explains my write up. enter image description here

I have tried to query the database and use > than id or less than id to link to the next and previous articles which didn't work.

 <?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>";
?>    

I actually wanted the previous and next links to the next and previous articles with their titles shown.

#current blog post

$blog_id = 5;
$b_query=$conn->prepare("SELECT * FROM blogs WHERE blog_id=:id AND blog_status=:status");
$b_query->execute(array(
    'id' => $blog_id,
    'status' => 1
));
$b_query_get=$b_query->fetch(PDO::FETCH_ASSOC);

/* We check the existence of such data from the database. */
$single_post_control=$b_query->rowCount();
if($single_post_control == 0   ){
    echo "error";
    exit;
}

/* next post - previous post*/
$b_pre_nex=$conn->prepare("SELECT * FROM blogs WHERE   blog_id = (SELECT MIN(blog_id)  FROM blogs WHERE blog_id > :pre_nex_id AND blog_status=:pre_nex_status )  OR blog_id = (SELECT MAX(blog_id) FROM blogs WHERE blog_id < :pre_nex_id AND blog_status=:pre_nex_status) ");
$b_pre_nex->execute(array(
    'pre_nex_id' => $blog_id_get,
    'pre_nex_status' => 1
));

$control=$b_pre_nex->rowCount();
if($control == 0 || $control  > 3  ){
    echo "error";
    exit;
}

/* Here, we are splitting our previous and next posts separately.*/
$prev_post = $b_pre_nex->fetch(PDO::FETCH_ASSOC);
$next_post = NULL;
while ( $rows = $b_pre_nex->fetch(PDO::FETCH_ASSOC) ) {
    $next_post = $rows;
}
if(!isset($next_post)) {
    $next_post = null;
}


echo '<a href="#" style="float:left">'.$prev_post['blog_name'].'</a>';

echo '<a href="#" style="float:right">'.$next_post['blog_name'].'</a>';

maybe it can come in handy.

/**
 * 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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