Write to XML file with certain formatting

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
JimiH
Forum Commoner
Posts: 92
Joined: Thu Jun 15, 2006 6:10 am

Write to XML file with certain formatting

Post by JimiH »

Hello

I have the following PHP code which produces a table with "Subcategory" & "Time"

Code: Select all

include("conn.php");


$result = mysql_query("SELECT subcategory, SUM(time) FROM live inner join category on cat = cat_id inner join subcategory on subcat = subcat_id where cat = '1' AND Date BETWEEN '2006-10-01'AND'2009-10-04' GROUP BY subcat") or die(mysql_error());

echo "<table Align ='center' border='1'>"; 
echo "<tr> <th>Sub Category</th> <th>Time</th></tr>";

 // keeps getting the next row until there are no more to get 
while($row = mysql_fetch_array( $result )) { 
    
    
// Print out the contents of each row into a table 
echo "<tr><td>"; 
echo $row['subcategory']; 
echo "</td><td>"; 
echo $row['SUM(time)']; 
echo "</td><td>"; 

 } 
echo "</table>";
I wish to write the values to an XML file with certain formatting as follows

Code: Select all

<Pie title="Sustaining Engineering YTD">
<Data title="Manufacturing" value="0.60" pullOut="true"/>
<Data title="Administration" value="0.60" pullOut="false"/>
<Data title="Approvals" value="0.40" pullOut="false"/>
<Data title="IT" value="2.40" pullOut="false"/>
<Data title="Engineering Documents" value="3.60"/>
</Pie>
The "Data title=" are the "subcategory" and the "value=" is the "Time"

Hope this makes sence

Thanks

geoff
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

What problem are you having?
JimiH
Forum Commoner
Posts: 92
Joined: Thu Jun 15, 2006 6:10 am

Post by JimiH »

I dont know how to write the values to an XML file with the following formating.

Code: Select all

<Pie title="Sustaining Engineering YTD"> 
<Data title="Manufacturing" value="0.60" pullOut="true"/> 
<Data title="Administration" value="0.60" pullOut="false"/> 
<Data title="Approvals" value="0.40" pullOut="false"/> 
<Data title="IT" value="2.40" pullOut="false"/> 
<Data title="Engineering Documents" value="3.60"/> 
</Pie>
Thanks

geoff
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Well, without using an XML extension, it would be done with fopen() + fwrite() + fclose().

I'm not seeing how you are extracting the attribute values however.
JimiH
Forum Commoner
Posts: 92
Joined: Thu Jun 15, 2006 6:10 am

Post by JimiH »

The values are extracted via this PHP

Code: Select all

// keeps getting the next row until there are no more to get 
while($row = mysql_fetch_array( $result )) { 
    
    
// Print out the contents of each row into a table 
echo "<tr><td>"; 
echo $row['subcategory']; 
echo "</td><td>"; 
echo $row['SUM(time)']; 
echo "</td><td>"; 

 }
Each time it loops through the query I would like to write the result to the XML file like this

<Data title="Manufacturing" value="0.60" pullOut="true"/>

Where Manufacturing is the 'subcategory' and the value is the 'SUM(time)'

It will keep on adding lines to the XML until the query has no more records.

Thanks

Geoff
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Okay, so what have you tried with the links I've posted thus far?
JimiH
Forum Commoner
Posts: 92
Joined: Thu Jun 15, 2006 6:10 am

Post by JimiH »

Yes, I'm working my way through now.

Thanks for the tips

Geoff
JimiH
Forum Commoner
Posts: 92
Joined: Thu Jun 15, 2006 6:10 am

Post by JimiH »

Right nearly there, I just cant get one piece of info ("SUM(time)") to come out in the XML

It is wrote as "Array SUM(time)"

Code: Select all

while($row = mysql_fetch_array( $result )) { 
    
$somecontent = "<Data title=\"$row[subcategory]\" value=\"$row SUM(time)\" pullOut=\"true\"/>"; 
 
if (fwrite($handle, $somecontent) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
The XML is produced as follows

Code: Select all

<Pie title="Sustaining Engineering" YTD>
<Data title="Manufacturing" value="Array SUM(time)" pullOut="true"/>
<Data title="Administration" value="Array SUM(time)" pullOut="true"/>
<Data title="Repairs" value="Array SUM(time)" pullOut="true"/>
<Data title="Approvals" value="Array SUM(time)" pullOut="true"/>
<Data title="IT" value="Array SUM(time)" pullOut="true"/>
<Data title="Engineering Documents" value="Array SUM(time)" pullOut="true"/>
So the $row[subcategory] (Manufacturing, Administration etc) is working but the $row SUM(time) is producing "Array SUM(time)"

Hope you can help

Thanks

Geoff
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You're missing something. Some that's in "$row[subcategory]" and not in "$row SUM(time)"
JimiH
Forum Commoner
Posts: 92
Joined: Thu Jun 15, 2006 6:10 am

Post by JimiH »

Fixed it, I put the value in a variable, "$sumtime"

Code: Select all

while($row = mysql_fetch_array( $result )) { 

$sumtime = $row['SUM(time)'];       
    
$somecontent = "<Data title=\"$row[subcategory]\" value=\"$sumtime\" pullOut=\"true\"/>"; 
 
if (fwrite($handle, $somecontent) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
Now it works fine.

Another question regarding the destination XML file

Code: Select all

$filename = 'sustaining.xml';
How do I specify a specific directory.

Thanks

Geoff
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

relative path: path/to/directory/filename ( relative to the current working directory, see http://de2.php.net/getcwd )
absolute path: /path/to/directory/filename
Post Reply