波音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>
@LLOVE