Abhi
Abhi

Reputation: 5561

Generating XML file through PHP results in a minor issue . Please help

In order to generate a XML i am using following Code currently.

   .<?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;
        }        
}

?>

Now in this code i want to replace all echo statements with a String variable and then i want to write that String variable content into a file , the porblem is if i define the string variable at the top of the file even then also that string variable cannot accessible from the giveItems functions below.

Pls suggest some solution

Upvotes: 0

Views: 89

Answers (1)

Rudi Visser
Rudi Visser

Reputation: 21969

To access a global variable from within a function you need to write global $varname; at the top of that function's definition. Example:

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

However, this is pretty bad practice nowadays and you'd be better off writing a class that will both produce the XML and write it to a file.

Upvotes: 1

Related Questions