Simple Example of storing data in php xml message board xml

Source: Internet
Author: User

Class file: _ class. xmldb. inc. php
Copy codeThe Code is as follows:
<? Php
Class xmldb extends DOMDocument {
Var $ xmldb;
Public function _ construct ($ xmldbname ){
$ This-> xmldb = $ xmldbname;
If (! File_exists ($ this-> xmldb )){
$ Initxmldb = "<? Xml version = \ "1.0 \" encoding = \ "UTF-8 \"?> <Message> </Message> ";
$ This-> loadXML ($ initxmldb );
$ This-> save ($ this-> xmldb );
} Else {
$ This-> load ($ this-> xmldb );
}

}
Public function insert_message ($ nickname, $ message ){

$ Messagedom = $ this-> getElementsByTagName ("Message ");
$ Subnode = $ this-> createElement ("Rows ");
$ Nicknamenode = $ this-> createElement ("Nickname ");
$ Nicknamenode-> appendChild ($ this-> createTextNode ($ nickname ));
$ Subnode-> appendChild ($ nicknamenode );
$ Contentnode = $ this-> createElement ("Content ");
$ Contentnode-> appendChild ($ this-> createTextNode ($ message ));
$ Subnode-> appendChild ($ contentnode );
$ Timernode = $ this-> createElement ("Timer ");
$ Timernode-> appendChild ($ this-> createTextNode (time ()));
$ Subnode-> appendChild ($ timernode );
$ Messagedom-> item (0)-> appendChild ($ subnode );
$ This-> saveXML ();
$ This-> save ($ this-> xmldb );
}
Function get_node_length ($ nodename ){

$ Odom = $ this-> getElementsByTagName ($ nodename );
Return $ odom-> length;
}
Public function get_message_list ($ start, $ rowscount, $ pagesize ){
$ End = $ rowscount> $ pagesize? $ Start + $ pagesize: $ rowscount;

$ K = 0;
For ($ I = $ start; $ I <$ end; $ I ++ ){
$ Rowslist [$ k] ["Nickname"] = $ this-> getElementsByTagName ("Nickname")-> item ($ I)-> nodeValue;
$ Rowslist [$ k] ["Content"] = $ this-> getElementsByTagName ("Content")-> item ($ I)-> nodeValue;
$ Rowslist [$ k] ["Timer"] = $ this-> getElementsByTagName ("Timer")-> item ($ I)-> nodeValue;
$ K ++;
}
Return $ rowslist;
}
Public static function splitpage ($ pageall, $ page = 1, $ urled = null, $ strpage = "page", $ pageaverage = 10 ){
$ Pageaverage-= 1;
$ Page = intval ($ page> = 1? $ Page: 1 );
$ Page = $ page> $ pageall? $ Pageall: $ page;
$ Startpage = $ page-$ pageaverage> 0? $ Page-ceil ($ pageaverage/2): 1;
$ Startpage = ($ page + ceil ($ pageaverage/2)> $ pageall )? $ Pageall-$ pageaverage: $ startpage;

$ Startpage = $ startpage> 0? $ Startpage: 1;
$ Stoppage = $ startpage + $ pageaverage> $ pageall? $ Pageall: $ startpage + $ pageaverage;
If (empty ($ urlfile) {$ urlfile = $ _ SERVER ["PHP_SELF"];}
If (! Strrpos ($ urlfile ,'? ') $ Urled. = '? ';
Foreach ($ _ GET as $ k => $ v)
{
$ Urled = ($ k <> $ strpage )? $ Urled. $ k. '='. urlencode ($ v). '&': $ urled;
}
If ($ page> 1 ){
$ Mess = "<a href = "". $ urled. $ strpage. "= 1" href = "". $ urled. $ strpage. "= 1"> homepage </a> ";
$ Mess. = "<a href = "". $ urled. $ strpage. "= ". ($ page-1 ). "" href = "". $ urled. $ strpage. "= ". ($ page-1 ). ""> previous page </a> ";
} Else {
$ Mess = "Homepage ";
$ Mess. = "Previous Page ";
}
If ($ page> 9 ){
$ Startpage = $ page-9;
}
$ Stoppage = $ startpage + 9 >=$ pageall? $ Pageall: $ startpage + 9;
For ($ I = $ startpage; $ I <= $ stoppage; $ I ++)
{
If ($ I <= $ pageall &&! ($ Page = $ I ))
$ Mess. = "<a href = "". $ urled. $ strpage. "= ". $ I. "" href = "". $ urled. $ strpage. "= ". $ I. ""> ". $ I. "</a> ";
Else
$ Mess. = "". $ I ."";
}
If ($ page <$ pageall ){
$ Mess. = "<a href = "". $ urled. $ strpage. "= ". ($ page + 1 ). "" href = "". $ urled. $ strpage. "= ". ($ page + 1 ). ""> next page </a> ";
$ Mess. = "<a href = "". $ urled. $ strpage. "= ". $ pageall. "" href = "". $ urled. $ strpage. "= ". $ pageall. ""> last page </a> ";
} Else {
$ Mess. = "next page ";
$ Mess. = "last page ";
}
Return $ mess;
}
}
?>

Call file: index. php
Copy codeThe Code is as follows:
<? Php
Require ("_ class. xmldb. inc. php ");
$ Xmldb = new xmldb ("./my_xmldb.xml ");
If (isset ($ _ POST ["btnform1"]) {
$ Xmldb-> insert_message ($ _ POST ["nickname"], $ _ POST ["content"]);
}
$ Pagesize = 3;
$ Page =! Empty ($ _ GET ['page'])? Intval ($ _ GET ['page']): 1;
$ Rowscount = $ xmldb-> get_node_length ("Rows ");
$ Pagecount = ceil ($ rowscount/$ pagesize );
$ Start = ($ page-1) * $ pagesize> $ rowscount? $ Rowscount: ($ page-1) * $ pagesize;
$ Rowslist = $ xmldb-> get_message_list ($ start, $ rowscount, $ pagesize );
?>
<! Doctype html public "-// W3C // dtd html 4.0 Transitional // EN">
<HTML>
<HEAD>
<Meta http-equiv = "content-type" content = "text/html; charset = UTF-8">
<TITLE> New Document </TITLE>
<Style type = 'text/css '> <! --
# Messagelist ul li {float: left; list-style-type: none ;}

--> </Style> <style type = 'text/css 'bogus = "1"> # messagelist ul li {float: left; list-style-type: none ;}
</Style>
</HEAD>
<BODY>
<Div id = "messagelist">
<? Php
If (! Empty ($ rowslist ))
{
Foreach ($ rowslist as $ key => $ value)
{
Echo "<ul> <li> ". $ value ["Nickname"]. "<span> (". date ("Y-m-d H: I", $ value ["Timer"]). ") </span> </li> ";
Echo "<li>". $ value ["Content"]. "</li> </ul> <br> ";
}
}
?>
<Div> <? = $ Xmldb-> splitpage ($ pagecount, $ page, '', 'page', $ pagesize)?> </Div>
</Div>
<Div>
<Form action = "" method = "post" name = "form1">
Nickname: <input type = "text" name = "nickname"> <br>
Content: <textarea name = "content">

Simple message board for php to operate xml, with pagination, for reference only

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.