From the database I receive these values in AJAX in JSON format , which will change as the data is received.
I attach a small example to know how I can go through it to be able to print it in a table through Codeigniter
This is what I get from the database by AJAX
{
"respuesta": "success",
"post": [
{
"ID_OTPloteo": "61",
"ID_Cliente": "3",
"ID_Documento": "2",
"Subtotal_OTPloteo": "23.75",
"Impuesto_OTPloteo": "2.85",
"Total_OTPloteo": "26.60",
"NumeroDocumento_OTPloteo": "000140",
"Serie_OTPloteo": "001",
"Fecha_OTPloteo": "2020-08-19 21:45:12",
"Nombre_Cliente": "David Alejandro",
"Apellido_Cliente": "Paucar Briones",
"Telefono_Cliente": "0992896820",
"Nombre_Documento": "Factura",
"Impuesto_Documento": "12",
"Cantidad_Documento": "141",
"Serie_Documento": "001",
"ID_DetalleOTPloteo": "30",
"Precio_OTPloteo": "3.00",
"Importe_OTPloteo": "3.75"
},
[
{
"ID_DetalleOTPloteo": "30",
"ID_OTPloteo": "61",
"Precio_OTPloteo": "3.00",
"Importe_OTPloteo": "3.75"
},
{
"ID_DetalleOTPloteo": "31",
"ID_OTPloteo": "61",
"Precio_OTPloteo": "7.00",
"Importe_OTPloteo": "8.75"
},
{
"ID_DetalleOTPloteo": "32",
"ID_OTPloteo": "61",
"Precio_OTPloteo": "9.00",
"Importe_OTPloteo": "11.25"
}
]
]
}
This is the table where I need to print the price and amount
<table class="table table-bordered tablaEditarAgregarPloteo">
<thead>
<tr>
<th>Metros</th>
<th>Precio Final</th>
<th>Acción</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
This is what I have tried to do to add the rows to the table
html = `<tr>`;
html += `<td><input type="text" name="metrosTotalPloteo[]" class="metrosTotalPloteo form-control" value="${data.post[1][i].Precio_OTPloteo}"></td>`;
html += `<td><input type="text" class="form-control importeMetrosPloteo" name="importeMetrosPloteo[]" value="${(data.post[1][i].Importe_OTPloteo)}" readonly></td>`;
html += `<td><a href="#" id="eliminarMetros" value="" class="btn btn-danger btn-icon" type="button"><i class="icon-trash"></i></a></td>`;
html += `</tr>`;
$('.tablaEditarAgregarPloteo').append(html);
$(document).on('click', '#eliminarMetros', function () {
$(this).closest('tr').remove()
calculosMetrosPloteo()
})
Your array is dynamic because you don't know what they come in the json string for that reason the way to go through it is somewhat peculiar you must use recursion and mapping of the array
I have previously left this answer for PHP :
Explanation:
This line:
echo $key . " => " . $value . "<br>";
It would be correct in a one-dimensional array.
Error is due to the fact that at some point you are doing
echo
to$value
and it is a variable of typearray
.I recommend you use recursion; I recently updated this answer in another post that should help you: Link
the String:
the function that does the magic:
Output:
For Javascript:
I cannot add how to print the data in your table, since you did not leave that part of the code, which does not work for you or gives you an error; but this is the easiest and most convenient way to iterate a dynamic JSON string
If you need other languages you can use this link as a reference:
Documentation
You pretty much had it ready, I don't know exactly what was failing you, I just made some adjustments: