通过PHP生成XML文件会导致一个小问题。请帮忙

通过PHP生成XML文件会导致一个小问题。请帮忙,php,xml,Php,Xml,为了生成XML,我目前正在使用以下代码 .<?php require ('../dbconfig/dbconfig.php'); $appId = $_GET['id']; $sqlTabs = "Select _id,tab_title,position from tab_info where app_id=$appId order by position "; $resultTabs=mysql_query($sqlTabs ); $

为了生成XML,我目前正在使用以下代码

   .<?php

    require ('../dbconfig/dbconfig.php');

    $appId = $_GET['id'];

    $sqlTabs = "Select _id,tab_title,position from tab_info where app_id=$appId order by position ";
    $resultTabs=mysql_query($sqlTabs );
    $countTabs=mysql_num_rows($resultTabs);

    if($countTabs>0)
    {
        echo '<application applicationName="sample app">';
        echo '<tabs>';
        while ($rowTab = mysql_fetch_array($resultTabs) ) {


            echo '<tab id="t'.$rowTab['_id'].'" tabtitle="'.$rowTab['tab_title'].'" position="'.$rowTab['position'].'" data="somedata">';

            $sqlTabItems =  "Select _id as tabitemId,item_type,item_title,item_data from items_info where tab_id=".$rowTab['_id']." and parent_id=0 order by _id ";
            $resultTabItems=mysql_query($sqlTabItems);
            $countTabItems=mysql_num_rows($resultTabItems);
            if($countTabItems>0)
            {
               $level = 1; 
               echo '<listItems>';
                while ($rowTabItem = mysql_fetch_array($resultTabItems) ) {

                        echo '<listItem id="'.$rowTabItem['tabitemId'].'" parentId="t'.$rowTab['_id'].'" itemtype="'.$rowTabItem['item_type'].'" itemtitle="'.$rowTabItem['item_title'].'" itemdata="'.$rowTabItem['item_data'].'" level="'.$level.'">';

                             giveitems($rowTabItem['tabitemId'],$rowTab['_id'],$level);

                        echo '</listItem>';
                }
                echo '</listItems>';

            }
            else
               echo 'No Data';
            echo '</tab>';
        }
        echo '</tabs></application>';
    }
    else
    {
        echo 'No Data';//  no tabs found
    }

function giveitems($pitemId,$tabId,$level)
{

$sqlItems = "Select _id,item_type,item_title,item_data from items_info where parent_id=".$pitemId." order by _id ";

$resultItems=mysql_query($sqlItems);

$countItems=mysql_num_rows($resultItems);

$numbers = 0;

 if($countItems>0)
{
 $level = $level +1;
 echo '<listItems>';

while ($rowItem = mysql_fetch_array($resultItems) ) {

echo '<listItem  id="'.$rowItem[0].'" parentId="'.$pitemId.'" itemtype="'.$rowItem[1].'" itemtitle="'.$rowItem[2].'" itemdata="'.$rowItem[3].'" level="'.$level.'">';


/*********** Recursive Logic ************/
 if(giveitems($rowItem[0],$tabId,$level)==1)
 {
      echo '</listItem></listItems>';
           return 1;
  }    
else 
 {
   echo '</listItem></listItems>';
   return -1;
  }                   
 }         
        } 
        else
        {
                echo 'No Data';//  no tabs found
                return -1;
        }        
}

?>
现在,在这段代码中,我想用一个字符串变量替换所有echo语句,然后我想将该字符串变量内容写入一个文件,问题是如果我在文件的顶部定义了字符串变量,那么该字符串变量也无法从下面的giveItems函数访问


请建议一些解决方案

从需要编写
global$varname的函数中访问全局变量位于该函数定义的顶部。例如:

function giveitems($pitemId,$tabId,$level) {
    global $xmlstring;
    // ....
}

然而,现在这是一种非常糟糕的做法,最好编写一个类,该类将生成XML并将其写入文件。

另一种方法是,可以将字符串作为参数传递给giveitems函数。但正如鲁迪所说,你最好使用一个类。可能是