<?
/*存放貼子的表結構------------------------------------------------------
create table bbsrow(
bbsrow_id int(6) not null auto_increment, //貼子ID號
bbsrow_auth varchar(20) not null, //貼子作者
bbsrow_parentid int(6), //貼子的父親貼子ID號,如為首發貼則為空
bbsrow_title varchar(200) not null, //貼子標題
bbsrow_returncount int(3), //貼子的回復貼數,如果沒有回貼則為空
primary key (bbsrow_id)
);
-----------------------------------------------------------------------------*/
//顯示兒子貼的遞歸函數--------------------------------------------------
function showchildren($parent_id){
global $connect_id;
$query="select * from bbsrow where bbsrow_parentid='" . $parent_id . "'";
$result_top=mysql_query($query,$connect_id);
echo "<ul>
";
while($myrow_child=mysql_fetch_row($result_top)){
echo "<li>";
echo $myrow_child[0];
echo $myrow_child[1];
echo $myrow_child[2];
echo $myrow_child[3];
echo $myrow_child[4] . "
";
//如果回復貼數不為空,則表示有兒子貼,繼續顯示兒子貼
if($myrow_child[4]!=''){
showchildren($myrow_child[0]);
}
}
echo "</ul>";
}
//----------------------------------------------------------------------
//連接數據庫並將所有首發貼放到$mainrow數組裡----------------------------
$connect_id=mysql_connect("localhost","test","test") or die("無法連接數據庫");
mysql_select_db("bbs") or die("無法選擇數據庫");
$query="select * from bbsrow where bbsrow_parentid=''";
$result=mysql_query($query,$connect_id);
$i=0;
while($myrow=mysql_fetch_row($result)) {
$mainrow[$i][0]=$myrow[0];
$mainrow[$i][1]=$myrow[1];
$mainrow[$i][2]=$myrow[2];
$mainrow[$i][3]=$myrow[3];
$mainrow[$i][4]=$myrow[4];