简体   繁体   中英

JSON data not populating bootstrap table properly

Been trying to get a work around for this for hours now, but I just can't get my bootstrap table to being populated in a correct way. Here is my HTML:

<html>
<head>
    <link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" type="text/css">
  <link rel="stylesheet" href="https://v40.pingendo.com/assets/bootstrap/bootstrap-4.0.0-beta.1.css" type="text/css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-table/1.11.1/bootstrap-table.css" type="text/css">
</head>

<body>
    <script src="https://code.jquery.com/jquery-3.2.1.js" integrity="sha256-DZAnKJ/6XZ9si04Hgrsxu/8s717jcIzLy3oi35EouyE=" crossorigin="anonymous"></script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js" integrity="sha256-T0Vest3yCU7pafRw9r+settMBX6JkKN06dqBnpQ8d30=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.3/umd/popper.min.js" integrity="sha384-vFJXuSJphROIrBnz7yo7oB41mKfc8JzQZiCq4NCceLEaO4IHwicKwpJf9c9IpFgh" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/js/bootstrap.min.js" integrity="sha384-h0AbiXch4ZDo7tp9hKZ4TsHbi047NrKGLO3SEJAg45jXxnGIfYzk4Si90RDIqNm1" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-table/1.11.1/bootstrap-table.js"></script>
    <script src="client.js"></script>



    <table class="table" id="maintable">
            <thead>
              <tr>
                <th data-field="queue">#</th>
                <th data-field="nation_name">Nation</th>
              </tr>
            </thead>
          </table>

</body>


</html>

PHP:

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "db";

$con = mysqli_connect('localhost','root','','db');
if (!$con) {
    die('Could not connect: ' . mysqli_error($con));
}

mysqli_select_db($con,"db");
$sql = "SELECT queue, nation_name FROM nations WHERE queue IS NOT NULL ORDER BY queue ASC";
$result = mysqli_query($con, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        echo json_encode($row);
    }
} else {
    echo "0 results";
}

mysqli_close($con);

?>

JS:

url = "ws://localhost:8080";
ws = new WebSocket(url);

// event emmited when connected
    ws.onopen = function () {
        console.log('websocket is connected ...');
        // sending a send event to websocket server
        ws.send('connected');
    }
    // event emmited when receiving message 
    ws.onmessage = function (ev) {
        console.log(ev.data);
    }

        $.ajax({
                type: 'POST',
                url: 'getqueue.php',
                data: {},
                success: function(response) {
                    alert(response);
                        $(function () {
                            $('#maintable').bootstrapTable({
                                data: response
                            });
                        });

                },
                error: function() {
                    //something
                }
            })

The JSON data that is sent to the page from PHP looks exactly like this:

{"queue":"1","nation_name":"Afghanistan"}{"queue":"2","nation_name":"Sweden"}

But when the page is loaded this is the result: Screenshot

Why is the JSON data not being populated the way I want it? Ie, two rows containing 'queue' and 'nation_name'

The issue is this code returning multiple JSON strings in one:

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        echo json_encode($row);
    }
}

Instead you need to build one JSON string as an array of rows, and return it:

if (mysqli_num_rows($result) > 0) {
    $output = array();
    while($row = mysqli_fetch_assoc($result)) {
        $output[] = $row;
    }
    echo json_encode($output);
}

The fix to your next problem is that you are not using the bootstrap library correctly. You must set columns and tell it what fields to use or else it has no idea what to put where. Fix what @Matt S told you to do for the PHP side, then make my edits for the client side. (I'll make an edit in his answer that he can peer review if he wants). On top of setting the columns you can actually get rid of your ajax request entirely as bootstrapTable supports giving it a url directly.

$('#table').bootstrapTable({
    url: 'getqueue.php',
    columns: [{
        field: 'queue',
        title: '#'
    }, {
        field: 'nation_name',
        title: 'Nation'
    }]
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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