I have a SQL database and i am performing the following query on it.
$statistics="SELECT firstname AS 'user', count( * ) AS 'num',member_id AS ID
FROM members
JOIN member_photo
USING ( member_id )
GROUP BY firstname ";
$result= mysql_query($statistics);
This will retrieve the uploaders and the number of photos they have uploaded in the following format.
Uploader Number of photos uploaded
mun 20
ris 10
moz 5
What i want is for a PHP script to create the XML file. It has to actually save it in my directory. I used SimpleXML to do that.
This is what i did..
$xmlDoc=loadXMLDoc("photo.xml");
$statistics="SELECT firstname AS 'user', count( * ) AS 'num',member_id AS ID
FROM members
JOIN member_photo
USING ( member_id )
GROUP BY firstname ";
$result= mysql_query($statistics);
while($row=mysql_fetch_assoc($result)) {
$uploader=$xml->addChild('uploader');
$uploader->addAttribute('ID',$row['ID']);
$uploader->addChild('Name', $row['user']);
$uploader->addChild('Photos_Uploaded', $row['num']);
$xml->asXML('photo.xml');
}
where photo.xml is the file the XML nodes are being saved to. They are in this format.
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="user.xsl"?>
<users xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="users.xsd">
<uploader ID="11"><Name>moz</Name><Photos_Uploaded>2</Photos_Uploaded> </uploader>
<uploader ID="13"><Name>Mun</Name><Photos_Uploaded>6</Photos_Uploaded></uploader>
<uploader ID="12"><Name>ris</Name><Photos_Uploaded>10</Photos_Uploaded></uploader>
</users>
Now suppose user "Moz" uploads a new photo. His photo counter is initially 2 which should change to three. The XML file should reflect this change right? It does since it's pulling data from the database (remember the query?) but here's the problem.
<uploader ID="11"><Name>moz</Name><Photos_Uploaded>2</Photos_Uploaded> </uploader>
<uploader ID="13"><Name>Mun</Name><Photos_Uploaded>6</Photos_Uploaded></uploader>
<uploader ID="12"><Name>ris</Name><Photos_Uploaded>10</Photos_Uploaded></uploader>
moz3
<uploader ID="13"><Name>Mun</Name><Photos_Uploaded>6</Photos_Uploaded></uploader>
<uploader ID="12"><Name>ris</Name><Photos_Uploaded>10</Photos_Uploaded></uploader>
It replicates the other nodes too which creates duplication into the XML file.
What i thought of was to
- first clear the file before populating the XML file again.
Anybody knows how to do that? with php or javascript?