用PHP+MYSQL完成論壇里的分級(jí)+分頁顯示
發(fā)表時(shí)間:2023-07-14 來源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]用PHP+MYSQL實(shí)現(xiàn)論壇里的分級(jí)+分頁顯示 <? /*存放貼子的表結(jié)構(gòu)------------------------------------------------------ crea...
用PHP+MYSQL實(shí)現(xiàn)論壇里的分級(jí)+分頁顯示
<?
/*存放貼子的表結(jié)構(gòu)------------------------------------------------------
create table bbsrow(
bbsrow_id int(6) not null auto_increment, //貼子ID號(hào)
bbsrow_auth varchar(20) not null, //貼子作者
bbsrow_parentid int(6), //貼子的父親貼子ID號(hào),如為首發(fā)貼則為空
bbsrow_title varchar(200) not null, //貼子標(biāo)題
bbsrow_returncount int(3), //貼子的回復(fù)貼數(shù),如果沒有回貼則為空
primary key (bbsrow_id)
);
-----------------------------------------------------------------------------*/
//顯示兒子貼的遞歸函數(shù)--------------------------------------------------
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>n";
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] . "n";
//如果回復(fù)貼數(shù)不為空,則表示有兒子貼,繼續(xù)顯示兒子貼
if($myrow_child[4]!=''){
showchildren($myrow_child[0]);
}
}
echo "</ul>";
}
//----------------------------------------------------------------------
//連接數(shù)據(jù)庫并將所有首發(fā)貼放到$mainrow數(shù)組里----------------------------
$connect_id=mysql_connect("localhost","test","test") or die("無法連接數(shù)據(jù)庫");
mysql_select_db("bbs") or die("無法選擇數(shù)據(jù)庫");
$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];
$i++;
}
mysql_free_result($result);
//----------------------------------------------------------------------
//開始構(gòu)建分頁顯示------------------------------------------------------
if($currentpage!=""){
$page=$currentpage;
}
else{
$page=0;
}
$pagesize=10;//每頁顯示的首發(fā)貼數(shù)!
$start=$page*$pagesize;
$end=$start+$pagesize;
if($end>$i) $end=$i;
$totalpage=$i/$pagesize;
$info=" 共有" . $i . "條紀(jì)錄,分" . ceil($totalpage) . "頁,當(dāng)前為第" . ($page+1) . "/" . ceil($totalpage) . "頁 <br>n";
echo $info;
if($page>0) $pagestr="<a href=bbsrow.php4?currentpage=" . ($page-1) . ">上一頁</a>";
$pagestr=$pagestr . " [第 ";
for($i=0;$i<$totalpage;$i++){
if($i!=$page){
$pagestr=$pagestr . " <a href=bbsrow.php4?currentpage=" . $i . ">" . ($i+1) . "</a> ";
}
else{
$pagestr=$pagestr . " " . ($i+1) . " ";
}
}
$pagestr=$pagestr . "頁]";
if($page<$totalpage-1) $pagestr=$pagestr . "<a href=bbsrow.php4?currentpage=" . ($page+1) .">下一頁</a><p>n";
echo $pagestr;
//----------------------------------------------------------------------
//開始分級(jí)顯示----------------------------------------------------------
echo "<ul>n";
for($i=$start;$i<$end;$i++){
echo "<li>n";
echo $mainrow[$i][0];
echo $mainrow[$i][1];
echo $mainrow[$i][2];
echo $mainrow[$i][3];
echo $mainrow[$i][4] . "n";
//如果回復(fù)貼數(shù)不為空,則表示有兒子貼,繼續(xù)顯示兒子貼
if($mainrow[$i][4]!=''){
showchildren($mainrow[$i][0]);
}
}
echo "</ul>n";
//----------------------------------------------------------------------?>