开发者

PHP or Javascript to format data into displayable output?

I'm creating a little web app to play videos. Data about the videos will be stored in a MySQL database. One of the features of the app will be google-instant-style results as you type. I'm using AJAX on my search page to call on a php script which will query the sql database and return relevant videos. I would like the video results to be displayed youtube-style: so, a thumbnail of the video, a big clickable title, info in smaller font below the title, and maybe a drop-down menu of actions to do with that video.

My question is essentially this: what should I use to transform the video data into a displayable result (i.e. HTML code)? PHP or Javascript?

Should I:

A.) Use php to query the sql database, format the result as xml and return it as the AJAX response ... then use javascript to turn the xml into the HTML th开发者_开发技巧at will display the snazzy youtube-style thing I described above.

OR

B.) Do everything in php (query database and create the HTML code), and have javascript just copy and paste the AJAX response into the webpage.

Any advice appreciated!


I prefer getting and packaging information into JSON then delivering that to a receiving AJAX Request and let the JavaScript generate the HTML. Use JSON rather than XML, JSON is easier to handle and transport. But to answer your question it is a lot easier to generate HTML Client Side rather than using PHP scripts to generate HTML, I learned that the hard way.


Option A is the better choice because this will separate the data from the presentation. If you ever want to change the layout or design, you will only need to edit the javascript and HTML instead of the PHP file that is serving the XML data.


All you really need from youtube is the player code, which may or may not be uniform for every video depending on your preference. If it stays the same every time, then just embed the player code in your html file and retrieve the video id via ajax. You can then update the video id live in your player code via javascript.


It depends on what you plan to do with the data. If you simply want to display the data, go for option B. If you're probably going to manipulate it after loading the data, go for option A.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