我有一个SQL数据库,我正在对它执行以下查询。
$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);
这将按以下格式检索上传者及其上传的照片数量。
Uploader Number of photos uploaded
mun 20
ris 10
moz 5
我想要的是PHP脚本来创建XML文件。它必须实际将其保存在我的目录中。我使用SimpleXML来做到这一点。
这就是我做的..
$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');
}
其中photo.xml是保存XML节点的文件。它们采用这种格式。
<?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>
现在假设用户“Moz”上传了一张新照片。他的照片计数器最初是2,应该改为3。 XML文件应该反映这种变化吗?它确实是因为它从数据库中提取数据(还记得查询吗?)但这就是问题所在。
<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>
它也会复制其他节点,从而在XML文件中创建重复。
我想到的是
有谁知道怎么做?用php或javascript?
答案 0 :(得分:2)
所以你在写/重写之前问你如何'清空'photo.xml文件?这是对的吗?
我不熟悉SimpleXML,所以我不知道你是否可以使用它,但你可以随时使用:
$f = fopen("photo.xml", "w");
fclose($f);
这将:“仅用于写入;将文件指针放在文件的开头并将文件截断为零长度。如果文件不存在,请尝试创建它。”
(见:http://www.php.net/manual/en/function.fopen.php)
修改强>
在仔细观察SimpleXML之后,您可能希望有2个文件,1个您将加载的xml文档的模板,第二个将是您保存的文件(photo.xml)。使用带有'w'标志的fopen将清除内容,但是你也将丢失你的xml结构。
实际上,你甚至可能根本不需要使用fopen ......
$xmlDoc=loadXMLDoc("photo_template.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');
}
其中photo_template.xml如下所示:
<?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">
</users>
而photo.xml最终会像:
<?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>