从mysql数据库以正确的方式格式化JSON

I have this database:

enter image description here

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>
查看全部
dongzuan4860
dongzuan4860
2014/10/30 16:55
  • json
  • php
  • pdo
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

2个回复