又写了个留言板求指点

@Ta 2013-03-29 5681点击
波音747

代码贴上
<?php
date_default_timezone_set("PRC");
class Note {
    public $userName;
    public $userNote;
    private $writeTime;
    
    
    function __construct($name, $note) {
        $this->userName = $name;
        $this->userNote = $note;
        $this->writeTime = time();
        }
    function writeDownNote() {
        $db = "Note.db";
        $sql = "INSERT INTO note(username, usernote, writetime) VALUES('{$this->userName}', '{$this->userNote}', '{$this->writeTime}')";
        
        $con = sqlite_open($db);
        
        $writeResault = sqlite_query($con, $sql);
        return $writeResault?true:false;
    }
    
    
    static function createTB() {
        $tableCreate = "CREATE TABLE note(username CHAR(10), usernote VARCHAR(200), writetime INT(10))";
        $db = "Note.db";
        $con = sqlite_open($db);
        $createResault = sqlite_query($con, $tableCreate);
            return $createResault?true:false;
    
    
    
    }
    function checkNote(&$_note, $_name) {
        $_note = htmlspecialchars($_note);
        return (preg_match('/.+/',$_note))?"":($_name."不能为空");
    
    }
    
    static function readNote($_page) {
        $pos = ($_page-1)*10;
        $db = "Note.db";
        $sql = "SELECT * FROM note ORDER BY writetime DESC LIMIT {$pos},10";
        $con = sqlite_open($db);
        $readResault = sqlite_query($con, $sql);
        $i = 0;
        while($resault = sqlite_fetch_array($readResault)) {
            $info[$i] .= "昵称:".$resault["username"]."
";
            $info[$i] .= "留言:".$resault["usernote"]."
";
            $info[$i] .= "时间:".date('Y-m-d H:i:s',$resault["writetime"]);
            $i++;
        }
        return $info?$info:"亲,留言信息为空哦!";
    
    
    }
        static function checkRow() {
            $db = "Note.db";
            $sql = "SELECT * FROM note";
            $con = sqlite_open($db);
            $checkResault = sqlite_query($con, $sql);
            return sqlite_num_rows($checkResault);
        
        }
    
    




}


//Note::createTB();


