php - pulling mysql data for timelinejs? -
i'm trying pull data mysql database create json file use timelinejs timeline. problem json file must formatted way. i've created following code formats json correctly, pulling 1 entry database (specifically last entry). guys offer appreciated!
<?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 * 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 -->
in while loop keep assigning (=overwriting) contents of result vector $row
same (singular!) object $object
. not being familiar timelinejs assume need build array in $object['timeline']
with
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'=>'')) ); }
this same array_push($object['timeline'], ...)
, append data each of result rows array 'timeline'
in $object
.
edit
now loop fill arrays date
, era
within $object['timeline']
(although there no real data in 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'=>''); }
maybe helps?
Comments
Post a Comment