0

I have taken the working code from this thread: Creating Next and Previous buttons for navigation

It works for me but right now I would like to ask is there any way to disable the link if it reaches the 1st or the last ID?

Here's my brief code:

function getNavID($id) {

$result4= mysql_query("SELECT 
( SELECT id FROM products_list WHERE id > '$id' LIMIT 1 )
AS nextValue,
( SELECT id FROM products_list WHERE id < '$id' ORDER BY id DESC LIMIT 1 )
AS prevValue
FROM products_list");
if ($resultID = mysql_fetch_array($result4)) {
    return $resultID;
}
else {
    return NULL;
}
}

$LinkID = getNavID($id);

<p><a href="update.php?id=<?php echo urlencode($LinkID['prevValue']); ?>">Previous</a></p>
<p><a href="update.php?id=<?php echo urlencode($LinkID['nextValue']); ?>">Next</a></p>

Let's say the page I'm on now is ID=1 so logically, there is no ID lesser than 1. Is there any way that I can do to disable the links if the ID reaches the min and max ID ?

Thanks in advance guys.

Regards, Jeff

1 Answer 1

2

For ID=1, $LinkID['prevValue'] should return NULL, which you can check easily. Include a check, so that you only print the link if there is a previous/next value, e.g.:

<p>
<?php
    if (!is_null($LinkID['prevValue'])) {
>?
    <a href="update.php?id=<?php echo urlencode($LinkID['prevValue']); ?>">Previous</a>
<?php
    } else {
        echo "No previous entries.";
    }
?>
</p>

And analogously for Next entry.

By the way, just for completeness' sake, I would rewrite the SQL statement like this:

SELECT 
    ( SELECT id FROM products_list
        WHERE id > '$id' LIMIT 1 ) AS nextValue,
    ( SELECT id FROM products_list
        WHERE id < '$id' ORDER BY id DESC LIMIT 1 ) AS prevValue
    FROM products_list
    LIMIT 1

I.e. an additional LIMIT 1 at the end. You might not notice a difference in your result, but with your statement, the database delivers one row per entry in the table products_list (of which you only use the first, and they are all equal).

Sign up to request clarification or add additional context in comments.

2 Comments

Hi, thanks for the reply. I've tried your code but it's not working. It show me a blank page.
I think I missed a parenthesis, I will correct that. In such cases (blank page), check the server log (or enable display of errors in browser (display_errors setting in php.ini, but enable that only if you're not on a production system!).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.