Hi there,
I' ve got some data in my database and I' m using PHP to fetch it and would like to transport it to client side via JSON.
The problem that I'm facing is the folowing:
I have a timeline which was built in html and css and I would like to store the data from the database to the timeline. What is the best approach on tackling such a problem?
please consider that I have to do some sorting on that timeline (I would like to store the data on the timeline depending on the year, going from the latest to the oldest year).

I'm not asking for any code or so just to give me some suggestions on how to tranfer the data from server to the client, manipulating at the client side ect.

generally, the way this works -- the you write your connection + query in PHP, gather your data, and use PHP to format the output. This can be its very own .php page, or a library, or however you see fit.

Since PHP will format your output (and set the appropriate headers for the data type), all you will need to do is call that page via an AJAX call, and either JSON.parse the text response from PHP or hope that the browser understands the headers and believes you that it's JSON data as the return.

Once you have your data in JSON format, you can use it as an object (or, any of the data types that your data is in, be it an array or just a parameter block).

You can use Javascript to iterate through your data, set whatever needs to be set, and allow your styles to control layout as needed.

Of course, if this is a one off page, it may just be easier to have PHP do all of this and not need the AJAX call at all. AJAX is for dynamic pages that need to update without a full page refresh.

Member Avatar for diafol

I'd add that you just grab the data that you need to display. If you have a kind of 'display ealier...' type link then you load the next lot of data via Ajax. If you have a lot of data, you probably don't want to display it all in one go to create a page that goes on forever.

THis implementation is similar to 'pagination', except here you grab the next 'page' of data via Ajax and append it to the end of the existing data instead of reloading the page with neww data. Just an idea.

if you need to further process data client side you can generate an js array which contains json object

`$js="list = new Array();\n";
$index=0;
while ($row)`{
  $js.="list[".$index."]=".json_encode($row).";\n";
}
//in the page just print $js
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.