【问题标题】:I want to display the following data in table我想在表格中显示以下数据
【发布时间】:2019-03-25 03:04:49
【问题描述】:

我想在表格中显示以下数据。

我尝试从数据库中获取数据,但只有一列可见

# AJAX CODE

    $.ajax({
        url:'process/getState.php',
        method:'GET',
        success:function(response){
            res = JSON.parse(response);
            console.log(res);
            $.each(res,function(k,v){
                var t = $('.template > table > tbody > tr').clone();
                t.find('.state').html(v.state);
                t.find('.count').html(v.count);

                $('#tbody').append(t);
                console.log(v); 
            });
        }
    })

# PROCESS FILE

<?php
    include('connection.php');

    $conn = connection();
    $sql = "SELECT * FROM statedistribution";
    $result = $conn->query($sql);
    $state = [];
    if ($result->num_rows > 0) {
        // output data of each row
        while($row = $result->fetch_assoc()) {
            array_push($state,$row); 
        }
        die(json_encode($state));
    } else {
        echo "0 results";
    }
    $conn->close();
?>

我只获取状态列中的数据,但计数列却是空白

【问题讨论】:

    标签: javascript php mysql html


    【解决方案1】:

    您看到一列或一行是因为您的 $state 未正确声明为数组 只需将您的流程文件编辑成如下所示:

    include('connection.php');
    
    $conn = connection();
    $sql = "SELECT * FROM statedistribution";
    $result = $conn->query($sql) or die ("Error :".mysql_error());
    $state = array();
    if ($result->num_rows > 0) {
        while($row = $result->fetch_assoc()) {
            $state[] = $row;
        }
        $output = json_encode($state);
    } else $output = "0 results";
    $conn->close();
    echo $output;
    

    【讨论】:

      猜你喜欢
      • 2012-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-26
      相关资源
      最近更新 更多