主程序guestbook.php
<?php
//==========================================
// 文件名: guestbook.php
// 说 明: 简单的XML留言簿
// power by ice_berg16
//==========================================
class guestbook extends DOMDocument
{
public function __construct()
{
parent:: __construct();
if (!file_exists("guestbook.xml"))
{
$xmlstr = "<?xml version='1.0' encoding='GB2312'?><messageList></messageList>";
$this->loadXML($xmlstr);
$this->save("guestbook.xml");
}
else
$this->load("guestbook.xml");
}
//==========================================
// 函数: addMessage
// 功能: 添加一条留言
// 参数: $userName 留言人名称
// 参数: $message 留言内容
// 参数: $postTime 留言时间
// 返回: 1 成功 0 失败
//==========================================
public function addMessage($userName, $content, $postTime)
{
$root = $this->documentElement;
$id = $this->getElementsByTagName("id")->length+1;
$Nid = $this->createElement("id");
$text = $this->createTextNode(iconv("GB2312","UTF-8",$id));
$Nid->appendChild($text);
$NuserName = $this->createElement("userName");
$text = $this->createTextNode(iconv("GB2312","UTF-8",$userName));
$NuserName->appendChild($text);
$Ncontent = $this->createElement("content");
$text = $this->createTextNode(iconv("GB2312","UTF-8",$content));
$Ncontent->appendChild($text);
$NpostTime = $this->createElement("postTime");
$text = $this->createTextNode(iconv("GB2312","UTF-8",$postTime));
$NpostTime->appendChild($text);
$Nmessage = $this->createElement("message");
$Nmessage->appendChild($Nid);
$Nmessage->appendChild($NuserName);
$Nmessage->appendChild($Ncontent);
$Nmessage->appendChild($NpostTime);
$root->appendChild($Nmessage);
if ($this->save("guestbook.xml"))
{
return 1;
}
else
return 0;
}
//==========================================
// 函数: delMessage($id)
// 功能: 根据信息ID删除一条信息
// 参数: $id 留言编号
// 返回: 1 成功 0 失败
//==========================================
function delMessage($id)
{
$root = $this->documentElement;
$xpath = new DOMXPath($this);
$Nmessage = $xpath->query("//message[id=$id]");
if ($root->removeChild($Nmessage->item(0)))
{
$this->save("guestbook.xml");
return 1;
}
else
return 0;
}
}
if (isset($_POST['add']))
{
$guestbook = new guestbook;
$now = date("Y-m-d H:i:s");
if ($guestbook->addMessage($_POST['userName'],$_POST['content'],$now))
{
echo "<script>alert('添加成功');location.href='".$_SERVER['PHP_SELF']."'</script>";
}
}
if (isset($_GET['method']) && $_GET['method'] == "del")
{
$guestbook = new guestbook;
if ($guestbook->delMessage($_GET['id']))
{
echo "<script>alert('删除成功');location.href='".$_SERVER['PHP_SELF']."'</script>";
}
}
else
{
$script = <<<SCRIPT
<script>
var xml = new ActiveXObject("Msxml2.DOMDocument");
var xsl = new ActiveXObject("Msxml2.DOMDocument");
xml.async = false;
xml.load("guestbook.xml");
xsl.async = false;
xsl.load("guestbook.xsl");
document.write(xml.transformNode(xsl));
</script>
SCRIPT;
$form = <<<FORM
<form method='post' action='guestbook.php'>
<input type=text name='userName' size=20><br/>
<textarea name='content' cols=50 rows=5></textarea><br/>
<input type='submit' name='add' value='添加留言'>
FORM;
echo $script;
echo $form;
//*/
}
?>
-------样式表guestbook.xsl-----------------
<?xml version='1.0' encoding='GB2312'?>
<xsl:stylesheet version='1.0' xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/messageList">
<table style='font-size:12px;' width='60%' cellpadding='5' cellspacing='1' bgcolor='#999999'>
<xsl:for-each select='message'>
<tr>
<td style='color:#FFFFFF'>
<xsl:value-of select="userName" />
 At 
<xsl:value-of select="postTime" />
     
<xsl:element name="A">
<xsl:attribute name="href">guestbook.php?method=del&id=<xsl:value-of select="id" /></xsl:attribute>
删除
</xsl:element>
</td>
</tr>
<tr bgcolor='#FFFFFF'>
<td><xsl:value-of select="content" /></td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=53464