php+xml留言板实例教程二

最后我们把xml文档格式贴出来吧.

好了,其它的都快完了我们就来看看index.php文件显示留方的页面吧.

?phpclass Message_XML extends DOMDocument{const file_name =
e:/myphp/xmldom/xml/message.xml;private $root; //根节点private $PageNo;
//当前页private $allNum; //记录总数private $PageSize; //页大小private
$allPages; //总页数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();}//得到页信息private function get_pagemsg(){$this –
PageSize = 3; //页大小$allNode = $this –
getElementsByTagName(record);$this – allNum = $allNode – length;
//记录总数$this – allPages = ceil($this – allNum / $this – PageSize);
//总页数$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;}//显示留言public function
show_message(){$start_num = ($this – PageNo – 1) * $this – PageSize;
//记录开始数$end_num = $start_num + $this – PageSize – 1;
//记录结束数$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留言标题:/span$subject/pp
class=’msgC’span留言内容:/spanbr /br
/nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;$content/p;$str .= p
class=’msgCMD’a
href=’?Action=edit_messageAutoID=$autoidPageNo=$_GET[PageNo]’编辑/a
a
href=’?Action=delete_messageAutoID=$autoidPageNo=$_GET[PageNo]’删除/a/p/div;print
$str;}$i++;}$this – get_pageCode();}//得到当前页码public function
get_pageCode(){$str = div class=’pageCode’当前页:.$this – PageNo. /
.$this – allPages.nbsp;nbsp;nbsp;a href=’?PageNo=1’首页/anbsp;a
href=’?PageNo=.($this-PageNo – 1).’/anbsp;a href=’?PageNo=.($this-PageNo

今天没事,公司也要写一个php
解析xml的程序,在网上看看了,觉得这个不错就写出来和各位分享吧,我们先看看看留言页面吧.

<body>
<table width=”681″ border=”0″ cellspacing=”0″ cellpadding=”0″
align=”center”>
  <form action=”<?php echo $_SERVER[‘PHP_SELF’]; ?>”
method=”post” name=”message” id=”message” onsubmit=”return
FrontPage_Form1_Validator(this)”>
    <tr>
      <td height=”25″ colspan=”2″
align=”center”><strong>发表留言(带*为必填)</strong></td>
    </tr>
    <tr>
      <td width=”109″ height=”25″
align=”right”>呢称: </td>
      <td width=”572″ height=”25″><label>
        <input name=”username” type=”text” class=”input”
id=”username” value=”<?php
   if($_SESSION[user])
       echo “{$_SESSION[user]}”;
 ?>” />
        <font color=”#000000″>*</font>(<font
color=”#ff6600″>*</font>为必填项)</label></td>
    </tr>
   
   
    <tr>
      <td height=”25″ align=”right”>QQ: </td>
      <td height=”25″><label>
        <input name=”qq” type=”text” class=”input” id=”qq” />
      (纯数字)</label></td>
    </tr>
    <tr>
      <td height=”25″ align=”right”>E-mail: </td>
      <td height=”25″><label>
        <input name=”email” type=”text” class=”input” id=”email”
size=”30″ />
      (必须含有@)</label></td>
    </tr>
    <tr>
      <td height=”25″
align=”right”>个人主页(blog): </td>
      <td height=”25″><label>
        <input name=”weburl” type=”text” class=”input” id=”weburl”
size=”30″ />
      (以;
    </tr>
    <tr>
      <td height=”25″ align=”right”>内容: </td>
      <td height=”25″><label>
        <textarea name=”content” cols=”70″ rows=”15″ class=”input”
id=”content”></textarea>
        <font
color=”#000000″>*</font></label></td>
    </tr>
    <tr>
      <td height=”25″> </td>
      <td height=”25″><label>
        <input name=”submit” type=”submit” class=”botton” id=”submit”
value=”发表留言” />
         
        <input name=”reset” type=”reset” class=”botton”
value=”重新填写” id=”reset” />
        <input name=”add” type=”hidden” id=”add” value=”add” />
      </label></td>
    </tr>
  </form>
</table>
</body>
</html>
<?php
 }
?>

这是一个xml 解析类

?phpinclude_once(class.php);$HawkXML = new Message_XML();$Action =
$_GET[Action];?htmlheadmeta content=en /meta name=GENERATOR
content=Zend Studio /meta content=text/html; charset=utf-8 /style
type=text/cssbody{margin: 0;padding: 0;text-align: center;color:
#666;}div{margin: 0 auto;width: 800px;}p,h1,h2,h3,h4,h5,h6{margin: 0;
padding:0}a{text-decoration: none; color:
#0033FF}a:hover{text-decoration: underline}span{font-weight:
bold;}.top{margin-top: 15px;height: 35px;background:
#00FFFF;line-height: 35px;}.msg{height: 25px;text-align: left;margin:
8px;}.msgT,.msgC{text-align:
left;word-break:break-all;}.msgCMD{margin-top: 10px;text-align:
right;}.msgInfo{margin: 3px 0;border: 1px solid #ccc;}.pageCode{margin:
3px 0;height: 35px;line-height: 35px;background:
#eee;}/styletitlephp+xml留言本/title/headbodydiv
class=topphp+xml留言本/divdiv class=msga
href=?Action=post_messagePageNo=?= $_GET[‘PageNo’] ?[发表留言]/a a
href=?Action=show_messagePageNo=?= $_GET[‘PageNo’] ?[显示留言]/a
[a href=?Action=clear_message清空留言/a]/div!–显示留言–div
clas=showmsg?phpswitch($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
clear_message:$HawkXML – clear_message();break;case delete_message:
//删除留言$AutoID = $_GET[‘AutoID’];$HawkXML –
delete_message($AutoID);break;case edit_message: //修改留言$AutoID =
$_GET[‘AutoID’];$HawkXML – edit_message($AutoID);break;case
save_message: //修改留言提交$AutoID = $_GET[‘AutoID’];$HawkXML –
save_message($AutoID,$_POST[‘Subject’],$_POST[‘Content’]);break;default:
//默认显示留言$HawkXML – show_message();break;}?/div/body/html

OK了简单的留言系统就OK了.

  • 1).’/anbsp;a href=’?PageNo=.($this-allPages).’末页/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;}//添加留言页面public function post_message(){print divform
    method=’post’ action=’?Action=add_messagePageNo=$_GET[PageNo]’;print
    p标题:input type=’text’ name=’Subject’ size=’50’ //p;print
    p内容:textarea name=’Content’ cols=’50’ rows=’5’/textarea/p;print
    pinput type=’submit’ value=’添加留言’/p/div/form;}//添加留言public
    function add_message($Subject,$Content){$autoid = microtime();
    //留言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
    scriptalert(‘添加留言成功!’);window.location=’.$_SERVER[‘PHP_SELF’].?PageNo=.$_GET[‘PageNo’].’/script;}//清空留言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
    scriptalert(‘清空成功!’);window.location=’.$_SERVER[‘PHP_SELF’].’/script;}else{echo
    scriptalert(‘清空失败!’);history.back();/script;}}//设置节点路径和操作对象IDprivate
    function set_nodePath($AutoID){$xpath = new DOMXPath($this);$node_top
    = $xpath – query(//record[autoid=$AutoID]);return
    $node_top;}//删除留言public function
    delete_message($AutoID){$node_top = $this –
    set_nodePath($AutoID);$this – root – removeChild($node_top –
    item(0));$this – save(self::file_name);echo
    scriptalert(‘删除成功!’);location=’.$_SERVER[‘PHP_SELF’].?PageNo=.$_GET[‘PageNo’].’/script;}//编辑留言页面public
    function edit_message($AutoID){$node_top = $this –
    set_nodePath($AutoID);//取值方法1//$subject = $node_top – item(0) –
    getElementsByTagName(subject) – item(0) – nodeValue;//$content =
    $node_top – item(0) – getElementsByTagName(‘content’) – item(0)
    -nodeValue;//取值方法2foreach($node_top – item(0) – childNodes as
    $v){$value[] = $v – textContent;
    //注意:这里的$value必须这样写成一个数组,否则要出错}print divform
    method=’post’
    action=’?Action=save_messageAutoID=$AutoIDPageNo=$_GET[PageNo]’;print
    p标题:input type=’text’ name=’Subject’ value=$value[1] size=’50’
    //p;print p内容:textarea name=’Content’ cols=’50’
    rows=’5’$value[2]/textarea/p;print pinput type=’submit’
    value=’编辑留言’/p/div/form;}//编辑留言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
    scriptalert(‘编辑成功!’);location=’.$_SERVER[‘PHP_SELF’].?PageNo=.$_GET[‘PageNo’].’/script;}}?

