【问题标题】:Format JSON in right way from mysql database从mysql数据库以正确的方式格式化JSON
【发布时间】:2014-10-30 16:55:05
【问题描述】:

我有这个数据库:

我需要用 php pdo 得到这个 JSON 输出:

{
    "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",
        }
    ]
}

如您所见,我需要在 JSON 编码之前修改数据...我需要添加一些 html 和 css。

我尝试这样做:

/* 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);

这不会像我上面所说的那样呈现正确的 JSON 格式。

更新:

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>

【问题讨论】:

  • 顺便说一句,为什么要在同一个响应中混合直接数据和 HTML 内容。将 HTML DOM 元素的创建留给调用 UI 层。
  • 我该怎么做?将 json 与 DOM 数据混合是一种不好的做法吗?
  • 所以,我假设您使用json_encode() 发送此数据作为对请求(可能是 AJAX 请求)的响应,因为如果只是 PHP 的其他一些区域,则无需序列化它应用程序需要它。假设这个假设是正确的,那么显然还有其他地方可以显示页面的其余 HTML。为什么要在 PHP 脚本中“隐藏”该页面实现的一部分,该脚本仅用于从数据库中检索数据?现在,例如,如果您想更改 div 上的类,则必须修改 PHP 脚本。
  • 这与简单地将 ID 值与您的数据一起传递并让调用者将其放在 DOM 元素的上下文中相反。使用这种方法,您的所有显示逻辑都将集中在一个地方,并且此脚本只需担心检索请求的数据。
  • 一些帮助我创建 UI 层的教程?

标签: php json pdo


【解决方案1】:

现在您正在覆盖您的 $temp 变量而不使用它。您需要将$output = ['data' =&gt; $temp]; 移动到 foreach 循环中并稍作更改:

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;
}

【讨论】:

  • 谢谢,非常感谢。在 JSON 中混合 html 和数据是一种不好的做法,以及如何解决这个问题?
  • @JamesD。通常您不必将 html 存储在数据库中。如果您在 javascript 中使用返回的 json,您可以只使用数据在其中构建您认为合适的 html。
  • 但我没有将 html 存储在数据库中,我只在 JSON 中添加 html ......正如您从上面的 mysql 数据库中的图片中看到的那样
  • @JamesD。我真的看不出有什么问题,但如果你真的想吹毛求疵,你可能会争辩说你发送的数据比必要的多;您只能发送值并在 javascript 中构建 html。如果你想改变你的 html 结构,你需要在更多的地方做,而不是严格必要的。但如果它适合你:-)
【解决方案2】:

您将在循环的每次迭代中覆盖您的 $temp 数组。你可能想要:

foreach($result as $r) {
    $temp[] = array(....);
         ^^---array push shorthand;
}

改为。

【讨论】:

    猜你喜欢
    • 2013-02-02
    • 2021-12-07
    • 1970-01-01
    • 1970-01-01
    • 2015-12-18
    • 1970-01-01
    • 2020-06-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多