创建用于导航的下一个和上一个按钮 [英] Creating Next and Previous buttons for navigation
本文介绍了创建用于导航的下一个和上一个按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在页面上创建下一个和上一个按钮.我使用 id 从数据库中调用信息,并使用 id 变量选择新行和上一行.
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.
它适用于下一个按钮,但上一个按钮一直返回 1 的 id.
It works for the next button, but the previous button keeps returning an id of 1.
这是我在 php 中的函数:
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;
}
}
我在 html 头中这样调用它:
and I am calling it in the html head like this:
<?php
$subj = $_GET['subj'];
$navLink = getNavLinks($subj);
?>
在这样的身体中:
<?php echo "<a href=\"artist.php?subj=" . urlencode($navLink['prevValue']) . "\">Prev</a>"; ?>
<?php echo "<a href=\"artist.php?subj=" . urlencode($navLink['nextValue']) . "\">Next</a>"; ?>
为什么 $navLink['prevValue'] 返回值 1?
Why is the $navLink['prevValue'] returning a value of one?
任何帮助都会很棒!
推荐答案
你需要把你的SQL语句改成这样:
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"
这篇关于创建用于导航的下一个和上一个按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文