?xml version=1.0
encoding=utf-8?rootrecordautoid265634001218784833/autoidsubject测试留言1/subjectcontent测试留言1测试留言1/content/recordrecordautoid140627001218784843/autoidsubject测试留言2/subjectcontent测试留言2测试留言2测试留言2测试留言2测试留言2测试留言2测试留言2测试留言2测试留言2测试留言2测试留言2/content/recordrecordautoid078128001218784850/autoidsubject测试留言3/subjectcontent测试留言3测试留言3测试留言3/content/recordrecordautoid765628001218784857/autoidsubject测试留言4/subjectcontent测试留言4测试留言4测试留言4测试留言4测试留言4测试留言4测试留言4测试留言4测试留言4/content/record/root

  <div class=”pages”>共 <strong><?php echo(“$pagenum”);
?></strong> 页 当前第 <strong><?php echo(“$page”);
?></strong> 页 <a
href=”index.php?page=1″>第一页</a> <a
href=”index.php?page=<?php%20echo(“$prepage”);
?>”>上一页</a> <a href=”index.php?page=<?php
echo(“$nextpage”); ?>”>下一页</a> <a
href=”index.php?page=<?php%20echo(“$pagenum”);
?>”>最后一页</a> </div><!– pages –>
 
  <div class=”foot”>
  菜根 <br />
  Copyright © 菜根  xanxin <a
