Rumah >pembangunan bahagian belakang >tutorial php >php xml留言板 xml存储数据的简单例子_PHP教程

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

WBOY
WBOYasal
2016-07-21 15:44:29786semak imbas

类文件 : _class.xmldb.inc.php

复制代码 代码如下:

class xmldb extends DOMDocument{
    var $xmldb;
    public function __construct($xmldbname){
        $this->xmldb=$xmldbname;
        if(!file_exists($this->xmldb)){
            $initxmldb = "";
            $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             $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 ="首页 ";
                $mess .="上一页 ";
            }else{
                $mess ="首页 ";
                $mess .="上一页 ";
            }
            if($page > 9){
                $startpage = $page - 9;
            }
            $stoppage = $startpage + 9 >= $pageall ? $pageall : $startpage + 9;
            for($i= $startpage; $i            {
                if($i                    $mess .= "".$i." ";
                else
                    $mess .= "".$i." ";
            }
            if ($page                 $mess .=" 下一页";
                $mess .=" 尾页";
            }else{
                $mess .=" 下一页";
                $mess .=" 尾页";
            }
        return $mess;
    }
}
?>

调用文件 : index.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);
?>




New Document
    


    

            if(!empty($rowslist))
        {
            foreach($rowslist as $key => $value)
            {
                echo "
  • ".$value["Nickname"]." (".date("Y-m-d H:i",$value["Timer"]).")
  • ";
                    echo "
  • ".$value["Content"]."

";
            }
        }
    ?>
        
=$xmldb -> splitpage($pagecount,$page,'','page',$pagesize)?>

    

    

        

            昵称:

            内容:

php操作xml的简单留言板,带分页,仅供参考

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/320569.htmlTechArticle类文件 : _class.xmldb.inc.php 复制代码 代码如下: ?php class xmldb extends DOMDocument{ var $xmldb; public function __construct($xmldbname){ $this-xmldb=$xmldbname; if(!fi...
Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn