php+xml留言板实例教程二

 更新时间:2016年11月25日 16:04  点击:1766

这是一个xml 解析类

<?php
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 = "<?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</p><p class='msgC'><span>留言内容:</span><br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$content</p>";
     $str .= "<p class='msgCMD'><a href='?Action=edit_message&AutoID=$autoid&PageNo=$_GET[PageNo]'>编辑</a> <a href='?Action=delete_message&AutoID=$autoid&PageNo=$_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'>首页</a>&nbsp;<a href='?PageNo=".($this->PageNo - 1)."'>上一页</a>&nbsp;<a href='?PageNo=".($this->PageNo + 1)."'>下一页</a>&nbsp;<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 "<div><form method='post' action='?Action=add_message&PageNo=$_GET[PageNo]'>";
   print "<p>标题:<input type='text' name='Subject' size='50' /></p>";
   print "<p>内容:<textarea name='Content' cols='50' rows='5'></textarea></p>";
   print "<p><input 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 "<script>alert('添加留言成功!');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 "<script>alert('清空成功!');window.location='".$_SERVER['PHP_SELF']."'</script>";
   }else{
    echo "<script>alert('清空失败!');history.back();</script>";
   }
 }
 //设置节点路径和操作对象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 "<script>alert('删除成功!');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;
   //取值方法2
   foreach($node_top -> item(0) -> childNodes as $v)
   {
    $value[] = $v -> textContent; //注意:这里的$value必须这样写成一个数组,否则要出错
   }
   print "<div><form method='post' action='?Action=save_message&AutoID=$AutoID&PageNo=$_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 "<p><input 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 "<script>alert('编辑成功!');location='".$_SERVER['PHP_SELF']."?PageNo=".$_GET['PageNo']."'</script>";
 }
}
?>

在asp,.net中日期相加减是相当简单的一个datediff函数就可以算出两个日期的相差多长的时间了,而当我学php时以为也是这样,但完全与我想的不同,下面是我看到一个朋友写的php 日期相加减

<?PHP 
$Date_1="2008-8-15";//格式也可以是:$Date_1="2003-6-25 23:29:14";
 
$Date_2="2009-10-1"; 

$Date_List_a1=explode("-",$Date_1);
 
$Date_List_a2=explode("-",$Date_2); 

$d1=mktime(0,0,0,$Date_List_a1[1],$Date_List_a1[2],$Date_List_a1[0]); 

$d2=mktime(0,0,0,$Date_List_a2[1],$Date_List_a2[2],$Date_List_a2[0]); 

$Days=round(($d1-$d2)/3600/24); 

Echo "两日期之前相差有$Days 天"; 
?>
哈哈,只要我们会使用时间N就也不难了.

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

<?php
include_once("class.php");
$HawkXML = new Message_XML();
$Action = $_GET["Action"];
?>
<html>
<head>
<meta http-equiv="Content-Language" content="en" />
<meta name="GENERATOR" content="Zend Studio" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
body{
 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;
}
</style>
<title>php+xml留言本</title>
</head>
<body>
<div class="top">php+xml留言本</div>
<div class="msg"><a href="?Action=post_message&PageNo=<?= $_GET['PageNo'] ?>">[发表留言]</a> <a href="?Action=show_message&PageNo=<?= $_GET['PageNo'] ?>">[显示留言]</a> [<a href="?Action=clear_message">清空留言</a>]</div>
<!--显示留言-->
<div clas="showmsg">
<?php
switch($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>

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

<?xml version="1.0" encoding="utf-8"?>
<root><record><autoid>265634001218784833</autoid><subject>测试留言1</subject><content>测试留言1测试留言1</content></record><record><autoid>140627001218784843</autoid><subject>测试留言2</subject><content>测试留言2测试留言2测试留言2测试留言2测试留言2测试留言2测试留言2测试留言2测试留言2测试留言2测试留言2</content></record><record><autoid>078128001218784850</autoid><subject>测试留言3</subject><content>测试留言3测试留言3测试留言3</content></record><record><autoid>765628001218784857</autoid><subject>测试留言4</subject><content>测试留言4测试留言4测试留言4测试留言4测试留言4测试留言4测试留言4测试留言4测试留言4</content></record></root>

ajax 注册验证,今天这个文章有一点怪了,就是验证url 是否能被正常访问哦,

<tr>
    <td height="34" align="center">博客地址:</td>
    <td height="25" align="center"><span class="STYLE1">*</span></td>
    <td><label>
    <input name="blog" type="text" id="blog" size="47" onblur="ajax(0);"  />
    </label></td>
    <td  id="u_info"></td>
  </tr>

上面是简单的html代码程序.下面贴出php 验证代码.

if( $ta==0){
 if(@file_get_contents($xm)){
  echo '<div id="success">&nbsp;&nbsp;&nbsp;&nbsp;博客地址可以访问</div>';
 }else{
  echo '<div id="error">对不起,你输入的博客地址不能正常访问<div >';
 }
}else{
 echo "<div id='error'>对不起,无效的操作!</div>";
}

好了我还是反ajax 代码也写出来吧,

var xmlHttp = false;

function ajaxcreate(){

try {
  xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
 } catch (e) {
try {
  xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
 } catch (e2) {
  xmlHttp = false;
 }
 }

if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
  xmlHttp = new XMLHttpRequest();
 }
if(!xmlHttp){alert('Create Xmlhttp Fail ');return false;}
}