href=”;  </div>
  <!– foot –>
</div>
<!– all –>
</body>
</html>
显示了当然会有留言页面喽,如下.

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“;
<html xmlns=”;
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″
/>
<title>求职留言本</title>
<style type=”text/css”>
<!–
body {
 background: #C7EDDD;
 text-align: center;
 margin: 0px;
 padding: 0px;
 font-size: 12px;
}
a {
 text-decoration: none;
 margin: 0px;
 padding: 0px;
}
.messagebox img {
 margin: 0px;
 padding: 0px;
}
.all {
 margin: 0px auto;
 padding: 0px 0px 10px;
 width: 780px;
 background: #C7EDCC;
 text-align: left;
}
.header {
 margin: 0px;
 padding: 0px;
 height: 90px;
 background: #DDDDDD;
 border-top: 5px solid #660066;
 border-right: 1px solid #660066;
 border-bottom: 1px solid #660066;
 border-left: 1px solid #660066;
}
.header h1 {
 font-size: 36px;
 font-weight: bold;
 margin: 0px;
 padding: 5px 10px 0px;
}
.header #menu {
 margin: 0px;
 padding: 25px 10px 0px;
 height: 20px;
 font-size: 14px;
 font-weight: bold;
 text-align: right;
}
.messagebox {
 margin: 20px 10px 0px;
 padding: 0px 0px 5px;
 width: 760px;
 border: 1px solid #33FFFF;
 background: #C7EDCC;
}
.messagebox a {
 text-decoration: none;
 margin: 0px;
 padding: 0px;
}
.messagebox-top {
 border-bottom: 1px dashed #00FFFF;
 margin: 0px;
 padding: 3px 0px 0px;
 height: 20px;
 background: #CCFFFF;
}
.messagebox-topl {
 margin: 0px 0px 0px 10px;
 padding: 0px;
 float: left;
}
.messagebox-topr {
 margin: 0px 5px 0px 0px;
 padding: 0px;
 float: right;
}
.messagebox-topc {
 margin: 1px;
 padding: 0px 20px;
}
.messagebox-show {
 margin: 5px 10px 0px;
 padding: 3px 3px 5px;
 overflow: hidden;
 border: 1px solid #00FFCC;
}
.messagebox-reply {
 border-top: 1px dashed #00FFCC;
 border-right-width: 1px;
 border-bottom-width: 1px;
 border-left-width: 1px;
 border-right-style: dashed;
 border-bottom-style: dashed;
 border-left-style: dashed;
 margin: 2px 10px 0px;
 padding: 2px 2px 5px;
 background: #E0EDDC;
}
.messagebox-replyt {
 font-weight: bold;
 color: #0000FF;
 margin: 0px 0px 5px;
 padding: 0px;
}
.pages {
 background: #00FFFF;
 margin: 5px 20px 0px;
 padding: 0px 0px 5px;
}
.foot {
 background: #E0EDDC;
 margin: 5px 0px 0px;
 padding: 5px;
 text-align: center;
}
–>
</style>

发表评论

电子邮件地址不会被公开。 必填项已用*标注