Recursive | pagination | tree-type structure
I will present the results of my practice show to you, the disadvantage is that the method of pagination is not very good, can not show the specific number of pages, but there is no other better solution, had to do so, if there is similar to this forum of the paging method, the table enlighten one or two, two springs are appreciated! Specific access to my personal small Web site: HTTP://WEB.NYIST.NET/~WBGWRQ, no nonsense, start ...
The structure of the table is as follows:
Creat.sql
Simple description: Rootid topic ordinal; Layer: Post level, indentation basis; Orders: Order of posts
CREATE TABLE Over_post (
ID Int (one) not NULL auto_increment,
Title varchar NOT NULL default ',
Content text,
Postat datetime NOT NULL default ' 0000-00-00 00:00:00 ',
readed Int (one) not null default ' 0 ',
Size int (one) not null default ' 0 ',
Rootid Int (one) not null default ' 0 ',
Orders int (4) NOT null default ' 0 ',
Layer Int (4) NOT null default ' 0 ',
Primary KEY (ID)
) Type=myisam;
Creat.sql End
Post a root post, that is, Rootid,layer,orders 0
said.php
Begin
<form method= "POST" action= "post.php" >
<table width= "81%" border= "0" cellspacing= "1" cellpadding= "0" align= "center" >
<tr bgcolor= "#66cccc" >
<TD height= "width=" "23%" >
<div align= "Right" > Your thoughts:
</div>
</td>
<TD height= "width=" "77%" >
<input type= "text" name= "title" size= "class=" EQ >
</td>
</tr>
<tr bgcolor= "#66cccc" >
<TD height= "width=" "23%" >
<div align= "Right" > Your content:
</div>
</td>
<TD height= "width=" "77%" >
<textarea name= "Content" cols= "rows=" class= "eq" ></textarea>
</td>
</tr>
<tr bgcolor= "#66cccc" >
<TD colspan= "2" height= ">"
</td>
</tr>
<tr bgcolor= "#66cccc" >
<TD colspan= "2" height= ">"
<div align= "center" >
<input type= "Submit" Name= "said" value= "first say these" >
<input type= "reset" name= "Submit2" value= "Start Over" >
</div>
</td>
</tr>
</table>
</form>
Said.php End
Post content, and on this page to thread
content.php
Begin
<?php
$result =mysql_query ("Select
Over_post.title,over_post.content,over_post.postat,over_post.readed,over_post.rootid,over_post.la
Yer,over_post.orders from Over_post where over_post.id= $id ");
$readed =mysql_result ($result, 0, "readed");
$title =mysql_result ($result, 0, "title");
$content =mysql_result ($result, 0, "content");
$date =mysql_result ($result, 0, "Postat");
$rootid =mysql_result ($result, 0, "Rootid");
$orders =mysql_result ($result, 0, "orders");
$layer =mysql_result ($result, 0, "layer");
?>
<table width= "90%" cellspacing= "1" bgcolor= "Red" align= "center" >
<tr bgcolor= "#ffffff" >
<TD width= "12%" > Publishing Person:</td>
</tr>
<tr bgcolor= "#ffffff" >
<TD width= "12%" > Theme:</td>
<TD colspan= "5" > "<?php echo $title;?>" <font color= "666666" ><i> "
Readed:<?echo "$readed";? > "<?echo" $date "; ></i></font></td>
</tr>
<tr bgcolor= "#ffffff" >
<TD width= "12%" > Content:</td>
<TD colspan= "5" ><?php echo $content;?></td>
</tr>
</table>
<br>
<center>
Forum post Thread
<form method= "POST" action= "post.php" >
<table width= "78%" cellspacing= "0" align= "center" cellpadding= "0" border= "0" >
<tr bgcolor= "#ffffff" >
<TD height= "width=" "12%" >
<div align= "Right" > topic:</div>
</td>
<TD height= "width=" "88%" >
<input type= "text" name= "title" class= "eq" size= "M" >
</td>
</tr>
<tr bgcolor= "#ffffff" >
<TD height= "width=" "12%" >
<div align= "Right" > Content:</div>
</td>
<TD height= "width=" "88%" >
<textarea name= "content" class= "eq" cols= "rows=" ></textarea>
</td>
</tr>
<tr bgcolor= "#ffffff" >
<TD height= "colspan=" 2 ">
</td>
</tr>
<tr bgcolor= "#ffffff" >
<TD height= "colspan=" 2 ">
<div align= "center" >
<input type= "Submit" name= "reply" value= "thread" class=in>
<input type= "reset" value= "rewrite" class=in name= "reset" >
<input type= "hidden" name= rootid "value=" <?echo $rootid;? > ">
<input type= "hidden" name= "orders value=" <?echo$orders;? > ">
<input type= "hidden" name= "layer" value= "<?echo$layer";? > ">
</div>
</td>
</tr>
</table>
Content.php End
Update Database
post.php
Begin
?
$content =nl2br (Htmlspecialchars ($content));
$title =htmlspecialchars ($title); Never allow HTML on headers
$date =date ("y-m-d h:i:s");
$length =strlen ($content);
if (Isset ($said))//Post new posts
{
$query = "INSERT INTO Over_post
VALUES (null, ' $title ', ' $content ', $user _id, ' $date ', 0, $length, $img, ', ', ', ') ';
$result =mysql_query ($query) or Die (Mysql_error ());
$r =mysql_query ("SELECT Max (ID) from Over_post");
$rootid = mysql_result ($r, 0) +0;
mysql_query ("Update over_post set rootid= $rootid where id= $rootid") or Die (Mysql_error ());
}
if (Isset ($reply))://Post Thread
mysql_query ("Update over_post set orders=orders+1 where rootid= $rootid and orders> $orders") or
Die (Mysql_error ());
$layer = $layer +1;
$orders = $orders +1;
$query = "INSERT INTO Over_post
VALUES (null, ' $title ', ' $content ', $user _id, ' $date ', 0, $length, $img, $rootid, $orders, $layer) ";
$result =mysql_query ($query) or Die (Mysql_error ());
endif
if ($result) {
Include "list.php";
}
?>
Post.php End
Plays, showing all posts, and implementing pagination
list.php
Begin
<?php
Find the Rootid of the latest topic
$query = "Select Max (Rootid) as Maxid1, Min (rootid) as minid1 from Over_post";
$result = mysql_query ($query);
$maxid 1 = mysql_result ($result, 0, "maxid1");
$startid 1 = mysql_result ($result, 0, "minid1");
if (!) ( $maxid 1>0)) $maxid 1=0;
if (!) ( $startid 1>0)) $startid 1=0;
$totalid 1 = $maxid 1; This is really the largest rootid value, $maxid 1 to be changed according to $NEXTMAXID1
if ($nextmaxid 1>0) $maxid 1= $nextmaxid 1; Page
Calculate the minimum Rootid: note the following DESC, combined with limit, to ensure the selection of the range of records.
If you use ASC, retrieving the No. 0 in Mysql_result will be a big mistake!
$itemsperpage = 30;
$query = "SELECT distinct Rootid from Over_post where rootid<= $maxid 1 order BY Rootid DESC
$itemsperpage ";
$r =mysql_query ($query);
$n =mysql_num_rows ($R);
if ($n >0) {
$minid 1=mysql_result ($r, $n-1);
$query = "SELECT * from Over_post where rootid<= $maxid 1 and rootid>= $minid 1 order by Rootid
Desc,orders ";
$result =mysql_query ($query);
$num =mysql_num_rows ($result);
}
else {
$minid 1=0;
$maxid 1=0;
echo "<p><center><font color= ' #ff0000 ' > No more Speeches </font></center>";
}
$query = "SELECT distinct Rootid from Over_post where rootid> $maxid 1 order by Rootid limit
$itemsperpage ";
$r =mysql_query ($query);
$n =mysql_num_rows ($R);
if ($n >0) $up =mysql_result ($r, $n-1);
else $up = $totalid 1;
$query = "SELECT distinct Rootid from Over_post where rootid< $minid 1 order BY Rootid DESC
$itemsperpage ";
$r =mysql_query ($query);
$n =mysql_num_rows ($R);
if ($n >0) $down =mysql_result ($r, 0);
else $down = $maxid 1;
?>
<div align=center>
<br>
<table width= "90%" border= "0" cellspacing= "1" cellpadding= "0" align= "center" >
<tr bgcolor= "2880AC" >
<TD height= align= "center" ><font color= "#ffffff" ><a
Href= "Javascript:window.location.reload ()" Class=a1> Refresh </a>
<a href= "list.php" class=a1> home </a> <a href= "list.php?nextmaxid1=<?echo$up;? > "class=a1>
on page </a>
<a href= "List.php?nextmaxid1=<?echo$down;? > "class=a1> next page </a> <?echo" Existing topics
: $startid 1-$totalid 1 ";? >
</font></td>
</tr>
<tr>
<TD height= "bgcolor=" "#e0f0f8" cellspacing= "1" >
<?php
echo "<ul>";
while ($array =mysql_fetch_array ($result)) {
$id = $array ["id"];
$title = $array ["title"];
$content = $array ["Content"];
$postat = $array ["Postat"];
$readed = $array ["readed"];
$size = $array ["Size"];
if ($size ==0) $size = "no content";
else $size. = "bytes";
$rootid = $array ["Rootid"];
$orders = $array ["Orders"];
$layer = $array ["Layer"];
$ul = ""; Start tree structure
$_ul= "";
for ($j =0; $j < $layer; $j + +) {
$ul = $ul. " <ul> ";
$_ul=$_ul. " </ul> ";
}
echo $ul. " <li> "." <a href=\ "content.php?id= $id \ class=big> $title </a><font color=006699> ($size)"
". Author." "$postat < read: $readed > </font><br>". $_ul;
Flush ();
End of tree structure
}
?><br></td>
</tr>
<tr bgcolor= "2880AC" >
<TD height= align= "center" ><font color= "#ffffff" ><a
Href= "Javascript:window.location.reload ()" Class=a1> Refresh </a>
<a href= "list.php" class=a1> home </a> <a href= "list.php?nextmaxid1=<?echo$up;? > "class=a1>
on page </a>
<a href= "List.php?nextmaxid1=<?echo$down;? > "class=a1> next page </a> <?echo" Existing topics
: $startid 1-$totalid 1 ";? >
</font></td>
</tr>
</table>
</div>
List.php End