网络编程 
首页 > 网络编程 > 浏览文章

php xml留言板 xml存储数据的简单例子

(编辑:jimmy 日期: 2024/11/19 浏览:3 次 )
类文件 : _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>&nbsp;";
                $mess .="<a href="".$urled.$strpage."=".($page-1)."" href="".$urled.$strpage."=".($page-1)."">上一页&nbsp;</a>";
            }else{
                $mess ="首页&nbsp;";
                $mess .="上一页&nbsp;";
            }
            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 .="&nbsp;<a href="".$urled.$strpage."=".($page+1)."" href="".$urled.$strpage."=".($page+1)."">下一页</a>";
                $mess .="&nbsp;<a href="".$urled.$strpage."=".$pageall."" href="".$urled.$strpage."=".$pageall."">尾页</a>";
            }else{
                $mess .="&nbsp;下一页";
                $mess .="&nbsp;尾页";
            }
        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 开源框架22个简单简介
下一篇:建站常用13种PHP开源CMS比较
一句话新闻
一文看懂荣耀MagicBook Pro 16
荣耀猎人回归!七大亮点看懂不只是轻薄本,更是游戏本的MagicBook Pro 16.
人们对于笔记本电脑有一个固有印象:要么轻薄但性能一般,要么性能强劲但笨重臃肿。然而,今年荣耀新推出的MagicBook Pro 16刷新了人们的认知——发布会上,荣耀宣布猎人游戏本正式回归,称其继承了荣耀 HUNTER 基因,并自信地为其打出“轻薄本,更是游戏本”的口号。
众所周知,寻求轻薄本的用户普遍更看重便携性、外观造型、静谧性和打字办公等用机体验,而寻求游戏本的用户则普遍更看重硬件配置、性能释放等硬核指标。把两个看似难以相干的产品融合到一起,我们不禁对它产生了强烈的好奇:作为代表荣耀猎人游戏本的跨界新物种,它究竟做了哪些平衡以兼顾不同人群的各类需求呢?