<?php
class Message_XML extends DomDocument{//Message_XML类,继承PHP5的DomCocument类
private $Root;
public function __construct(){
parent::__construct();
if(!file_exists('message.xml')){//创建或读取存取留言信息的XML文档messagexml
$xmlstr = '<?xml version="1.0" encoding="UTF-8"?><message></message>';
$this->loadXML($xmlstr);
$this->save("message.xml");
}else{
$this->load('message.xml');
}
}
//增加留言
public function add_message($Subject,$Content){//内容 发布人 发布时间
$Root = $this->documentElement;
//获取留言信息
$AutoID = date("Ynjhis");//年月日时分秒
$Node_AutoID = $this->createElement("autoid");
$text = $this->createTextNode(iconv("GB2312","UTF-8",$AutoID));
$Node_AutoID->appendChild($text);
$Node_Subject = $this->createElement("subject");
$text = $this->createTextNode(iconv("GB2312","UTF-8",$Subject));
$Node_Subject->appendChild($text);
$Node_Content = $this->createElement("content");
$text = $this->createTextNode(iconv("GB2312","UTF-8",$Content));
$Node_Content->appendChild($text);
//建立一条留言记录
$Node_Record = $this->createElement("record");
$Node_Record->appendChild($Node_AutoID);
$Node_Record->appendChild($Node_Subject);
$Node_Record->appendChild($Node_Content);
//加入到根节点下
$Root->appendChild($Node_Record);
$this->save("message.xml");
echo "<script>alert('添加成功!');location.href='".$_SERVER['PHP_SELF']."'</script>";
}
//删除留言
public function delete_message($AutoID){
$Root = $this->documentElement;
//查询用户选择删除的留言记录
$xpath = new DOMXPATH($this);
$Node_Record = $xpath->query("//record[autoid=$AutoID]");
$Root->removeChild($Node_Record->item(0));
$this->save("message.xml");
echo "<script>alert('删除成功!');location.href='".$_SERVER['PHP_SELF']."'</script>";
}
//显示留言
public function show_message(){
$Root = $this->documentElement;
$xpath = new DOMXPATH($this);
//查询所有的留言记录
$Node_Record = $this->getElementsByTagName("record");
$Node_Record_Length = $Node_Record->length;
//循环输出其留言标题和内容信息
echo '<div id="messagelist">';
for($i=0;$i<$Node_Record->length;$i++){
$k = 0;
foreach($Node_Record->item($i)->childNodes as $articles){
$Field[$k] = iconv("UTF-8","GB2312",$articles->textContent);
$k++;
}
echo "<div><b>$Field[1]</b>留言说:<b>$Field[2]</b><a href='?Action=update_message&AutoID=$Field[0]'>编辑</a><a href='?Action=delete_message&AutoID=$Field[0]'>删除</a></div>";
echo '</div>';
}
}
//修改留言
public function update_message($AutoID){
$Root = $this->documentElement;
$xpath = new DOMXPATH($this);
$Node_Record = $xpath->query("//record[autoid=$AutoID]");
$k = 0;
foreach($Node_Record->item(0)->childNodes as $articles){
$Field[$k] = iconv("UTF-8","GB2312",$articles->textContent);
$k++;
}
echo "<form method='post' action='?Action=save_message&AutoID=$AutoID'>";
echo "<div>留言标题:<input type='text' name='Subject' value='".$Field[1]."' size='20'></div>";
echo "<div>留言内容:<textarea name='Content' cols='50' rows='5'>".$Field[2]."</textarea></div>";
echo "<div><input type='submit' value='修改留言'></div></form>";
}
//保存留言
public function save_message($AutoID,$Subject,$Content){
$Root = $this->documentElement;
//查询待修改的记录
$xpath = new DOMXPATH($this);
$Node_Record = $xpath->query("//record[autoid=$AutoID]");
$Replace[0] = $AutoID;
$Replace[1] = $Subject;
$Replace[2] = $Content;
$k = 0;
//修改
foreach($Node_Record->item(0)->childNodes as $articles){
$Node_newText = $this->createTextNode(iconv("GB2312","UTF-8",$Replace[$k]));
$articles->replaceChild($Node_newText,$articles->lastChild);
$k++;
}
echo "<script>alert('修改成功!');location.href='".$_SERVER['PHP_SELF']."'</script>";
$this->save("message.xml");
}
public function post_message(){
echo '<form method="post" action="?Action=add_message">';
echo '<div>留言标题:<input type="text" name="Subject" size="20"></div>';
echo '<div>留言内容:<textarea name="Content" cols="50" rows="5"></textarea></div>';
echo '<div><input type="submit" value="添加留言"></div></form>';
}
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=gb2312"/>
<title>PHP+XML留言板</title>
<style>
td,body{font-size:14px;}
#messagelist div{border:1px dotted #999999;margin:4px;padding:6px;}
#messagelist div b{}
#messagelist div a{float:right;margin-top:-20px;color:#000000;margin-right:4px;}
#messagelist div a:hover{background:#FF0000};
</style>
</head>
<body>
<div align="center"><a href="?Action=post_message">发表留言</a>^^<a href="?Action=show_message">显示留言</a></div>
<?php
//使用Message_XML类完成留言板
$HawkXML = new Message_XML;
$Action = '';
if(isset($_GET['Action']))
$Action = $_GET['Action'];
switch($Action){
case "show_message":
$HawkXML->show_message();
break;
case "post_message":
$HawkXML->post_message();
break;
case "add_message":
$HawkXML->add_message($_POST['Subject'],$_POST['Content']);
break;
case "delete_message":
$HawkXML->delete_message($_GET["AutoID"]);
break;
case "update_message":
$HawkXML->update_message($_GET["AutoID"]);
break;
case "save_message":
$HawkXML->save_message($_GET["AutoID"],$_POST['Subject'],$_POST['Content']);
break;
default:
$HawkXML->show_message();
break;
}
?>
</body>
</html>
PHP+XML留言板
最新推荐文章于 2021-04-11 12:07:17 发布