Php + xml message board example tutorial 2-PHP source code

Source: Internet
Author: User
Ec (2); this is an xml parsing Class & lt ;? PhpclassMessage_XMLextendsDOMDocument {& nbsp; constfile_name & quot; e: myphpxmldomxmlmessage. xml & quot; & nbsp; private $ root; root node & nbsp; private $ PageNo; current page & nb script ec (2); script

This is an xml parsing class

Class Message_XML extends DOMDocument
{
Const file_name = "e:/myphp/xmldom/xml/message. xml ";
Private $ root; // root Node
Private $ PageNo; // current page
Private $ allNum; // The total number of records.
Private $ PageSize; // page size
Private $ allPages; // the total number of pages.
Public function _ construct ()
{
Parent: :__ construct ();
If (! File_exists (self: file_name ))
{
$ XmlStr =" ";
$ This-> loadXML ($ xmlStr );
$ This-> save (self: file_name );
} Else {
$ This-> load (self: file_name );
}
$ This-> root = $ this-> documentElement;
$ This-> get_pagemsg ();
}
// Obtain the page information
Private function get_pagemsg ()
{
$ This-> PageSize = 3; // page size
$ AllNode = $ this-> getElementsByTagName ("record ");
$ This-> allNum = $ allNode-> length; // The total number of records.
$ This-> allPages = ceil ($ this-> allNum/$ this-> PageSize); // total number of pages
$ This-> PageNo =$ _ GET ["PageNo"];
If ($ this-> PageNo <1 |! Is_numeric ($ this-> PageNo ))
{
$ This-> PageNo = 1;
} Else if ($ this-> PageNo> $ this-> allPages ){
$ This-> PageNo = $ this-> allPages;
}
$ This-> PageNo = (int) $ this-> PageNo;
}
// Display the message
Public function show_message ()
{
$ Start_num = ($ this-> PageNo-1) * $ this-> PageSize; // record start count
$ End_num = $ start_num + $ this-> PageSize-1; // record end count
$ AllNode = $ this-> getElementsByTagName ("record ");
$ I = 0;
Foreach ($ allNode as $ v)
{
If ($ I >=$ start_num & $ I <= $ end_num)
{
$ Autoid = $ v-> getElementsByTagName ("autoid")-> item (0)-> nodeValue;
$ Subject = $ v-> getElementsByTagName ("subject")-> item (0)-> nodeValue;
$ Content = $ v-> getElementsByTagName ("content")-> item (0)-> nodeValue;
$ Str ="

Message Title: $ subject

Message content:

$ Content

";
$ Str. ="

Edit and delete

";
Print $ str;
}
$ I ++;
}
$ This-> get_pageCode ();
}
// Obtain the current page number
Public function get_pageCode ()
{
$ Str ="

Current page :". $ this-> PageNo. "/". $ this-> allPages. "homepage PageNo-1 ). "'> Previous Page PageNo + 1 ). "'> next page allPages ). "'> last page ";
$ Str. ="";
Print $ str;
}
// Add a message page
Public function post_message ()
{
Print"

";
}
// Add a message
Public function add_message ($ Subject, $ Content)
{
$ Autoid = microtime (); // Message ID
$ Autoid = substr (strrchr (str_replace ("", "", $ autoid), "."), 1 );
$ Node_top = $ this-> root-> appendChild ($ this-> createElement ("record "));
$ Node_top-> appendChild ($ this-> createElement ("autoid", $ autoid ));
$ Node_top-> appendChild ($ this-> createElement ("subject", $ Subject ));
$ Node_top-> appendChild ($ this-> createElement ("content", $ Content ));
$ This-> save (self: file_name );
Echo "script" alert ('message added successfully! '); Window. location =' ". $ _ SERVER ['php _ SELF ']."? PageNo = ". $ _ GET ['pageno']." 'script ";
}
// Clear the message
Public function clear_message ()
{
$ Fp = @ fopen (self: file_name, "w + ");
If ($ fp)
{
$ Str =" ";
Fwrite ($ fp, $ str );
Fclose ($ fp );
Echo "script" alert ('cleared successfully! '); Window. location =' ". $ _ SERVER ['php _ SELF ']."' script ";
} Else {
Echo "script alert ('failed to empty! '); History. back (); script ";
}
}
// Set the Node path and operation object ID
Private function set_nodePath ($ AutoID)
{
$ Xpath = new DOMXPath ($ this );
$ Node_top = $ xpath-> query ("// record [autoid = $ AutoID]");
Return $ node_top;
}
// Delete the message
Public function delete_message ($ AutoID)
{
$ Node_top = $ this-> set_nodePath ($ AutoID );
$ This-> root-> removeChild ($ node_top-> item (0 ));
$ This-> save (self: file_name );
Echo "script" alert ('deleted successfully! '); Location =' ". $ _ SERVER ['php _ SELF ']."? PageNo = ". $ _ GET ['pageno']." 'script ";
}
// Edit the message page
Public function edit_message ($ AutoID)
{
$ Node_top = $ this-> set_nodePath ($ AutoID );
// Method 1
// $ Subject = $ node_top-> item (0)-> getElementsByTagName ("subject")-> item (0)-> nodeValue;
// $ Content = $ node_top-> item (0)-> getElementsByTagName ('content')-> item (0)-> nodeValue;
// Method 2
Foreach ($ node_top-> item (0)-> childNodes as $ v)
{
$ Value [] = $ v-> textContent; // Note: $ value must be written as an array like this; otherwise, an error occurs.
}
Print"

";
}
// Edit the message
Public function save_message ($ AutoID, $ Subject, $ Content)
{
$ Node_top = $ this-> set_nodePath ($ AutoID );
$ Replace_info [0] = $ AutoID;
$ Replace_info [1] = $ Subject;
$ Replace_info [2] = $ Content;
$ I = 0;
Foreach ($ node_top-> item (0)-> childNodes as $ v)
{
$ New_content = $ this-> createTextNode ($ replace_info [$ I]);
$ V-> replaceChild ($ new_content, $ v-> lastChild );
$ I ++;
}
$ This-> save (self: file_name );
Echo "script" alert ('edited successfully! '); Location =' ". $ _ SERVER ['php _ SELF ']."? PageNo = ". $ _ GET ['pageno']." 'script ";
}
}
?>

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.