ajaxcreate();
  xmvalue=document.getElementById("blog").value;
 var url="ajax_check.php?txt="+encodeURI(xmvalue)+"&tag="+encodeURI(str)+"&rd="+Math.random();
 if (xmvalue== null || xmvalue.length>200 || xmvalue == "") return false;
 xmlHttp.open("POST",url,true);
 xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
 xmlHttp.send(xmvalue); 
 xmlHttp.onreadystatechange=returnstate; 
}
function returnstate(){
 if(xmlHttp.readyState != 4 ){
  document.getElementById("u_info").innerHTML="<div class=defult>正在验证请稍后...</div>";
 }
 if(xmlHttp.readyState == 4 ){
  document.getElementById("u_info").innerHTML=xmlHttp.responseText;
  
 }
}

OK下面这段我以前写过了,不过为了方便还是也写出来了,最后申明一下:本站原创文章转载请注明来:  www.111cn.net/phper/php.html


<?php
class Calendar{
   /**
    * @desc       :简单的日历类,供大家学习
    * @author     :Allen Wu
    * @Email      :wukewei00o@126.com
    * @Date       :2008-09-12
    * @version    :v1.0
    */
    /*定义变量年、月、日*/
    private $year,$month,$day;
    /*定义数组星期并初始化*/
    private $week    = array("星期日","星期一","星期二","星期三","星期四","星期五","星期六");
    /*定义数组月份并初始化*/
    private $monthes = array("01"=>"一月",
                             "02"=>"二月",
                             "03"=>"三月",
                             "04"=>"四月",
                             "05"=>"五月",
                             "06"=>"六月",
                             "07"=>"七月",
                             "08"=>"八月",
                             "09"=>"九月",
                             "10"=>"十月",
                             "11"=>"十一月",
                             "12"=>"十二月"
                             );

    function __construct(){
        $year  = isset($_POST['year']) ? $_POST['year'] : date('Y');
        $month = isset($_POST['month']) ? $_POST['month'] : date('m');
        $day   = isset($_POST['day']) ? $_POST['day'] : date('d');
        $this->set($year, $month, $day);
    }
    /**
     * @desc   设置年、月、日的值
     * @params String $year
     * @params String $month
     * @params String $day
     * @return
     */
    private function set($year, $month, $day){
        $this->year  = $year;
        $this->month = $month;
        $this->day   = $day;
    }

    /**
     * @desc   获取年、月、日的值并以数组形式返回
     * @params Array $info
     * @retrun Array
     */
    function get(array $info){
        $info = array('year' => $this->year,
                      'month'=> $this->month,
                      'day'  => $this->day);
        return $info;
    }
    /**
     * @desc   获得指定日期的星期值
     * @params String $year
     * @params String $month
     * @params String $day
     * @return String
     */
    private function getWeek($year, $month, $day){
        $weekday = date("w",mktime(0,0,0,$month,$day,$year));
        return $weekday;
    }
 

