当前位置: 首页 > 图文教程 > 网络编程 > PHP > 用PHP+MYSQL实现论坛里的分级+分页显示

PHP
《PHP设计模式介绍》第十三章 适配器模式
《PHP设计模式介绍》第十四章 动态记录模式
《PHP设计模式介绍》第十五章 表数据网关模式
《PHP设计模式介绍》第十六章 数据映射模式
《PHP设计模式介绍》第十七章 MVC 模式
Zend Framework 入门——快速上手
Zend Framework 入门——多国语言支持
Zend Framework 入门——错误处理
Zend Framework 入门——页面布局
详细介绍php5编程中的异常处理
PHP5 OOP编程中的代理与异常
PHP程序的常见漏洞攻击分析
PHP.MVC的模板标签系统
PHP教程:PHP编码书写规范
PHP开发大型项目的方法:OOP思想
php使用curl模拟用户登陆
php对gb编码动态转utf-8编码的几种方法评测
php设计模式介绍之章代理模式
“在phpMyAdmin使用用户口令登陆”补充
PHP入门速成

用PHP+MYSQL实现论坛里的分级+分页显示


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 77 ::
收藏到网摘: n/a

  用PHP+MYSQL实现论坛里的分级+分页显示

<?
/*存放贴子的表结构------------------------------------------------------

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>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";
        //如果回复贴数不为空,则表示有儿子贴,继续显示儿子贴
        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];
    $i++;
}
mysql_free_result($result);
//----------------------------------------------------------------------

//开始构建分页显示------------------------------------------------------

if($currentpage!=""){
    $page=$currentpage;
}
else{
    $page=0;
}

$pagesize=10;//每页显示的首发贴数!
$start=$page*$pagesize;
$end=$start+$pagesize;
if($end>$i) $end=$i;
$totalpage=$i/$pagesize;

     
$info=" 共有" . $i . "条纪录,分" . ceil($totalpage) . "页,当前为第" . ($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){
   &nb