【问题标题】:How can I retrieve 1 row at a time from a db on button click?如何在单击按钮时从数据库中一次检索 1 行?
【发布时间】:2019-06-21 01:35:53
【问题描述】:

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

这是我用来检索数据的 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);

这是我的按钮

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

【问题讨论】:

    标签: javascript php json ajax


    【解决方案1】:

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

    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++; // increment the value after successful AJAX call
          $.each(result, function(i, field){
             $("#div1").empty();
            $("#div1").append(JSON.stringify(result));     //alert(JSON.stringify(data));
          });
        });
      });
    });
    </script>
    

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

    【讨论】:

    • 感谢您的回复,我试过了,但我的 php 文件中不断出现错误:xdebug-error xe-warning: Warning: mysqli_fetch_assoc() 期望参数 1 为 mysqli_result,布尔值。出于某种原因,它不喜欢 sql 语句中的偏移变量,因为当我删除它时,错误消失了
    • 我忘了放一个 , 来分隔偏移量和限制。我已经更新了答案检查它现在是否有效。
    • 谢谢,我现在收到未定义行变量的错误。
    • 我已经设法解决了这个问题,但是每次我尝试使用按钮逐个遍历每条记录时,它都会将表中的下一项添加到最后一项
    【解决方案2】:

    我会在 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'].";"
    

    【讨论】:

      猜你喜欢
      • 2020-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-16
      • 1970-01-01
      相关资源
      最近更新 更多