创建导航的下一个和上一个按钮

时间:2011-09-12 21:08:27

标签: php mysql

我正在尝试在页面上创建下一个和上一个按钮。我使用id来调用数据库中的信息,并使用id变量来选择新行和上一行。

它适用于下一个按钮,但前一个按钮保持返回id为1。

这是我在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头中这样称呼它:

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

在身体里像这样:

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

为什么$ navLink ['prevValue']返回值为1?

任何帮助都会很棒!

1 个答案:

答案 0 :(得分:1)

您需要将SQL语句更改为:

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