Php+xml Message Board Example tutorial Two

Source: Internet
Author: User
Tags xpath

This is an XML parsing class

<?php
Class Message_xml extends DOMDocument
{
Const FILE_NAME = "E:/myphp/xmldom/xml/message.xml";
Private $root; Root node
Private $PageNo; Current page
Private $allNum; Total Records
Private $PageSize; Page size
Private $allPages; Total pages
Public Function __construct ()
{
Parent::__construct ();
if (!file_exists (self::file_name))
{
$XMLSTR = "<?xml version= ' 1.0 ' encoding= ' utf-8 '" ><root/> ";
$this-> Loadxml ($XMLSTR);
$this-> Save (Self::file_name);
}else{
$this-> Load (self::file_name);
}
$this-> root = $this-> documentelement;
$this-> get_pagemsg ();
}
Get page Info
Private Function Get_pagemsg ()
{
$this-> PageSize = 3; Page size
$allNode = $this-> getelementsbytagname ("Record");
$this-> allnum = $allNode-> length; Total Records
$this-> allpages = ceil ($this-> allnum/-> $this PageSize); Total 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;
}
Show message
Public Function Show_message ()
{
$start _num = ($this-> PageNo-1) * $this-> PageSize; Number of records started
$end _num = $start _num + $this-> PageSize-1; Number of records ended
$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 = "<div class= ' msginfo ' ><p class= ' MSgt ' ><span> message title:</span> $subject </p><p class= ' MSGC ' ><span> message content: </span><br/><br/>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; $content </p> ";
$str. = "<p class= ' msgcmd ' ><a href= '"? action=edit_message&autoid= $autoid &pageno=$_get[pageno] ' > Edit </a> <a href= '? action=delete_message&autoid= $autoid &pageno=$_get[pageno] ' > Delete </a></p></div> ';
Print $str;
}
$i + +;
}
$this-> Get_pagecode ();
}
Get current page number
Public Function Get_pagecode ()
{
$str = "<div class= ' pagecode ' > Current page:". $this-> PageNo. "/" $this-> allpages. " &nbsp;&nbsp;&nbsp;<a href= '? Pageno=1 ' > Home </a>&nbsp;<a href= '? Pageno= ". ($this->pageno-1). "' > previous page </a>&nbsp;<a href= '? Pageno= ". ($this->pageno + 1). "' > next page </a>&nbsp;<a href= '? Pageno= ". ($this->allpages). "' > Last </a> ";
$str. = "&nbsp;&nbsp;&nbsp;<input type= ' text ' size= ' 2 ' id= ' gopage ' value= '". $this->pageno. "' ><input type= ' button ' value= ' Go ' onclick=window.location= '? Pageno= ' +document.getelementbyid (' gopage ') .value> ";
Print $str;
}
Add a Message page
Public Function Post_message ()
{
Print "<div><form method= ' post ' action= '? Action=add_message&pageno=$_get[pageno] ' > ';
Print "<p> title: <input type= ' text ' name= ' Subject ' size= '/></p> ';
Print "<p> contents: <textarea name= ' content ' cols= ' rows= ' 5 ' ></textarea></p>";
Print "<p><input type= ' submit ' value= ' Add message ' ></p></div></form>";
}
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 (' Add message success! '); window.location= '. $_server[' php_self '. "? pageno= ". $_get[' PageNo ']." ' </script> ";
}
Clear message
Public Function Clear_message ()
{
$fp = @ fopen (self::file_name, "w+");
if ($FP)
{
$str = "<?xml version= ' 1.0 ' encoding= ' utf-8 '" ><root/> ";
Fwrite ($fp, $STR);
Fclose ($FP);
echo "<script>alert" (' Empty success! '); window.location= '. $_server[' php_self '. "' </script> ";
}else{
echo "<script>alert" (' Empty failed! '); History.back ();</script> ";
}
}
Set node path and action object ID
Private Function Set_nodepath ($AutoID)
{
$xpath = new Domxpath ($this);
$node _top = $xpath-> query ("//record[autoid= $AutoID]");
return $node _top;
}
Delete 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" (' Delete succeeded! '); location= '. $_server[' php_self '. "? pageno= ". $_get[' PageNo ']." ' </script> ";
}
Edit Message Page
Public Function Edit_message ($AutoID)
{
$node _top = $this-> set_nodepath ($AutoID);
Value Method 1
$subject = $node _top-> Item (0)-> getElementsByTagName ("subject")-> item (0)-> nodevalue;
$content = $node _top-> Item (0)-> getElementsByTagName (' content ')-> item (0)->nodevalue;
Value Method 2
foreach ($node _top-> Item (0)-> childnodes as $v)
{
$value [] = $v-> textcontent; Note: The $value here must be written in an array, otherwise there will be an error
}
Print "<div><form method= ' post ' action= '? action=save_message&autoid= $AutoID &pageno=$_get[pageno] ' > ';
Print "<p> title: <input type= ' text ' name= ' Subject ' value= $value [1] size= '/></p> ';
Print "<p> contents: <textarea name= ' content ' cols= ' rows= ' 5 ' > $value [2]</textarea></p>";
Print "<p><input type= ' submit ' value= ' editorial message ' ></p></div></form>";
}
Edit 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" (' Edit succeeded! '); 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.