0

A couple of weeks ago I started to switch from static to dynamic webpages to make life more easier. The result is looking better each day but I still got some issues that I didn't manage to fix myself.

The next thing I want to achieve is to add a link to the previous and next blog post in two divs at the bottom of each page, based on the "id".

The url for each blog post consists of an id and a title: "domain.com/id/title"

Example:

Let's say I'm reading blog post with id = 2. How can I link to the blog posts with id = 1 (previous) and 3 (next)?

<?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>
Stan
  • 937
  • 5
  • 15
  • 33

1 Answers1

0

Below is a simple method you could use to get what you require.

Note that the code is untested, and that you might get some errors. You can clarify them in the comments below or with the help of Google.

However, I hope that you would understand the required logic through this.

<?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>
TribalChief
  • 797
  • 5
  • 11
  • The $id should be escaped. – S. A. Kıyak Jun 22 '14 at 02:47
  • True. This code shouldn't be used in this state. It was meant to serve as an example of the logic that would give the expected results. – TribalChief Jun 22 '14 at 02:53
  • Thank you for your answer. I understand how it works but Dreamweaver tells me that there are a couple of errors. Apparently the code for $smallestID, $largestID, $previousPostTitle and $nextPostTitle is not entirely correct. Any idea about what is wrong with these codes? – Stan Jun 22 '14 at 18:48
  • I suspect that it has to do with your database connection variables not being in the function `selectFromDatabase`. Can you try redefining your database connection material within this function? Or you could use them `global`ly. – TribalChief Jun 23 '14 at 02:58