我想花点时间显示类似 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;
而且也不工作