-1

I'm trying to made a table which can get database table value all row record. I've made it and I can do it but the data record just showing one data from the record database table, and it always looping the record value to the right while I clicked the button for showing the record into the table. Does anyone know what wrong with my code here?

This is the php, jquery and the html script, and the screenshot of the page:

enter image description here

$(document).ready(function() {
    $("#ajaxButton").click(function() {
        $.ajax({
            type: "Post",
            url: "employee.php",
            success: function(data) {
                var list = JSON.parse(data);
                var tr = "";
                $.each(list, function(i, v) {
                    tr = +"<tr>";
                    tr += "<td>" + v['no'] + "</td>";
                    tr += "<td>" + v['sensor1'] + "</td>";
                    tr += "<td>" + v['sensor2'] + "</td>";
                    tr += "<td>" + v['sensor3'] + "</td>";
                    tr += "<td>" + v['sensor4'] + "</td>";
                    tr += "<td>" + v['sensor5'] + "</td>";
                    tr += "<td>" + v['sensor6'] + "</td>";
                    tr += "<td>" + v['sensor7'] + "</td>";
                    tr += "<td>" + v['sensor8'] + "</td>";
                    tr += "<td>" + v['sensor9'] + "</td>";
                    tr += "<td>" + v['sensor10'] + "</td>";
                    tr += "<td>" + v['sensor11'] + "</td>";
                    tr += "<td>" + v['sensor12'] + "</td>";
                    tr += "<td>" + v['ambien'] + "</td>";
                    tr += "<td>" + v['average'] + "</td>";
                    tr += "<td>" + v['deffiasi'] + "</td>";
                    tr += "<td>" + v['status'] + "</td>";
                    tr += "</tr>";

                });
                $("#table_s tbody").append(tr);
            }
        });
    });
});
            row += '<td>' + data.rows[i].date + '</td>';
            row += '<td>' + data.rows[i].company + '</td>';
            row += '<td>' + data.rows[i].location + '</td>';
<html>
<head>
<script language="javascript" type="text/javascript" src="jquery-1.6.2.js"></script>
<script language="javascript" type="text/javascript" src="ajax.js"></script>
</head>
<body>
<form name="table_s" id="table_s" class="table_s">
    <table id="table_s" class="table_s"cellspacing='0' class="js-serial" border="2">
        <thead>

      <tr>
                <th><center>No.</center></th>
                <th><center>S1</center></th>
                <th><center>S2</center></th>
                <th><center>S3</center></th>
                <th><center>S4</center></th>
                <th><center>S5</center></th>
                <th><center>S6</center></th>
                <th><center>S7</center></th>
                <th><center>S8</center></th>
                <th><center>S9</center></th>
                <th><center>S10</center></th>
                <th><center>S11</center></th>
                <th><center>S12</center></th>
                <th><center>Ambien</center></th>
                <th><center>Average</center></th>
                <th><center>Deff</center></th>
                <th><center>Status</center></th>
      </tr>

        </thead>
        <tbody>
          <tr>
          </tr>

      </tbody>
      </table>
<input type="button" value="Click Here" id="ajaxButton"/>

</body>
</html>
<?php
$con=mysqli_connect("localhost","root","","silo");

if (!$con) {
  die('Could not connect: ' . mysql_error());
}


// Data for Titik1
$query = mysqli_query($con,"SELECT * FROM termocouple");
$rows = array();

while($tmp= mysqli_fetch_array($query)) {
    $rows[] = $tmp;


}

echo json_encode($rows);
mysqli_close($con);
?> 
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Jack Henaldy
  • 69
  • 1
  • 10
  • If you're writing new code, **_please_ don't use the `mysql_*` functions**. They are old and broken, were deprecated in PHP 5.5 (which is so old it no longer even receives security updates), and completely removed in PHP 7. Use [`PDO`](https://secure.php.net/manual/en/book.pdo.php) or [`mysqli_*`](https://secure.php.net/manual/en/book.mysqli.php) with _prepared statements_ and _parameter binding_ instead. See http://stackoverflow.com/q/12859942/354577 for details. – ChrisGPT was on strike Mar 09 '17 at 01:28
  • @Chris i've done editing the code. i use mysqli now – Jack Henaldy Mar 09 '17 at 01:42
  • `tr = +"";` Makes that the tr variable is equal NaN as that + operator will convert string to number, which is converted to NaN (not a number) and then stored to tr variable, so you're missing opening tag in result. Saying that, `tr = +""` is not equal to `tr += ""` – Mr_KoKa Mar 09 '17 at 01:56
  • then which one i must use? tr = + "" or tr +=""? – Jack Henaldy Mar 09 '17 at 02:07
  • im sorry.. im new at json. can you give me an example? – Jack Henaldy Mar 09 '17 at 02:12

1 Answers1

0

You should append tr within the loop.

success: function(data){
  var list = JSON.parse(data);
  for(var i = 0; i < list.length; i++){
    var tr = "<tr>";
    tr += "<td>"+list[i]['no']+"</td>";
    tr += "<td>"+list[i]['sensor1']+"</td>";
    tr += "<td>"+list[i]['sensor2']+"</td>";
    tr += "<td>"+list[i]['sensor3']+"</td>";
    tr += "<td>"+list[i]['sensor4']+"</td>";
    tr += "<td>"+list[i]['sensor5']+"</td>";
    tr += "<td>"+list[i]['sensor6']+"</td>";
    tr += "</tr>";
    $("#table_s tbody").append(tr);
  }
}
Su Nandar
  • 1
  • 1