简体   繁体   中英

Creating Next and Previous buttons for navigation

I am trying to create next and previous buttons on a page. I am using the id to call the information from the database and also using the id variable to choose the new and previous row.

It works for the next button, but the previous button keeps returning an id of 1.

This is my function in php:

function getNavLinks($subj) {
global $connection;
$query = "SELECT 
( SELECT id FROM artists WHERE id > " . $subj . " LIMIT 1 )
AS nextValue,
( SELECT id FROM artists WHERE id < " . $subj . " LIMIT 1 )
AS prevValue
FROM artists";
$result_set = mysql_query($query, $connection);
confirmQuery($result_set);
//if no rows are returned, fetch array will return false.
if ($Links = mysql_fetch_array($result_set)) {
    return $Links;
}
else {
    return NULL;
}
}

and I am calling it in the html head like this:

<?php
$subj = $_GET['subj'];
$navLink = getNavLinks($subj);
?>

and in the body like this:

<?php echo "<a href=\"artist.php?subj=" . urlencode($navLink['prevValue']) . "\">Prev</a>"; ?>&nbsp;
<?php echo "<a href=\"artist.php?subj=" . urlencode($navLink['nextValue']) . "\">Next</a>"; ?>

Why is the $navLink['prevValue'] returning a value of one?

Any help would be great!

You need to change your SQL statement to this:

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

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