I have this database:
I need to get this JSON output with php pdo:
{
"data": [
{
"ID": "<div class=\"btn btn-danger\">1</div>",
"naziv": "some data from database",
"vrsta": "some data from database",
},
{
"ID": "<div class=\"btn btn-danger\">2</div>",
"naziv": "some data from database",
"vrsta": "some data from database",
}
]
}
So as you can see I need to modify data before JSON encoding...I need to add some html and css.
I try to do this with:
/* select all the weekly tasks from the table googlechart */
$result = $db->prepare('SELECT ID,naziv,vrsta FROM investicije');
$result->execute();
/* Extract the information from $result */
foreach($result as $r) {
$temp = array();
// the following line will be used to slice the Pie chart
$temp['ID'] = '<div class="btn btn-danger">'.$r['ID'].'</div>';
$temp['vrsta'] = $r['vrsta'];
$temp['naziv'] = $r['naziv'];
}
$output = ['data' => $temp];
$jsonTable = json_encode($output);
This does not render the right JSON format as I put above.
UPDATE:
JS:
$(document).ready(function() {
$('#example').dataTable( {
"ajax": "table1.php",
"columns": [
{ "data": "ID" },
{ "data": "naziv" },
{ "data": "vrsta" },
]
} );
} );
HTML
<div class="container">
<table id="example" class="table table-striped table-bordered table-responsitive" cellspacing="0" width="100%">
<thead>
<tr>
<th>ID</th>
<th>Naziv</th>
<th>Vrsta</th>
</tr>
</thead>
<tfoot>
<tr>
<th>ID</th>
<th>Naziv</th>
<th>Vrsta</th>
</tr>
</tfoot>
</table>
</div>