我正在尝试从mysql数据库中提取数据以创建一个json文件,以便与timelinejs时间轴一起使用。问题是json文件必须以某种方式格式化。我已经创建了以下代码来正确格式化json,但它只从我的数据库中拉出一个条目(特别是最后一个条目)。我们非常感谢您提供的任何帮助!
<?php
$link = mysql_pconnect("localhost", "root", "********") or die("Could not connect");
mysql_select_db("php_test") or die("Could not select database");
$rs = mysql_query("SELECT * FROM timelinetest");
while($row = mysql_fetch_array($rs, MYSQL_ASSOC))
$object = array
('timeline'=>
array(
'headline'=>'Georgia History Title Page',
'type'=>'default',
'text'=> 'Testing Overview',
'startDate'=>'1700',
'asset'=>array('media'=>'titlepagemedia', 'credit'=>'titlepagecredit', 'caption'=>'titlepagecaption'),
'date'=>array(array('startDate'=>($row['startDate']), 'endDate'=>($row['endDate']), 'headline'=>($row[ 'headline']), 'text'=>($row['text']), 'tag'=>'', 'asset'=>array('media'=>($row['media']), 'credit'=>($row[ 'credit']), 'caption'=>($row['caption']))), ),
'era'=>array(array('startDate'=>'', 'endDate'=>'', 'headline'=>'', 'text'=>'', 'tag'=>''))
)
);
$json = json_encode($object);
file_put_contents("testing.json", $json);
?>
<head>
<!-- jQuery -->
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<!-- BEGIN TimelineJS -->
<script type="text/javascript" src="http://cdn.knightlab.com/libs/timeline/latest/js/storyjs-embed.js"></script>
<script>
$(document).ready(function() {
createStoryJS({
type: 'timeline',
width: '800',
height: '600',
source: 'http://localhost/php_test/timeline_test/testing.json',
embed_id: 'my-timeline'
});
});
</script>
<!-- END TimelineJS -->
答案 0 :(得分:0)
在while循环中,您将结果向量$row
的内容分配(=覆盖)到同一个(奇异!)对象$object
。不熟悉TimelineJS我假设你需要在$object['timeline']
中建立一个你可以用
while($row = mysql_fetch_array($rs, MYSQL_ASSOC)) {
$object['timeline'][]=array(
'headline'=>'Georgia History Title Page',
'type'=>'default',
'text'=> 'Testing Overview',
'startDate'=>'1700',
'asset'=>array('media'=>'titlepagemedia', 'credit'=>'titlepagecredit', 'caption'=>'titlepagecaption'),
'date'=>array(array(
'startDate'=>($row['startDate']),
'endDate'=>($row['endDate']),
'headline'=>($row['headline']),
'text'=>($row['text']),
'tag'=>'',
'asset'=>array('media'=>($row['media']),
'credit'=>($row['credit']),
'caption'=>($row['caption'])))),
'era'=>array(array('startDate'=>'', 'endDate'=>'', 'headline'=>'', 'text'=>'', 'tag'=>''))
);
}
这与array_push($object['timeline'], ...)
基本相同,它会将每个结果行的数据附加到'timeline'
中的数组$object
。
修改强>
现在循环将填充date
内的数组era
和$object['timeline']
(尽管era
中没有真实数据):
$object=array('timeline'=>array(
'headline'=>'Georgia History Title Page',
'type'=>'default',
'text'=> 'Testing Overview',
'startDate'=>'1700',
'asset'=>array('media'=>'titlepagemedia', 'credit'=>'titlepagecredit', 'caption'=>'titlepagecaption'),
'date'=>array(),'era'=>array()
));
while($row = mysql_fetch_array($rs, MYSQL_ASSOC)) {
$object['timeline']['date'][]=array(
'startDate'=>($row['startDate']),
'endDate'=>($row['endDate']),
'headline'=>($row['headline']),
'text'=>($row['text']),
'tag'=>'',
'asset'=>array('media'=>($row['media']),
'credit'=>($row['credit']),
'caption'=>($row['caption'])));
$object['timeline']['era'][]=array('startDate'=>'', 'endDate'=>'', 'headline'=>'', 'text'=>'', 'tag'=>'');
}
也许这有帮助?