    /**
     * 输出日历,有兴趣的可以改进!
     * 其实这不是一个方法,不希望在类里出现html和样式
     * 有兴趣的可以改进下!给大家起个抛砖引玉的作用
     *
     */
    public function out(){
        $firstDay = $this->getWeek($this->year, $this->month, 1);
        echo "<div style="margin:0;border:1 solid black;width:300;font:9pt">".
             "<form action=$_SERVER[PHP_SELF] method="post" style="margin:0">".
             "<select name="month" onchange="this.form.submit();">";

        /*打印12个月*/
        for($month = 1; $month <= 12; $month++){
            $tmp = sprintf("%02d", $month);
            if(strcmp($tmp, $this->month) == 0){
                $select = "selected style="background-color:#c0c0c0"";
            }else{
                $select = "";
            }
            echo "<option value="$tmp" $select>".$this->monthes[$tmp]."</option>rn";
        }
        echo "</select><select name="year" onchange="this.form.submit();">";

        /*打印年份,前后10年*/
        for($year = $this->year - 10; $year < $this->year + 10; $year++){
            if($year > 2037){break;}
            if($year < 1970){continue;}
            if(strcmp($year, $this->year) == 0){
                $select = "selected style="background-color:#c0c0c0"";
            }else{
                $select = "";
            }
            echo "<option value="$year" $select>$year</option>rn";
        }
        echo "</select></form><table border=0 align=center>";

        /*打印星期标头*/
        for($week = 0; $week < count($this->week); $week++){
            echo "<td>".$this->week[$week];
        }

        /*打印所有日期*/
        for($tmpd = 1; $tmpd <= date("t",mktime(0,0,0,$this->month,$this->day,$this->year)); $tmpd++){
            if(strcmp($tmpd, $this->day) == 0){   //获得当前日期,做标记
                $flag="bgcolor='#ff0000'";
            }else{
                $flag=" bgcolor='#ffffff'";
            }
            if($tmpd == 1){
                echo "<tr>";      //补充打印
                for($i = 0; $i < $firstDay; $i++){
                    echo "<td>";
                }
            }
            if(strcmp($this->getWeek($this->year, $this->month, $tmpd), 0) == 0){
                echo "<tr><td align=center $flag>$tmpd";
            }else{
                echo "<td align=center $flag>$tmpd";
            }
        }
        echo "</table></div>";
    }
}
$obj = new Calendar();
$obj->out();
?>
[!--infotagslink--]

