php xml留言板 xml存储数据的简单例子
程序员文章站
2023-11-28 18:48:28
类文件 : _class.xmldb.inc.php 复制代码 代码如下:
类文件 : _class.xmldb.inc.php
<?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">首页</a> ";
$mess .="<a href="".$urled.$strpage."=".($page-1)."" href="".$urled.$strpage."=".($page-1)."">上一页 </a>";
}else{
$mess ="首页 ";
$mess .="上一页 ";
}
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)."">下一页</a>";
$mess .=" <a href="".$urled.$strpage."=".$pageall."" href="".$urled.$strpage."=".$pageall."">尾页</a>";
}else{
$mess .=" 下一页";
$mess .=" 尾页";
}
return $mess;
}
}
?>
调用文件 : index.php
<?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">
昵称:<input type="text" name="nickname"><br>
内容:<textarea name="content">
php操作xml的简单留言板,带分页,仅供参考
复制代码 代码如下:
<?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">首页</a> ";
$mess .="<a href="".$urled.$strpage."=".($page-1)."" href="".$urled.$strpage."=".($page-1)."">上一页 </a>";
}else{
$mess ="首页 ";
$mess .="上一页 ";
}
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)."">下一页</a>";
$mess .=" <a href="".$urled.$strpage."=".$pageall."" href="".$urled.$strpage."=".$pageall."">尾页</a>";
}else{
$mess .=" 下一页";
$mess .=" 尾页";
}
return $mess;
}
}
?>
调用文件 : index.php
复制代码 代码如下:
<?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">
昵称:<input type="text" name="nickname"><br>
内容:<textarea name="content">
php操作xml的简单留言板,带分页,仅供参考