热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

php+xml留言板实例教程二-PHP源码

ec(2);这是一个xml解析类<?phpclassMessage_XMLextendsDOMDocument{ constfile_name"e:myphpxmldomxmlmessage.xml"; private$root;根节点 private$PageNo;当前页&nb

这是一个xml 解析类

class 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 = "";
$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 = "

留言标题:$subject

留言内容:

        $content

";
$str .= "

编辑 删除

";
print $str;
}
$i++;
}
$this -> get_pageCode();
}
//得到当前页码
public function get_pageCode()
{
$str = "

当前页:".$this -> PageNo." / ".$this -> allPages."   首页 上一页 下一页 末页";
$str .= "   ";
print $str;
}
//添加留言页面
public function post_message()
{
print "

";
print "

标题:

";
print "

内容:

";
print "

";
}
//添加留言
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 "";
}
//清空留言
public function clear_message()
{
$fp = @ fopen(self::file_name,"w+");
if($fp)
{
$str = "";
fwrite($fp,$str);
fclose($fp);
echo "";
}else{
echo "";
}
}
//设置节点路径和操作对象ID
private 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 "";
}
//编辑留言页面
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;
//取值方法2
foreach($node_top -> item(0) -> childNodes as $v)
{
$value[] = $v -> textContent; //注意:这里的$value必须这样写成一个数组,否则要出错
}
print "

";
print "

标题:

";
print "

内容:

";
print "

";
}
//编辑留言
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 "";
}
}
?>
推荐阅读
  • 优化ListView性能
    本文深入探讨了如何通过多种技术手段优化ListView的性能,包括视图复用、ViewHolder模式、分批加载数据、图片优化及内存管理等。这些方法能够显著提升应用的响应速度和用户体验。 ... [详细]
  • 本文详细介绍了如何在Linux系统上安装和配置Smokeping,以实现对网络链路质量的实时监控。通过详细的步骤和必要的依赖包安装,确保用户能够顺利完成部署并优化其网络性能监控。 ... [详细]
  • 本文详细介绍了如何使用PHP检测AJAX请求,通过分析预定义服务器变量来判断请求是否来自XMLHttpRequest。此方法简单实用,适用于各种Web开发场景。 ... [详细]
  • This guide provides a comprehensive step-by-step approach to successfully installing the MongoDB PHP driver on XAMPP for macOS, ensuring a smooth and efficient setup process. ... [详细]
  • 本文详细介绍了 Dockerfile 的编写方法及其在网络配置中的应用,涵盖基础指令、镜像构建与发布流程,并深入探讨了 Docker 的默认网络、容器互联及自定义网络的实现。 ... [详细]
  • Python自动化处理:从Word文档提取内容并生成带水印的PDF
    本文介绍如何利用Python实现从特定网站下载Word文档,去除水印并添加自定义水印,最终将文档转换为PDF格式。该方法适用于批量处理和自动化需求。 ... [详细]
  • 在当前众多持久层框架中,MyBatis(前身为iBatis)凭借其轻量级、易用性和对SQL的直接支持,成为许多开发者的首选。本文将详细探讨MyBatis的核心概念、设计理念及其优势。 ... [详细]
  • golang常用库:配置文件解析库/管理工具viper使用
    golang常用库:配置文件解析库管理工具-viper使用-一、viper简介viper配置管理解析库,是由大神SteveFrancia开发,他在google领导着golang的 ... [详细]
  • [论文笔记] Crowdsourcing Translation: Professional Quality from Non-Professionals (ACL, 2011)
    Time:4hoursTimespan:Apr15–May3,2012OmarZaidan,ChrisCallison-Burch:CrowdsourcingTra ... [详细]
  • 深入解析JVM垃圾收集器
    本文基于《深入理解Java虚拟机:JVM高级特性与最佳实践》第二版,详细探讨了JVM中不同类型的垃圾收集器及其工作原理。通过介绍各种垃圾收集器的特性和应用场景,帮助读者更好地理解和优化JVM内存管理。 ... [详细]
  • 非公版RTX 3080显卡的革新与亮点
    本文深入探讨了图形显卡的进化历程,重点介绍了非公版RTX 3080显卡的技术特点和创新设计。 ... [详细]
  • Docker的安全基准
    nsitionalENhttp:www.w3.orgTRxhtml1DTDxhtml1-transitional.dtd ... [详细]
  • 程序员妻子吐槽:丈夫北漂8年终薪3万,存款情况令人意外
    一位程序员的妻子在网上分享了她丈夫在北京工作八年的经历,月薪仅3万元,存款情况却出乎意料。本文探讨了高学历人才在大城市的职场现状及生活压力。 ... [详细]
  • 本文详细介绍 Go+ 编程语言中的上下文处理机制,涵盖其基本概念、关键方法及应用场景。Go+ 是一门结合了 Go 的高效工程开发特性和 Python 数据科学功能的编程语言。 ... [详细]
  • Valve 发布 Steam Deck 的新版 Windows 驱动程序
    Valve 最新发布了针对 Steam Deck 掌机的 Windows 驱动程序,旨在提升其在 Windows 环境下的兼容性、安全性和性能表现。 ... [详细]
author-avatar
mobiledu2502853463
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有