相关文章

  • Painter绘制红衣喝酒男水粉画效果教程

    今天小编在这里就来给Painter的这一款软件的使用者们来说一说绘制红衣喝酒男水粉画效果的教程,各位想知道具体绘制步骤的使用者,那么下面就快来跟着小编一起看一看教程...2016-09-14
  • iPhone6怎么激活?两种苹果iPhone6激活教程图文详解

    iPhone6新机需要激活后才可以正常使用,那么对于小白用户来说,iPhone6如何激活使用呢?针对此问题,本文就为大家分别介绍Wifi无线网络激活以及iPhone6连接电脑激活这两种有效的方法,希望本文能够帮助到大家...2022-09-14
  • Photoshop制作雨中野外孤独行走的一头牛海报教程

    今天小编在这里就来给各位photoshop的这一款软件的使用者们来说下制作雨中野外孤独行走的一头牛海报的教程,各位想知道具体制作方法的使用者们,大家就快来看一看小编给...2016-09-14
  • Painter绘制帅气卡通魔法王子漫画教程

    今天小编在这里就来给Painter的这一款软件的使用者们来说一下绘制帅气卡通魔法王子漫画的具体教程,各位想知道绘制步骤的使用者,那么下面就快来跟着小编一起看一看教程...2016-09-14
  • Illustrator鼠绘堆雪人的孩童矢量插画教程

    今天小编在这里就来给各位Illustrator的这一款软件的使用者们来说说鼠绘堆雪人的孩童矢量插画的教程,各位想知道具体绘制方法的使用者们,那么各位就快来跟着小编来看看...2016-09-14
  • 美图秀秀给照片天空加蓝天白云教程一览

    今天小编在这里就来给美图秀秀的这一款软件的使用者们来说下究竟该怎么给照片天空加蓝天白云的教程,各位想知道具体制作步骤的,那么下面就来跟着小编一起看看吧。 ...2016-09-14
  • 安卓手机app添加支付宝支付开发教程

    支付宝支付在国内算是大家了,我们到处都可以使用支付宝了,下文整理介绍的是在安卓app应用中使用支付宝进行支付的开发例子。 之前讲了一篇博客关与支付宝集成获取...2016-09-20
  • llustrator绘制扁平化风格卡通警察护士空姐肖像教程

    今天小编在这里就来给llustrator的这一款软件的使用者们来说一说绘制扁平化风格卡通警察护士空姐肖像的教程,各位想知道具体绘制步骤的使用者们,那么下面就快来跟着小编...2016-09-14
  • Illustrator绘制一个方形的录音机图标教程

    今天小编在这里就来给Illustrator的这一款软件的使用者们来说一下绘制一个方形的录音机图标的教程,各位想知道具体绘制方法的使用者们,那么下面就来看一下小编给大家分...2016-09-14
  • photoshop简单制作一个搞笑的换脸表情包教程

    今天小编在这里就来给photoshop的这一款软件的使用者们来说一说简单制作一个搞笑的换脸表情包的教程,各位想知道具体制作方法的使用者们,那么大家就快来看一看教程吧。...2016-09-14
  • photoshop给手绘画调色变换场景后期教程

    今天小编在这里就来给各位photoshop的这一款软件的使用者们来说说给手绘画调色变换场景的后期教程,各位想知道具体后期处理步骤的使用者们,那么大家就快来跟着小编来看...2016-10-02
  • 美图秀秀让你胸丰满起来处理教程

    今天小编在这里就来给美图秀秀的这一款软件的使用者们来说一下让你胸丰满起来的处理教程,各位想知道具体处理步骤的,那么下面就快来跟着小编一起看一下教程吧。 给...2016-09-14
  • Painter绘制雷神传插画教程

    今天小编在这里就来给Painter的这一款软件的使用者们来说一下绘制雷神传插画的教程,各位想知道具体绘制步骤的使用者,那么下面就快来跟着小编一起看看绘制方法吧。 ...2016-09-14
  • MySQL中的联合索引学习教程

    联合索引又叫复合索引。对于复合索引:Mysql从左到右的使用索引中的字段,一个查询可以只使用索引中的一部份,但只能是最左侧部分。例如索引是key index (a,b,c). 可以支持a | a,b| a,b,c 3种组合进行查找,但不支持 b,c进...2015-11-24
  • 美图秀秀制作隔离区聊天背景教程

    今天小编在这里就来给美图秀秀的这一款软件的使用者们来说下制作隔离区聊天背景的教程,各位想知道具体方法的,那么下面就快来跟着小编一起看一看吧。 给各位美图秀...2016-09-14
  • MySQL日志分析软件mysqlsla的安装和使用教程

    一、下载 mysqlsla [root@localhost tmp]# wget http://hackmysql.com/scripts/mysqlsla-2.03.tar.gz--19:45:45-- http://hackmysql.com/scripts/mysqlsla-2.03.tar.gzResolving hackmysql.com... 64.13.232.157Conn...2015-11-24
  • Lua语言新手简单入门教程

    这篇文章主要给大家介绍的是关于Lua语言新手入门的简单教程,文中通过示例代码一步步介绍的非常详细,对各位新手们的入门提供了一个很方便的教程,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧。...2020-06-30
  • photoshop素材合成古典园林场景制作教程

    今天小编在这里就来给photoshop的这一款软件的使用者们来说说用素材合成古典园林场景教程,各位想知道到底该怎么制作的,那么就快来一起看一下吧。 给各位photoshop...2016-09-14
  • php类的使用实例教程

    php类的使用实例教程 <?php /** * Class program for yinghua05-2 * designer :songsong */ class Template { var $tpl_vars; var $tpl_path; var $_deb...2016-11-25
  • node.js+express留言板功能实现示例

    本文介绍基于nodejs+express+art-template的留言板功能。包含列表界面、添加界面和发送留言功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-21