Best practices when generating XML web services data for PHP -
I am currently using Fusioncharts in my project, for which XML should be in a specific format, data display
I have a PHP file named webservice.php that has many similar iterations below:
$ address = $ _SERVER [ 'PHP_SELF']; ... if ($ address == 'webService.php / fcf / last5pricechanges /') // // To query the MySQL db and export string in the browser to output the output variable to at least every line of XML Line output by line}
Outputed XML can look like this:
& Lt; Set name = "abc.com" value = "5" /> & Lt; / Graph & gt; This is a maintenance nightmare to update this code, however afaik is that it will not be possible to transfer data using standard format - as in the rest of the form, Fusionerts require specific XML properties. Are there better ways to handle the above? Can I easily improve the humility and maintenance of my code?
I have some ideas that you can think of.
# 1
Using a template engine, like it might be a bit easier to maintain, it can at least get rid of XML from your PHP code. For example, you could create a template for the XML snippet you posted: {/ Foreach} & lt; / Graph & gt; {/ Foreach}
and call it from PHP
# 2
You can create an object to help you manage the code. For example, to generate similar XML output, you could:
& lt ;? Php class graph {secure $ caption; $ Show_names protected; Protected $ decimal_complete; Protected $ bg_color; Protected $ set; Public function __construct ($ graph_id) {$ graph_result = mysql_query ("select caption, show_name, decimal_prescription_bug_guol, where something = 'some more'"); While ($ graph_row = mysql_fetch_assoc ($ graph_result)) {list ($ this-> caption, $ this-> show_name, $ this-> decimal_primation, $ this- & gt; bg_color) = $ graph_result; $ Set_result = mysql_query ("select name, set with value WHERE graph_id = {$ graph_row ['id']}"); While ($ set_row = mysql_fetch_assoc ($ set_result)) {$ this- & gt; Sets [] = $ set_row; }}} Public function fetch_xml () {$ output = '& lt ;?' . 'Xml version = "1.0" encoding = "UTF-8"?' . '& Gt;'; $ Output = "& Gt; Graph Caption = \" {$ this-> Caption} \ "showname = \" {$ this-> Show_name} \ "decimalprice = \" {$ this-> Decimal_paration} \ "bgcolor = \" {$ this- & gt; Bg_color} \ "& gt; \ n"; $ {$ Set-> Name} \ "value = \" {$ set-> Value} \ "/ $ {$ set-> \ n"; } $ Output. = "& Lt; / graph>"; Return $ output; }}? & Gt;
And call it in your main code:
& lt; Php $ address = $ _SERVER ['PHP_SELF']; If ($ address == 'web service .php / fcf / last5pricechanges /') {$ graph = new graph (1); $ Graph-> Fetch_xml (); }? & Gt; You can try to do something like this, but I suspect that maintenance will be of great help, because it is only for verbose As the echo method
and # 4
... No, I am everything :-) Hope that helps.
Comments
Post a Comment