2009-12-05 62 views
0

我目前在我的項目中使用Fusioncharts Free,它需要XML以特定格式顯示數據和顯示圖表。爲PHP生成XML Web服務數據的最佳實踐

我有一個PHP文件名爲webservice.php其中有下面的許多類似的重複:

$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 
} 

的輸出XML可能看起來像這樣:

<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> 

這已經成爲一個有點維護噩夢來更新此代碼,但不可能使用標準格式(例如REST)傳輸數據,因爲Fusioncharts需要特定的XML屬性。有更好的方法來處理上述問題嗎?我可以輕鬆改進代碼的模塊化和可維護性嗎?

回答

2

我有幾個想法可以考慮。

#1
使用模板引擎,像Smarty,很可能會令這一點更容易維護。它至少會擺脫你的PHP代碼中的XML。

例如,您可以創建的XML模板段,您發佈的評論:

<?xml version="1.0" encoding="UTF-8" ?> 
{foreach from=$graphs item=graph} 
    <graph caption="{$graph.caption}" showNames="{$graph.show_names}" decimalPrecision="{$graph.decimal_precision}" bgcolor="{$graph.bg_color}"> 
    {foreach from=$graph.set item=set} 
     <set name="{$set.name}" value="{$set.value}"/> 
    {/foreach} 
    </graph> 
{/foreach} 

而且從PHP稱其爲

<?php 
$address = $_SERVER['PHP_SELF']; 

$smart = new Smarty(); 
$graphs = array(); 

if ($address == 'webService.php/fcf/last5pricechanges/') 
{ 
    $graph_result = mysql_query("SELECT caption, show_names, decimal_precision, bg_color 
           FROM graph WHERE something='something else'"); 
    while($graph_row = mysql_fetch_assoc($graph_result)) 
    { 
     $graph_row; 
     $set_result = mysql_query("SELECT name, value FROM set WHERE graph_id = {$graph_row['id']}"); 
     while($set_row = mysql_fetch_assoc($set_result)) 
     { 
      $graph_row['sets'][] = $set_row; 
     } 
     $graphs[] = $graph_row; 
    } 
} 

$smarty->assign('graphs', $graphs); 
$smarty->display('graph_template.tpl'); 
?> 

#2
您可以創建對象幫助您管理代碼。例如,像以前一樣產生相同的XML輸出,你可以這樣做:

<?php 
class Graph 
{ 
    protected $caption; 
    protected $show_names; 
    protected $decimal_precision; 
    protected $bg_color; 

    protected $sets; 

    public function __construct($graph_id) 
    { 
     $graph_result = mysql_query("SELECT caption, show_names, decimal_precision_bg_color 
           FROM graph WHERE something='something else'"); 
     while($graph_row = mysql_fetch_assoc($graph_result)) 
     { 
      list($this->caption, $this->show_names, $this->decimal_precision, $this->bg_color) = $graph_result; 
      $set_result = mysql_query("SELECT name, value FROM set WHERE graph_id = {$graph_row['id']}"); 
      while($set_row = mysql_fetch_assoc($set_result)) 
      { 
       $this->sets[] = $set_row; 
      } 
     } 
    } 

    public function fetch_xml() 
    { 
     $output = '<?' . 'xml version="1.0" encoding="UTF-8" ?' . '>'; 
     $output .= "<graph caption=\"{$this->caption}\" showNames=\"{$this->show_names}\" decimalPrecision=\"{$this->decimal_precision}\" bgcolor=\"{$this->bg_color}\">\n"; 
     foreach($this->sets as $set) 
     { 
      $output .= "<set name=\"{$set->name}\" value=\"{$set->value}\"/>\n"; 
     } 
     $output .= "</graph>"; 
     return $output; 
    } 
} 
?> 

,並調用它在你的主代碼,如:

<?php 
$address = $_SERVER['PHP_SELF']; 
if ($address == 'webService.php/fcf/last5pricechanges/') 
{ 
    $graph = new Graph(1); 
    echo $graph->fetch_xml(); 
} 
?> 

#3
你可以嘗試使用的東西像SimpleXML,但我懷疑這將有助於很大程度的可維護性,因爲它將與回聲方法一樣冗長。

#4
...不,我全力以赴:-) 希望有所幫助。

+0

感謝您的回答。 Smarty似乎是要走的路,因爲我可以爲每個圖創建模板,使修改變得更容易。 – CJD 2009-12-06 16:15:17