具有树状结构的文本论坛(四)——主题列表

    这是一开始进入该论坛看到的界面,这部分还包括了分页显示系统。该界面文件listtheme.php在$action参数没有指定或指定为themelist的时候被包含进来。下面是该文件的代码:
<script language="JavaScript">
  function MM_jumpMenu(targ,selObj,restore)
  {
    eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
    if (restore) selObj.selectedIndex=0;
  }
</script>
<?
  
if(!isset($listfrom))
     
$listfrom 1;
  
get_theme($listfrom$themeid$theme$maker$maketime$notes);
  echo 
"<table width='100%' border='1'>";
  echo 
"<tr bgcolor='#006600'>";
  echo 
"<th width='10%'><div align='center'><font color='#FFFFFF'>序号</font></div></th>";
  echo 
"<th width='45%'><div align='center'><font color='#FFFFFF'>主题</font></div></th>";
  echo 
"<th width='15%'><div align='center'><font color='#FFFFFF'>创建人</font></div></th>";
  echo 
"<th width='20%'><div align='center'><font color='#FFFFFF'>创建时间</font></div></th>";
  echo 
"<th width='10%'><div align='center'><font color='#FFFFFF'>帖子数</font></div></th>";
  echo 
"</tr>";
  for(
$i 0$i count($themeid); $i ++)
  {
    if(
$i == 0)
      
$bgcolor '#CCFF99';
    else
      
$bgcolor '#A9FFA9';
    echo 
"<tr bgcolor= $bgcolor>";
    echo 
"<td width='10%'><div align='center'>$themeid[$i]</div></td>";
    echo 
"<td width='45%'><div align='center'><a href = 'forum.php?action=read&id=$themeid[$i]&title=$theme[$i]&notes=$notes[$i]' title = '进入"$theme[$i]"主题'>$theme[$i]</a></div></td>"
    echo 
"<td width='15%'><div align='center'>$maker[$i]</div></td>";         
    echo 
"<td width='20%'><div align='center'>$maketime[$i]</div></td>";         
    echo 
"<td width='15%'><div align='center'>$notes[$i]</div></td>";         
    echo 
"</tr>";
  }
  echo 
"</table><br>";
  
  
//下面就是分页实现代码
  
$total_theme get_total_theme();
  
$total_page intval($total_theme THEMES_IN_PAGE);
  
$current_page intval($listfrom THEMES_IN_PAGE 1);
  if(
$total_theme THEMES_IN_PAGE)
    
$total_page ++;
    
  echo 
"<table width=100% border=0><tr><td width=50%>";
  if(
$listfrom <= THEMES_IN_PAGE)
  {
      echo 
"[首页][前页]&nbsp;";
  }
  else
  {
    echo 
"[<a href='forum.php?action=themelist&listfrom=1'>首页</a>]"
    echo 
"[<a href='forum.php?action=themelist&listfrom=".($listfrom THEMES_IN_PAGE)."'>前页</a>]&nbsp;";
  }
  echo 
"第".$current_page."/".$total_page."页&nbsp;";
  if(
$total_theme $listfrom THEMES_IN_PAGE)
    echo 
"[后页][末页]</td>";
  else
  {
    echo 
"[<a href='forum.php?action=themelist&listfrom=".($listfrom THEMES_IN_PAGE)."'>后页</a>]";
    echo 
"[<a href='forum.php?action=themelist&listfrom=".($total_theme $total_theme THEMES_IN_PAGE 1)."'>末页</a>]&nbsp;</td>";
  }
  echo 
"<form name=sortsel>";
  
$htmlstr "<td width=20%>转到<select name='gopage' onChange="MM_jumpMenu('parent',this,0)"><option selected>---</option>";
  for(
$i 1$i <= $total_page$i ++)
  {
    if(
$i != $current_page)
    {
      
$go = ($i 1) * THEMES_IN_PAGE 1;
      
$htmlstr $htmlstr."<option value='forum.php?action=themelist&listfrom=$go'>$i</option>";
    }
  }
  echo 
$htmlstr."</select>页</td></form>";
  
  echo 
"<form name='searchform' method='post' action='forum.php?action=search'><td>搜索:<input type='text' name='keyword' size=10><input type='Submit' name='submit' value='搜索'><td></form>";
  echo 
"</table></tr>";
?>
    前面有一段Javascript代码是定义转跳页面下拉菜单的响应函数MM_jumpMenu()的。其中的get_theme()函数是这段代码的重点所在,它是把所要求的主题的相关信息读入五个数组内,然后才把五个数组的内容分别显示出来。在funcdef.php文件中,get_theme()函数的定义如下:
<?
  
//将主题列表中指定范围的主题ID、主题内容、创建人、创建时间、帖子数目列入五个数组中
  
function get_theme($themefrom, &$themeid, &$theme, &$maker, &$maketime, &$notes$themenum THEMES_IN_PAGE)
  {
    if(!
file_exists("rec/themelist"))
    {
       
$fp fopen("rec/themelist""w+");
       
fwrite($fp"0n0n");
       
fclose($fp);
    }
    
$fp fopen("rec/themelist""r");
    
    
fgets($fpMAX_READ_BYTES);
    
fgets($fpMAX_READ_BYTES);
    
$begin ftell($fp);
    
$curpos filesize("rec/themelist") - 1;
    
fseek($fp$curpos);    
    
    for(
$i 0$i $themefrom$i ++)
    {
      while(
fgetc($fp) != "n" && $curpos >= $begin)
      {
        
fseek($fp, --$curpos);
      }
      
$curpos -= 2;
      
fseek($fp$curpos);  
    }
    
    
//连续倒序读入文件
    
for($i 0$i $themenum && $curpos >= $begin$i ++)
    {
      while(
fgetc($fp) != "n" && $curpos >= $begin)
      {
        
fseek($fp, --$curpos);
      }  
      
$rows fgets($fpMAX_READ_BYTES);
      
$themeid[$i] = strtok($rows"t");
      
$theme[$i] = strtok("t");
      
$maker[$i] = strtok("t");
      
$maketime[$i] = strtok("t");
      
$notes[$i] = strtok("t");
      
$curpos -= 2;
      
fseek($fp$curpos);
    }
    
fclose($fp);
  }
?>
    注意到它是倒序读入themelist文件的,$themefrom是指从倒数第几个主题开始读入,$themenum是读入的主题个数,一般就等于每页显示的个数。
    而分页代码中的get_total_theme()函数是取得该论坛现有的总主题数目。在funcdef.php文件中定义如下:
<?
  
//获得主题总数
  
function get_total_theme()
  {
    if(!
file_exists("rec/themelist"))
    {
       
$fp fopen("rec/themelist""w+");
       
fwrite($fp"0n0n");
       
fclose($fp);
    }
    
$fp fopen("rec/themelist""r");
    
$rows fgets($fpMAX_READ_BYTES);
    
fclose($fp);
    return 
trim($rows);
  }
?>
    下次将介绍主题显示部分,这是该论坛的特色,主题中的帖子具有树状结构。(未完待续)