我正在尝试从mysql数据库中获取下一个先前的行到ajax
我设法做到了,但是当我单击上一个按钮时遇到问题,它总是使我回到第一个结果
<script>
$(document).ready(function(){
var card_id = document.getElementById('cardid').value
$(document).on('click', '#previous', function(){
$.ajax({
url:"includes/fetchcards.php",
method:"POST",
dataType: 'json',
data:{card_id:card_id,prev:1},
success:function(data)
{
$('#cardfront').text(data.front)
$('#cardback').text(data.back);
card_id = data.id
alert(card_id)
}
});
});
$(document).on('click', '#next', function(){
$.ajax({
url:"includes/fetchcards.php",
method:"POST",
dataType: 'json',
data:{card_id:card_id, next:1},
success:function(data)
{
$('#cardfront').html(data.front)
$('#cardback').html(data.back);
card_id = data.id
alert(card_id)
}
});
});
});
</script>
php代码
<?php
include 'config.php';
if(isset($_POST["card_id"]))
{
$card_id = $_POST["card_id"];
if(isset($_POST["next"])){
$db->where("id", $card_id, ">") ;
$flaschcard = $db->getOne('cards') ;
}
if(isset($_POST["prev"])){
$db->where("id", $card_id, "<") ;
$flaschcard = $db->getOne('cards') ;
}
if(isset($flaschcard)) {
echo json_encode($flaschcard);
}
}
我希望当我单击上一个按钮时,它显示上一行,但它总是将我带回到第一个结果
答案 0 :(得分:0)
if(isset($_POST["prev"])){
$db->where("id", $card_id, "<") ;
$db->orderBy("id", "desc"); // Reverse the order to fetch the last one
$flaschcard = $db->getOne('cards') ;
}