简体   繁体   English

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

[英]How can I retrieve 1 row at a time from a db on button click?

I'm developing an item picking system and need to iterate through a table one item at a time on the click of a button. 我正在开发一个项目拣选系统,需要在点击一个按钮时一次遍历一个项目。 Currently, the button only retrieves and displays the first item in the table. 目前,该按钮仅检索并显示表格中的第一个项目。 How can I change it so it iterates through each one at a time when I click the button? 如何更改它,以便在我单击按钮时一次迭代每个? Any help would be appreciated, thank you 任何帮助将不胜感激,谢谢

Here's my php file to retrieve data 这是我的php文件来检索数据

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);

And here's my button 这是我的按钮

<script>
 $(document).ready(function () {
$('#button1').click(function (e) {
    $.getJSON("getItem.php", function(result){
      $.each(result, function(i, field){
         $("#div1").empty();
        $("#div1").append(JSON.stringify(result));     //alert(JSON.stringify(data));
      });
    });
  });
});
</script>

You need to maintain a variable to handle the offset for the LIMIT query. 您需要维护一个变量来处理LIMIT查询的偏移量。 You can try this way, 你可以这样试试,

JS Code : JS代码:

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

PHP Code : PHP代码:

mysqli_select_db($con,"items");

// Get the offset value
$offset = empty($_GET['offset']) ? 0 : $_GET['offset']

$sql="SELECT * FROM items Limit 1, $offset"; // pass the offset value to LIMIT query
$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);

I would put an attribute in button1 to keep track of the count. 我会在button1中放置一个属性来跟踪计数。

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

In your javascript, add code to find that count and send it in your request to your PHP page. 在您的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

In your PHP page, I have to assume you have some kind of ID as the key. 在您的PHP页面中,我必须假设您有某种ID作为密钥。 item_id = 1, item_id = 2, etc. Change the query to this: item_id = 1,item_id = 2等。将查询更改为:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM