Simple XML Message Board
Main program guestbook.php
<? Php
//==========================================
/ / File Name: guestbook.php
/ / Note: simple XML book
/ / 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");
)
//==========================================
/ / Function: addMessage
/ / Function: Add a Message
/ / Parameter: $ userName name Message
/ / Parameter: $ message Content
/ / Parameter: $ postTime time Message
/ / Returns: a successful failure 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 (the "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;
)
//==========================================
/ / Function: delMessage ($ id)
/ / Function: According to delete a message ID information
/ / Parameter: $ id Message #
/ / Returns: a successful failure 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 ( "Ymd H: i: s");
If ($ guestbook-> addMessage ($ _POST [ 'userName'],$_ POST [' content '], $ now))
(
Echo "<script> alert ( 'Add success'); location.href ='".$_ SERVER [' PHP_SELF']."'</ script>";
)
)
If (isset ($ _GET [ 'method']) & & $ _GET [ 'method'] == "del")
(
$ Guestbook = new guestbook;
If ($ guestbook-> delMessage ($ _GET [ 'id']))
(
Echo "<script> alert ( 'delete success'); 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;
/ / * /
)
–>
——- Style sheet 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 = & id = del <xsl:value-of select="id" /> </ xsl: attribute>
Delete
</ Xsl: element>
</ Td>
</ Tr>
<tr Bgcolor='#FFFFFF'>
<td> <xsl:value-of Select="content" /> </ td>
</ Tr>
</ Xsl: for-each>
</ Table>
</ Xsl: template>
</ Xsl: stylesheet>
Tags: XML








0 Comments to “Simple XML Message Board”
No Comments. Send your comment.
Leave a Reply
You must be logged in to post a comment.