我是新手,还在学习php& mysql。搜索一整天并尝试不同的教程但没有任何反应。到目前为止,我只有这个,我从我的数据库中获取文本。如何在这里制作上一个和下一个按钮?
if($q = mysqli_query($con, 'SELECT * FROM joke WHERE `id` = ' . mysqli_real_escape_string($con,$_GET['id']))){
if($row = mysqli_fetch_array($q)){
echo nl2br($row['text']);
echo '<div id="data">Date ' . $row['date'] . "</div>';
} else {
echo 'Not found';
}
} else {
echo mysqli_error($con);
}
echo "</div>";
$query = "select * from joke order by RAND() LIMIT 1";
$result = mysqli_query($con, $query) or die("Query failed: " . mysqli_errno($con));
while ($row = mysqli_fetch_array($result, MYSQL_BOTH)){
echo '<a href="single.php?id='.$row['id'].'"class="random">Random</a>';
}
答案 0 :(得分:1)
如果我理解正确你想要显示基于id的下一个和之前的笑话,
所以你想从笑话中选择max(id),其中id&lt;当前的身份
和...从笑话中选择min(id),其中id&gt;当前的身份
$currentId = mysqli_real_escape_string($con,$_GET['id']);
if($q = mysqli_query($con, 'SELECT *,
(SELECT IFNULL(max(id),-1) FROM joke WHERE `id` < '.$currentId.') as previousid,
(SELECT IFNULL(min(id),-1) FROM joke WHERE `id` > '.$currentId.') as nextid
FROM joke WHERE `id` = ' . $currentId)){
if($row = mysqli_fetch_array($q, MYSQL_BOTH)){
echo nl2br($row['text']);
echo '<div id="data">Date ' . $row['date'] . '</div>';
if ($row['previousid'] > -1){
echo '<a href="single.php?id='.$row['previousid'].'"class="random">Previous</a>';
}
if ($row['nextid'] > -1){
echo '<a href="single.php?id='.$row['nextid'].'"class="random">Next</a>';
}
} else {
echo 'Not found';
}
} else {
echo mysqli_error($con);
}
echo "</div>";
$query = "select * from joke order by RAND() LIMIT 1";
$result = mysqli_query($con, $query) or die("Query failed: " . mysqli_errno($con));
while ($row = mysqli_fetch_array($result, MYSQL_BOTH)){
echo '<a href="single.php?id='.$row['id'].'"class="random">Random</a>';
}