仿網易163新聞評論蓋樓效果代碼
//發送留言
require("global.func.php教程");
$fid=isset($_GET["id"])?intval($_GET["id"]):0;
$page=isset($_GET["page"])?intval($_GET["page"]):1;
if($_SERVER["REQUEST_METHOD"]=="POST")
{
if(!file_exists("1.php"))
{
writeFile("1.php",$array=array());
}
$arr=require("1.php");
$post=array();
unset($_POST["Submit"]);
foreach($_POST as $k=>$v)
{
$post[$k]=addslashes(htmlspecialchars($v));
}
$post["content"]=str_replace("n","<br>",$post["content"]);
$post["id"]=count($arr)+1;
$post["date"]=time();
$post["fid"]=$fid;
$post["ip"]=$_SERVER["REMOTE_ADDR"];
$arr[]=$post;
writeFile("1.php",$arr);
echo "<script>location.href="index.php?page=$page"</script>";
exit;
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>發帖</title>
<script>
var arr=new Array();
function ckusername()
{
if(document.form1.username.value.length<3)
{
document.getElementById("user_msg").innerHTML="<font color="red"><b>用戶名不能小於3個字符</b></font>";
arr[0]=1;
}else
{
document.getElementById("user_msg").innerHTML="<font color="green"><b>√</b></font>";
arr[0]=0;
}
}
function ckcontent()
{
if(document.form1.content.value.length<8||document.form1.content.value.length>255)
{
document.getElementById("content_msg").innerHTML="<font color="red"><b>內容8-255個字符</b></font>";
arr[1]=1;
}else
{
document.getElementById("content_msg").innerHTML="<font color="green"><b>√</b></font>";
arr[1]=0;
}
}
function c()
{
if(arr.length<2)
{
return false;
}
for(x in arr)
{
if(arr[x]==1)
{
return false;
}
}
form1.submit();
}
</script>
</head>
<body>
<form name="form1" method="post" action="">
<table width="100%" border="0">
<tr>
<td>用戶
<input name="username" type="text" id="id" onBlur="ckusername()" ></td>
</tr>
<tr>
<td><div id="user_msg"></div>
</td>
</tr>
<tr>
<td><textarea name="content" cols="100" rows="6" id="content" onBlur="ckcontent()"></textarea></td>
</tr>
<tr>
<td><div id="content_msg"></div>
</td>
</tr>
<tr>
<td><div align="center">
<input type="button" name="Submit" value="提交" onClick="c();">
</div></td>
</tr>
</table>
</form>
</body>
</html>
<?php
ini_set("display_errors",0);
require("global.func.php");
?>
顯示像163評論
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.bkjia.com/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style>
.b{
border:1px dotted #e3e3e3;
word-wrap:break-word;
word-break:break-all;
}
</style>
<title>PPC備用論壇(遞歸限制15層+主題)</title>
</head>
<body>
<?php
echo "<a href="post.php?page=1"> 發帖</a><br>";
if(file_exists("1.php"))
{
$__array=require("1.php");
}
else
{
$__array=array();
}
$page=isset($_GET["page"])?intval($_GET["page"]):1;
$pagesize=5;
$offset=($page-1)*$pagesize;
$__str="";
function getTotal()
{
global $__array;
$i=0;
foreach($__array as $k=>$v)
{
if($v["fid"]==0)
{
$i=$i+1;
}
}
return $i;
}
function getFid0()
{
global $__array,$__str;
foreach ($__array as $k=>$v)
{
if($v["fid"]==0)
{
$fid0[]=$v;
}
}
return array_reverse($fid0);
}
$fid0=getFid0();
function show($fid0,$pagesize,$offset)
{
global $__str,$page;
foreach (array_slice($fid0,$offset,$pagesize,true) as $k=>$v)
{
$__str.="<blockquote class="b"><p>";
$__str.=$v["ip"]."用戶:".$v["username"]."內容:".$v["content"]."<a id="a1" href="post.php?id=".$v["id"]."&page=$page">回復</a>";
showChild($v["id"]);
$__str.="</p></blockquote>";
}
}
function showChild($id,$i=0)
{
global $__array,$__str,$page,$offset;
if($i==15)//遞歸層數
{
return;
}
$i++;
foreach($__array as $k=>$v)
{
if($v["fid"]==$id)
{
$__str.= "<blockquote class="b"><p id="a1">";
$__str.= $__array[$k]["ip"]."用戶:".$__array[$k]["username"]."內容:".$__array[$k]["content"]."<a href="post.php?id=".$v["id"]."&page=$page">回復</a>";
showChild($v["id"],$i);
$__str.= "</p></blockquote>";
}
}
}
show($fid0,$pagesize,$offset);
$total=getTotal();
echo $__str;
pageft($total,$pagesize,"index.php");
echo "<br>".$pagenav;
?>
</body>
</html>
global.func.php文件代碼如下
<?php
function writeFile($id,$array)
{
$content="<?php return ".var_export($array,true)."?>";
return safe_file_put_contents($id,$content);
}
/**
*寫入文件
*@param string $filename
*@param string $contents
*@return boolean
*/
function safe_file_put_contents($filename,&$contents)
{
$fp=fopen($filename,"wb");
if($fp)
{
flock($fp,LOCK_EX);
fwrite($fp,$contents);
flock($fp,LOCK_UN);
fclose($fp);
return true;
}else
{
return false;
}
}
/**
*讀取文件
*@param string $filename
*@return $data|false
*/
function safe_file_get_contents($filename)
{
$fp=fopen($filename,"rb");
if($fp)
{
flock($fp,LOCK_SH);
clearstatcache();
$filesize=filesize($filename);
if($filesize>0)
{
$data=fread($fp,$filesize);
}else
{
$data=false;
}
flock($fp,LOCK_UN);
fclose($fp);
return $data;
}else
{
return false;
}
}
function pageft($total,$displaypg=10,$url=''){
global $page,$firstcount,$pagenav,$_SERVER;
$GLOBALS["displaypg"]=$displaypg;
if(!$page) $page=1;
if(!$url){ $url=$_SERVER["REQUEST_URI"];}
$parse_url=parse_url($url);
@$url_query=$parse_url["query"];
if($url_query){
$url_query=ereg_replace("(^|&)page=$page","",$url_query);
$url=str_replace($parse_url["query"],$url_query,$url);
if($url_query) $url.="&page"; else $url.="page";
}else {
$url.="?page";
}
$lastpg=ceil($total/$displaypg);
$page=min($lastpg,$page);
$prepg=$page-1;
$nextpg=($page==$lastpg ? 0 : $page+1);
if(!$page) $page=1;
$firstcount=($page-1)*$displaypg;
$pagenav.=" <a href='$url=1' class='s1'>首頁</a> ";
if($prepg) $pagenav.=" <a href='$url=$prepg' class='s1'>上頁</a> "; else $pagenav.="上頁 ";
if($nextpg) $pagenav.=" <a href='$url=$nextpg' class='s1'>下頁</a> "; else $pagenav.="下頁 ";
$pagenav.=" <a href='$url=$lastpg' class='s1'>尾頁</a>";
$pagenav.=" 到 <select name='topage' size='1' onchange='window.location="$url="+this.value'>n";
for($i=1;$i<=$lastpg;$i++){
if($i==$page) $pagenav.="<option value='$i' selected>$i</option>n";
else $pagenav.="<option value='$i'>$i</option>n";
}
$pagenav.="</select> 頁";
}
?>
//保存的1.php文件數數組形式,如下格式
<?php return array (
0 =>
array (
'username' => 'test',
'content' => 'php100.com',
'id' => 1,
'date' => 1253853347,
'fid' => 0,
'ip' => '116.226.2.200',
),
1 =>
array (
'username' => 'aaaaaaaaaaad',
'content' => 'www.bkjia.com',
'id' => 2,
'date' => 1253857672,
'fid' => 0,
'ip' => '116.226.6.188',
),
)?>