一個在PHP中利用遞迴實現論壇分級顯示的例子(為了簡單起見,我將分頁顯示部分去掉了)

來源:互聯網
上載者:User
遞迴|分頁|顯示 <?php
/*存放文章的表結構
CREATE TABLE announce (
   announce_id int(11) NOT NULL auto_increment,
   board_id smallint(6) NOT NULL,
   title varchar(100) NOT NULL,
   content tinytext,
   add_time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
   auth_name varchar(20) NOT NULL,
   auth_mail varchar(40),
   hit_count smallint(6) NOT NULL,
   bytes mediumint(9) NOT NULL,
   parent_id tinyint(4) NOT NULL,
   auth_ip varchar(15) NOT NULL,
   top_id int(11) NOT NULL,
   return_count tinyint(4) NOT NULL,
   face char(3) NOT NULL,
   PRIMARY KEY (announce_id),
   KEY board_id (board_id),
   KEY top_id (top_id)
);
*/

function show_announce($id,$self_id){
    global $dbconnect;
    global $board_id;
    $query="select * from announce where announce_id='$id'";
    $result=mysql_query($query,$dbconnect);
    $myrow=mysql_fetch_array($result);
    mysql_free_result($result);
    echo "<li>\n";
    echo "<img src='http://www.163design.net/p/b/images/mood".$myrow[face].".gif'> ";
    if($self_id!=$id)
        echo "<a href='show.php3?board_id=$board_id&announce_id=$myrow[announce_id]&top_id=$myrow[top_id]'>";
    echo $myrow[title];
    if($self_id!=$id)
        echo "</a>";
    echo " - <strong>【".$myrow[auth_name]."】</strong> ".$myrow[add_time]." <font color=darkblue>[id:$myrow][announce_id] 點擊:$myrow[hit_count]]</font> ($myrow[bytes] Bytes) <font color=red>($myrow[return_count])</font>\n";
    $query="select announce_id from announce where parent_id='$id' order by announce_id desc";
    $result=mysql_query($query,$dbconnect);
        echo "<ul>\n";
    while($myrow=mysql_fetch_array($result)){
        show_announce($myrow[announce_id],$self_id);
    }
        echo "</ul>\n";
    mysql_free_result($result);
    echo "</li>";
}
?>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <title>論壇內容</title>
<link rel="stylesheet" type="text/css" href="common.css">
</head>

<body>
<?php
//此處需要串連資料庫
//可以根據需要加入分頁
$query="select announce_id from announce where top_id='0' order by announce_id desc ";
$result_top=mysql_query($query,$dbconnect);
echo "<ul>\n";
while($myrow_top=mysql_fetch_array($result_top)){
    show_announce($myrow_top[announce_id],0);
}
echo "</ul>\n";
mysql_free_result($result_top);
?>
</body>
</html>


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.