如何单击按钮一次从数据库检索1行?

时间:2019-01-28 04:05:37

标签: javascript php json ajax

我正在开发一个项目拣选系统,需要单击一个按钮一次遍历一个表。当前,该按钮仅检索并显示表中的第一项。我如何更改它,以使其在单击按钮时一次遍历每一个?任何帮助将不胜感激,谢谢

这是我的php文件,用于检索数据

[w[0]] + [4 if i%2 else 2 for i, _ in enumerate(w[1:-1])] + [w[-1]]

这是我的按钮

mysqli_select_db($con,"items");


$sql="SELECT * FROM items Limit 1 ";
$result = mysqli_query($con,$sql);

while($row = mysqli_fetch_assoc($result))
    $rows[] = array_map('utf8_encode', $row);

header('Content-Type: application/json');
echo json_encode($rows);

2 个答案:

答案 0 :(得分:3)

您需要维护一个变量来处理LIMIT查询的偏移量。您可以尝试这种方式,

JS代码:

npm update -g firebase-tools

PHP代码:

<script>
var offset = 0;

 $(document).ready(function () {
$('#button1').click(function (e) {
    // pass offset value with GET request
    $.getJSON("getItem.php?offset=" + offset, function(result){

      offset++; // increament the value after sucessful AJAX call
      $.each(result, function(i, field){
         $("#div1").empty();
        $("#div1").append(JSON.stringify(result));     //alert(JSON.stringify(data));
      });
    });
  });
});
</script>

答案 1 :(得分:1)

我会在button1中放置一个属性以跟踪计数。

<div id="button1" count="1">clickme</div>

在您的JavaScript中,添加代码以查找该计数并将其在您的请求中发送到PHP页面。

('#button1').click(function (e) {
var thisCount = document.getElementById('button1').getAttribute('count');
// don't forget to increase that count for the next time
document.getElementById('button1').setAttribute('count', (thisCount + 1));
$.getJSON("getItem.php?count=" + thisCount, function(result){
// ... rest of code

在您的PHP页面中,我必须假定您具有某种ID作为密钥。 item_id = 1,item_id = 2,依此类推。将查询更改为此:

"SELECT * FROM items WHERE item_id = ".$_GET['count'].";"