动态网页:如何链接到上一篇和下一篇博文

时间:2014-06-22 01:32:00

标签: php post hyperlink echo blogs

几周前,我开始从静态网页切换到动态网页,让生活更轻松。结果每天都看起来更好,但我仍然遇到了一些我无法解决问题的问题。

我想要实现的下一步是根据" id"在每页底部的两个div中添加上一篇和下一篇博文的链接。

每篇博文的网址都包含一个ID和一个标题:" domain.com/id/title "

示例:

让我们说我正在阅读id = 2的博文。如何链接到id = 1(上一个)和下一个(下一个)的博客文章?

<?php
    (connect to database)
    $id = str_replace ('-', ' ', $_GET['id']);
    $sql = "SELECT * FROM `newstable` WHERE `id` = '$id'";
    $result = $conn->query($sql);
    if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
?>

<div class="content-title">
    <?php echo $row['title'];?>
</div>

<div class="content-text">
    <?php echo $row['text'];?>
</div>     


<div id="previous post">
    ...........
    (here comes the link to the previous post, I need to link to the correct url so that means I'll need to echo the id AND the title of that previous post)
</div>

<div id="next post">
    ...........
    (here comes the link to the next post, I need to link to the correct url so that means I'll need to echo the id AND the title of that next post)
</div>

1 个答案:

答案 0 :(得分:0)

以下是一种可以用来获得所需内容的简单方法。

请注意,代码未经测试,您可能会遇到一些错误。您可以在下面的评论中或在Google的帮助下澄清它们。

但是,我希望你能通过这个理解所需的逻辑。

<?php
    function selectFromDatabase($queryToRun){ // A function to facilitate the querying of the database. It returns an array.
        $result = $conn->query($queryToRun);
        if ($result->num_rows > 0) {
            $record = $result->fetch_assoc(); 
            return $record;
        }
    }   
    $pageLink = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; // Get the page's URL.
    $exploded = explode('/', $pageLink); // Split the URL into an array delimited by '/'s.
    $id = $exploded[3]; // This will be the ID of the content, based on the sample URL you provided.
    $smallestID = selectFromDatabase("SELECT MIN(id) FROM `newstable` WHERE `id` = '$id'")['id']; // Select the smallest ID from the database, to ensure that you link to existing content only.
    $largestID = selectFromDatabase("SELECT MAX(id) FROM `newstable` WHERE `id` = '$id'")['id']; // Select the greatest ID from the database for a similar purpose. Note that doing this would not reap benefits if you have gaps in the IDs of your content.
    $previousPostID = (($id - 1) < $smallestID) ? $largestID : ($id - 1); // If ($id - 1) is smaller than the smallest ID, make the previous post's ID = the largest ID. Else leave it at that.
    $nextPostID = (($id++) > $largestID) ? $smallestID : ($id++); // Similar to the previous line, but for the largest ID.
    $previousPostTitle = selectFromDatabase("SELECT `title` FROM `newstable` WHERE `id` = '$previousPostID'")['title']; // Select the required title.
    $nextPostTitle = selectFromDatabase("SELECT `title` FROM `newstable` WHERE `id` = '$nextPostID'")['title']; // Select the required title.
    $sql = "SELECT * FROM `newstable` WHERE `id` = '$id'"; // Your code to get the content in question.
    $result = $conn->query($sql);
    if ($result->num_rows > 0) {
        while($row = $result->fetch_assoc()) {
?>
<div class="content-title">
    <?php echo $row['title'];?>
</div>
<div class="content-text">
    <?php echo $row['text'];?>
</div>     
<?php 
        }
    } 
?>
<div id="previousPost">
    <a href="<?php echo "http://$_SERVER[HTTP_HOST]".$previousPostID."/".$previousPostTitle; ?>">Previous</a> <!-- Putting together the URL for the previous page. -->
</div>
<div id="nextPost">
    <a href="<?php echo "http://$_SERVER[HTTP_HOST]".$nextPostID."/".$nextPostTitle; ?>">Next</a> <!-- Putting together the URL for the next page. -->
</div>