?>
 
            <?php
                $noteMax = Note::checkRow();
                $pageMax = ceil($noteMax/10);
                $page = ($_GET['page']>0&&$_GET['page']<=$pageMax)?$_GET['page']:1;
                ?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>简易留言板</title>
        <script type="text/javascript">
            window.onload = function() {
                var pageMax = <?php echo $pageMax; ?>;
                
                var xmlHttpRequest;
                var page = <?php echo $page; ?>;
                var readMoreBtn = document.getElementById("readMore");
                
                function createXmlHttpRequest() {
                    if(window.ActiveXObject) {
                        xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
                        
                    }
                    else if(window.XMLHttpRequest) {
                        xmlHttpRequest = new XMLHttpRequest();
                    
                    }
                        
                }
                function startXmlHttpRequest() {
                    createXmlHttpRequest();
                    url = "http://www.kgda.tk/note/index.php?page=" + page + "&ajax=1" + "&t=" + new Date().getTime();
                    xmlHttpRequest.onreadystatechange = handleStateChange;
                    xmlHttpRequest.open("GET",url,true);
                    xmlHttpRequest.send(null);
                }
                function handleStateChange() {
                    if(xmlHttpRequest.readyState == 4) {
                   
                        if(xmlHttpRequest.status == 200) {
                        
                            var newNote = xmlHttpRequest.responseText;
                            
                               var createP = document.createElement("p");
                           var createT,hr,br;
                          
                           var arr = newNote.split("
");
                           
                      
                       for(i=6;i<(arr.length-1);i++)
                           {
                       
                       
                            createT = document.createTextNode(arr);
                              createP.appendChild(createT);
                            if((i-5)%3) {
                            br = document.createElement("br");
                            createP.appendChild(br);
                            
                            }
                            else{
                             hr = document.createElement("hr");
                            
                            createP.appendChild(hr);
                            }
                          
                            }   
                        document.body.insertBefore(createP,readMoreBtn);
                         readMoreBtn.value = "查看更多";
                    readMoreBtn.disabled = false;
                        }
                    
                    }
                    else {
                    readMoreBtn.value = "正在加载";
                    
                    }
                
                }
                readMoreBtn.onclick = function()  {
                this.disabled = true;
                    if(++page <= pageMax) {
                        startXmlHttpRequest();
                        
                    
                    }
                    if(page > pageMax) {
                            this.value = "没有留言了";
                            
                        }
                
                }
                
            
            }
            </script>
    </head>
    <body>
           <p id="write">
    
        <?php
            if($_POST['username']||$_POST['usernote']) {
                $note = new Note($_POST['username'],$_POST['usernote']);
                $namestatus = $note->checkNote($note->userName, "昵称");
                $notestatus = $note->checkNote($note->userNote, "留言");
                if(!($namestatus||$notestatus)) {
                    
                    echo ($note->writeDownNote())?"亲,留言成功啦!":"亲,留言失败了哦,重试一下吧!";
                    }
                else {
                
                    echo "亲,输入的数据错误啦,仔细检查一下吧";
                
                }
            
            
            }
        ?>
        </p>
        
        <form method="post" action="index.php">
            请输入您的昵称:

            <input type="text" name="username" />
            <span id="namestatus">
            <?php
                echo $namestatus;
            ?>
            </span>

            请输入您的留言:

            <input type="text" name="usernote" />
            <span id="notestatus">
               <?php
                echo $notestatus;
            ?>
            
            </span>

            <input type="submit" value="提交留言">
        </form>
        <p id="noteinfo">
         <span style="text-align:center">-----留言板----</span>

            <?php
            $end = $_GET["ajax"]?"
":"<hr/>";
            $i = 0;
             $arr = Note::readNote($page);
        
              while($arr[$i]) {
             echo $arr[$i].$end;
             $i++;
             
             }
           
            
            ?>
        </p>
     <?php 
            
         //   if($page>1) {
            //    echo " <a href='index.php?page=".($page-1)."'>上一页</a> ";
          //      }
         //   if($page<$pageMax) {
            
            
             //   echo " <a href='index.php?page=".($page+1)."'>下一页</a> ";
             //   }
            //echo " 共{$pageMax}页 ";
        
        ?>
        
       <input type="button" value="查看更多" id="readMore" />
       
     

       
    </body>
</html>
回复列表(14|隐藏机器人聊天)
  • @Ta / 2013-03-29 / /
    看不懂的代码。。。灰机747。。。。。
  • @Ta / 2013-03-29 / /
    <br/>
  • @Ta / 2013-03-29 / /
    转义单引号
  • @Ta / 2013-03-29 / /
    我擦,一个留言板还要用类写啊,有才的人,亮代码是来炫耀的吧?欺负人家看不懂?定义了那么多函数,变量,耗时拖死你……(数据多的时候)
    @LLOVE
  • @Ta / 2013-03-29 / /
    刚会自定义函数,接下来就是类了!
  • @Ta / 2013-03-29 / /
    @LLOVE 不要把别人代码帖出来炫耀
  • @Ta / 2013-03-29 / /
    4L
  • @Ta / 2013-03-30 / /
  • @Ta / 2013-03-30 / /
    @Xily 呵呵,这你写的?
  • @Ta / 2013-03-30 / /
    @love封尘 师傅,不要拆台嘛,我觉得要学习面向对象这种思想,不管写什么。而且我也是刚学,练练手么
  • @Ta / 2013-03-30 / /
    @LLOVE 抱歉,有一个地方看错了,代码不错,还有JS
  • @Ta / 2013-03-30 / /
    @LLOVE 是你啊,呵呵,不错不错,都会用类了……不过不能这么写,太耗时,而且代码复杂,扩展性也不是太强,不要这样用,类,我一般都不用,又不是开发大型门户,再说,类的效率也低,下次用模板写,可能比较好……
  • @Ta / 2013-03-30 / /
    <?phpdate_default_timezone_set("PRC");class Note {    public $userName;    public $userNote;    private $writeTime;    function __construct($name, $note) {        $this->userName = $name;        $this->userNote = $note;        $this->writeTime = time();        }    function writeDownNote() {        //写入数据        $db = "Note.db";        $sql = "INSERT INTO note(username, usernote, writetime) VALUES('{$this->userName}', '{$this->userNote}', '{$this->writeTime}')";        $con = sqlite_open($db);        $writeResault = sqlite_query($con, $sql);        return $writeResault?true:false;    }    static function createTB() {    //建表        $tableCreate = "CREATE TABLE note(username CHAR(10), usernote VARCHAR(200), writetime INT(10))";        $db = "Note.db";        $con = sqlite_open($db);        $createResault = sqlite_query($con, $tableCreate);            return $createResault?true:false;    }    function checkNote(&$_note, $_name) {//验证用户        $_note = htmlspecialchars($_note);return (preg_match('/.+/',$_note))?"":($_name."不能为空");    }    static function readNote($_page) {//显示数据        $pos = ($_page-1)*10;        $db = "Note.db";        $sql = "SELECT * FROM note ORDER BY writetime DESC LIMIT {$pos},10";        //一页取多少        $con = sqlite_open($db);        $readResault = sqlite_query($con, $sql);        $i = 0;        //循环取出数据        while($resault = sqlite_fetch_array($readResault)) {            $info[$i] .= "昵称:".$resault["username"]."";            $info[$i] .= "留言:".$resault["usernote"]."";            $info[$i] .= "时间:".date('Y-m-d H:i:s',$resault["writetime"]);//格式化时间            $i++;        }        //没有数据返回没数据,否则返回一个一维数组        return $info?$info:"亲,留言信息为空哦!";    }        static function checkRow() {        //查页数            $db = "Note.db";            $sql = "SELECT * FROM note";            $con = sqlite_open($db);            $checkResault = sqlite_query($con, $sql);            return sqlite_num_rows($checkResault);        }}//Note::createTB();?>            <?php                $noteMax = Note::checkRow();//查一共多少数据                $pageMax = ceil($noteMax/10);//共有多少页                $page = ($_GET['page']>0&&$_GET['page']<=$pageMax)?$_GET['page']:1;//如果输入的页数大于0或者小于等于总页数则通过,否则等于1                ?><!DOCTYPE html><html>    <head>        <meta charset="utf-8">        <title>简易留言板</title>        <script type="text/javascript">            window.onload = function() {                var pageMax = <?php echo $pageMax; ?>;                var xmlHttpRequest;                var page = <?php echo $page; ?>;                var readMoreBtn = document.getElementById("readMore");                function createXmlHttpRequest() {                    if(window.ActiveXObject) {                        xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");                    }                    else if(window.XMLHttpRequest) {                        xmlHttpRequest = new XMLHttpRequest();                    }                }                function startXmlHttpRequest() {                    createXmlHttpRequest();                    url = "http://www.kgda.tk/note/index.php?page=" + page + "&ajax=1" + "&t=" + new Date().getTime();                    xmlHttpRequest.onreadystatechange = handleStateChange;                    xmlHttpRequest.open("GET",url,true);                    xmlHttpRequest.send(null);                }                function handleStateChange() {                    if(xmlHttpRequest.readyState == 4) {                        if(xmlHttpRequest.status == 200) {                            var newNote = xmlHttpRequest.responseText;                               var createP = document.createElement("p");                           var createT,hr,br;                           var arr = newNote.split("");                       for(i=6;i<(arr.length-1);i++)                           {                            createT = document.createTextNode(arr);                              createP.appendChild(createT);                            if((i-5)%3) {                            br = document.createElement("br");                            createP.appendChild(br);                            }                            else{                             hr = document.createElement("hr");                            createP.appendChild(hr);                            }                            }                        document.body.insertBefore(createP,readMoreBtn);                         readMoreBtn.value = "查看更多";                    readMoreBtn.disabled = false;                        }                    }                    else {                    readMoreBtn.value = "正在加载";                    }                }                readMoreBtn.onclick = function()  {                this.disabled = true;                    if(++page <= pageMax) {                        startXmlHttpRequest();                    }                    if(page > pageMax) {                            this.value = "没有留言了";                        }                }            }            </script>    </head>    <body>           <p id="write">        <?php            if($_POST['username']||$_POST['usernote']) {//用户两项不为空则执行                $note = new Note($_POST['username'],$_POST['usernote']);//初始化负值$this->usernote和$this->username                 $namestatus = $note->checkNote($note->userName, "昵称");//空则返回空,否则返回字符串                $notestatus = $note->checkNote($note->userNote, "留言");//空则返回空,否则返回字符串                if(!($namestatus||$notestatus)) {//如果两个输入不为空则写入数据,并判断是否写入成功                    echo ($note->writeDownNote())?"亲,留言成功啦!":"亲,留言失败了哦,重试一下吧!";                    }                else {  //如果输入为空                                   echo "亲,输入的数据错误啦,仔细检查一下吧";                }            }        ?>        </p>        <form method="post" action="index.php">            请输入您的昵称:            <input type="text" name="username" />            <span id="namestatus">            <?php                echo $namestatus;            ?>            </span>            请输入您的留言:            <input type="text" name="usernote" />            <span id="notestatus">               <?php                echo $notestatus;            ?>            </span>            <input type="submit" value="提交留言">        </form>        <p id="noteinfo">         <span style="text-align:center">-----留言板----</span>            <?php            $end = $_GET["ajax"]?"":"<hr/>";            $i = 0;             $arr = Note::readNote($page);              while($arr[$i]) {             echo $arr[$i].$end;             $i++;             }            ?>        </p>     <?php         //   if($page>1) {            //    echo " <a href='index.php?page=".($page-1)."'>上一页</a> ";          //      }         //   if($page<$pageMax) {             //   echo " <a href='index.php?page=".($page+1)."'>下一页</a> ";             //   }            //echo " 共{$pageMax}页 ";        ?>       <input type="button" value="查看更多" id="readMore" />    </body></html>
  • @Ta / 2013-03-30 / /
    @love封尘 嗯,我主要是刚学面向对象,就试试,嘿嘿
添加新回复
回复需要登录