0

jsonファイルのようなものを見せてくれる人を作りたいです。これは真似したいJSONファイルですが、PHPとMYSQLで

{
        "success": 1,
        "result": [
                {
                        "id": "293",
                        "title": "This is warning class event",
                        "url": "http://www.example.com/",
                        "class": "event-warning",
                        "start": "1362938400000",
                        "end":   "1363197686300"
                },
                {
                        "id": "294",
                        "title": "This is information class ",
                        "url": "http://www.example.com/",
                        "class": "event-info",
                        "start": "1363111200000",
                        "end":   "1363284086400"
                },
                {
                        "id": "297",
                        "title": "This is success event",
                        "url": "http://www.example.com/",
                        "class": "event-success",
                        "start": "1363284000000",
                        "end":   "1363284086400"
                }

それから私はしばらくの間作り続けます

$link=mysql_connect("localhost", "user", "pass");

    mysql_select_db("db",$link) OR DIE ("Error: No es posible establecer la conexión");

    mysql_set_charset('utf8');

    $eventos= mysql_query("SELECT * from eventos",$link);

    echo ("{'success': 1, 'result': [");

    while($matrizu=mysql_fetch_array($eventos))
    {


        $evento=$matrizu["id"];
        $nombre=$matrizu["name"];
        $clase=$matrizu["categoria"];
        $inicio=$matrizu["datetime"];
        $final=$matrizu["end"];

        echo ('"{"  
                "id": ".$evento.",
                "title": ".$nombre.",
                "url": "http://www.example.com",
                "class": ".$clase.",
                "start": ".$inicio.",
                "end": ".$final."

            "}",');
     }

    echo("        
        ]
}");

しかし、JSONファイルのように見えるため、文字をエスケープできません。これは、このBootstrap カレンダー用です。

そのカレンダーの作者は、次のようなことをしなければならないと言っています。

<?php
$db    = new PDO('mysql:host=localhost;dbname=testdb;charset=utf8', 'username', 'password');
$start = $_REQUEST['from'] / 1000;
$end   = $_REQUEST['to'] / 1000;
$sql   = sprintf('SELECT * FROM events WHERE `datetime` BETWEEN %s and %s',
    $db->quote(date('Y-m-d', $start)), $db->quote(date('Y-m-d', $end)))

$out = array()
foreach($db->query($sql) as $row) {
    $out[] = array(
        'id' => $row->id,
        'title' => $row->name,
        'url' => Helper::url($row->id),
        'start' => strtotime($row->datetime) . '000'
    );
}

echo json_encode($out);
exit;

そしてどちらも機能していません

4

1 に答える 1

0

あなたが示している2番目のオプションは、それを生成する組み込みのPHP関数がある場合、正しくフォーマットされた/エスケープされたJSONを手動で作成しようとしてもまったく意味がありません。構造的には、配列をキーsuccessresultキーで開始し、データベースの結果をキーにプッシュする必要がありresultます。

$out = array(
    'success' => 1,
    'result' => array()
);

resultこれで、値を簡単に取得してキーにプッシュできます。

foreach($db->query($sql) as $row) {
    $out['result'][] = array(
        'id' => $row['id'],
        'title' => $row['name'],
        'url' => Helper::url($row['id']), // up to you whether this is correct here...
        'class' => $row['categoria']
        'start' => $row['datetime'],
        'end' => $row['end']
    );
}

それはあなたの最初の試みを取り、変数を2番目の例に差し込んでいるだけです-これは PDOクエリマニュアル FYIです。補足として、名前付きのテーブルにフィールドを持たないでください。datetimeこれは予約語ではありませんが、列タイプであるため、良い習慣でもありません。

今すぐ行く:

echo json_encode($out);

...そして、あなたが求めているJSONが必要です。

于 2014-02-21T01:45:45.313 に答える