I’m currently using Fusioncharts Free in my project which requires XML to be in a specific format, both for the display of data and for the appearance of the chart.
I have a PHP file called webservice.php which has many similar repetitions of the below:
$address = $_SERVER['PHP_SELF'];
...
if ($address == 'webService.php/fcf/last5pricechanges/'){
//query MySQL DB and build up XML string in $output variable to output to browser
//building up each line of XML more or less line by line
}
The outputted XML may look like so:
<graph caption="Active Items - Grouped by Site" showNames="1" decimalPrecision="0" bgcolor="eeeeee">
<set name="xyz.co.uk" value="1"/>
<set name="abc.com" value="5"/>
</graph>
It’s becoming a bit of a maintenance nightmare to update this code, yet afaik it wouldn’t be possible to transfer data using a standard format – such as REST, as Fusioncharts requires specific XML properties. Are there better ways to handle the above? Can I improve the modularity and maintainability of my code easily?
I’ve got a couple of ideas you could consider.
#1
Using a template engine, like Smarty, would probably make this a little easier to maintain. It would at least get rid of the XML from your PHP code.
For example, you could create a template for the XML snippet you posted:
And call it from PHP as
#2
You could create objects to help you manage the code. For example, to generate the same XML output as before, you could do:
And call it in your main code like:
#3
You could try using something like SimpleXML, but I doubt that would help much with the maintainability, as it would be just as verbose as the echo method
And #4
… nope, I’m all out 🙂
Hope that helps.