johansson johansson - 4 months ago 10
PHP Question

View data with json php mysqli in tag table

I have table users :


  • id

  • login

  • password



and i have this code to display data :

page : config.php :

$rep = $db->query("SELECT * FROM users");
$array_user[] = array();
while($data = $rep->fetch()){
$array_user = $data;
}
echo json_encode($array_user);
?>


page : list.php :

<div id="tab"></div>

<script>
$(document).on("ready",function(){

loadData();
});
var loadData = function(){
$.ajax({
type:"POST",
url:"config.php"
}).done(function(data){
console.log(data);
var users = JSON.parse(data);


for(var i in users){
$("#tab").append(users[i].login + "<br>");
}

});
}
</script>


but i want to display id and login in tag table
for example :

<table id="tab">
<tr>
<th>id</th>
<th>login</th>
</tr>

<tr>
<td>(display data with json : id)</td>
<td>(display data with json : login )</td>
</tr>
</table>

Answer

For one thing, you need to reference the td tags, otherwise you're just appending to the table itself. For example:

for(var i in users){
    var data = '<tr><td>' + users[i].id + '</td></tr><tr><td>' + users[i].login + '</td></tr>';
    $("#tab").append(data);
}
Comments