I have a problem creating dynamic tables for my project. With AJAX, I get data from PHP and JSON converts the array into a table but the dump doesn't work.
if ($result->num_rows > 0)
{
while($row = $result->fetch_assoc())
{
if ($row["Author"] !== "" && $row["Name"])
{
$table_data = array(
"Author" => "".$row["Author"]."",
"Name" => "".$row["Name"]."",
"Text" => "".$row["Text"].""
);
array_push($filtr, json_encode($table_data));
echo "".json_encode($table_data)."";
}
}
}
$.post("../include/filtr_callback.php", { sel_id: "" + knih_callback + "" }, function(data, status) {
//var mydata = JSON.parse(data);
var mydata = JSON.parse(JSON.stringify(data));
mydata = "[ " + mydata + " ];"
console.log(mydata);
var tbl = $("<table/>").attr("id", "table-data");
$("#tabulka").append(tbl);
for (var i = 0; i < mydata.length; i++) {
var tr = "<tr>";
var td1 = "<td>" + mydata[i]["Author"] + "</td>";
var td2 = "<td>" + mydata[i]["Name"] + "</td>";
var td3 = "<td>" + mydata[i]["Name"] + "</td>";
var td4 = "<td>" + mydata[i]["Text"] + "</td></tr>";
$("#table-data").append(tr + td1 + td2 + td3 + td4);
}
});
Rory McCrossan
338k41 gold badges322 silver badges353 bronze badges
-
1Unless I'm mistaken, you are echoing a json encoded array every iteration of the loop. That is going to return invalid json to the front end. You need to collect all your results into a single element, and echo that whole object json_encoded at the end, And don't append "" to it, as that is unnecessaryTaplar– Taplar2019年03月19日 13:50:23 +00:00Commented Mar 19, 2019 at 13:50
-
Did you tried my solution below?Edison Biba– Edison Biba2019年03月19日 15:05:08 +00:00Commented Mar 19, 2019 at 15:05
-
Yes,Not work. Data in <table> write "undefined"TheGridCoder– TheGridCoder2019年03月19日 19:36:12 +00:00Commented Mar 19, 2019 at 19:36
1 Answer 1
You need to echo the result after you finish iteration of the loop. So I have created a secondary array called $table where I store results after each iteration.
$table = [];
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
if($row["Author"] !== "" && $row["Name"]){
$table_data = array(
"Author" => "".$row["Author"]."",
"Name" => "".$row["Name"]."",
"Text" => "".$row["Text"].""
);
array_push($filtr,json_encode($table_data));
$table[] = $table_data;
}
}
echo json_encode($table);
}
I have also made some small changes in your javascript code. You don't need to call anymore JSON.stringify on your response and also mydata is array itself after JSON.parse so I also removed mydata = "[ " + mydata + " ];".
$.post("../include/filtr_callback.php", { sel_id: "" + knih_callback + "" }, function(data, status) {
var mydata = JSON.parse(data);
console.log(mydata);
var tbl = $("<table/>").attr("id", "table-data");
$("#tabulka").append(tbl);
for (var i = 0; i < mydata.length; i++) {
var tr = "<tr>";
var td1 = "<td>" + mydata[i]["Author"] + "</td>";
var td2 = "<td>" + mydata[i]["Name"] + "</td>";
var td3 = "<td>" + mydata[i]["Name"] + "</td>";
var td4 = "<td>" + mydata[i]["Text"] + "</td></tr>";
$("#table-data").append(tr + td1 + td2 + td3 + td4);
}
});
answered Mar 19, 2019 at 13:53
Edison Biba
4,4833 gold badges20 silver badges36 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Rory McCrossan
mydata = "[ " + mydata + " ];" needs to be removed as welldefault