【问题标题】:Dynamic webpages: how to link to previous and next blog post [duplicate]动态网页:如何链接到上一篇和下一篇博文[重复]
【发布时间】:2014-08-12 09:34:31
【问题描述】:

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

接下来我要实现的是在每个页面底部的两个 div 中添加上一篇和下一篇博文的链接,基于“id”。

每篇博文的 url 由一个 id 和一个标题组成:“domain.com/id/title

示例:

假设我正在阅读 id = 2 的博文。如何链接到 id = 1(上一个)和 3(下一个)的博文?

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

【问题讨论】:

  • 究竟是什么问题,获取正确的 id 或生成链接?
  • 你的id的数据类型是什么??
  • @jeroen 获取正确的 ID 和标题是问题所在。

标签: php post hyperlink echo blogs


【解决方案1】:

下面是一个简单的方法,您可以使用它来获得所需的内容。

请注意,代码未经测试,您可能会遇到一些错误。您可以在下面的 cmets 中或在 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>

【讨论】:

  • $id 应该被转义。
  • 是的。此代码不应在此状态下使用。它旨在作为给出预期结果的逻辑示例。
  • 感谢您的回答。我了解它的工作原理,但 Dreamweaver 告诉我有几个错误。显然 $smallestID、$largestID、$previousPostTitle 和 $nextPostTitle 的代码并不完全正确。知道这些代码有什么问题吗?
  • 我怀疑这与您的数据库连接变量不在函数selectFromDatabase 中有关。你可以尝试在这个函数中重新定义你的数据库连接材料吗?或者你可以使用它们globally。
猜你喜欢
  • 2018-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-17
  • 1970-01-01
  • 2015-06-26
  • 1970-01-01
  • 2013-03-04
相关资源
最近更新 更多