<?php
//把DSN改成适合你自己的配置
//建立数据库连接
$forumconn=odbc_connect("forum","sa","");
//获得总共的0层帖子条数
$sqlTotal="select count(*) from forum where f_level=0";
$rsTotal=odbc_exec($forumconn,$sqlTotal);
odbc_fetch_into($rsTotal,&$total);
$totalPost=$total[0];
///////////////////////////////////////////
//设定每页显示的记录条数(不包括回复的)
$pageSize=20;
///////////////////////////////////////////
///////////////////////////////////////////
if ($totalPost%$pageSize<>0)
{
$totalPages=intval($totalPost/$pageSize)+1;
}
else
{
$totalPages=intval($totalPost/$pageSize);
}
?>
<html>
<head>
<title>PHP论坛</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style>
td { font-size: 9pt}
a:link { font-size: 9pt; text-decoration: none; color: #ffff00}
a:visited { text-decoration: none; color: #ffff00; font-size:9pt}
a:active { color: #660000; font-size:9pt}
a:hover { color: #FF0033; font-size:9pt}
a {font-szie:9pt}
body {font-size:9pt}
input {font-size:9pt}
</style>
</head>
<BODY bgcolor="#3399ff" leftmargin="0" topmargin="0">
<table align="center" border="1" width="90%" cellspacing="0" bordercolordark="#FFFFFF" bordercolorlight="#000000" bgcolor="#FAD185" cellpadding="3" >
<tr>
<td align=center>版面:PHP论坛 精华>> 查询>></td>
<td align=center>版主:Kain</td>
<td align=center>加贴</td>
<td align=center>刷新</td>
<td align=center>分页
<?php
if (isset($goPage))
$currentPage=$goPage;
else
$currentPage=1;
if ($currentPage==1)
echo "<font color=darkgray>首页 前页</font> ";
else
{
$previousPage=$currentPage-1;
echo "<a href='index.php?goPage=1'>首页</a> <a href='index.php?goPage=$previousPage'>前页</a>";
}
if ($currentPage==$totalPages)
echo "<font color=darkgray>后页 尾页</font> ";
else
{
$nextPage=$currentPage+1;
echo "<a href='index.php?goPage=$nextPage'>后页</a> <a href='index.php?goPage=$totalPages'>尾页</a>";
}
echo "页次:<strong>$currentPage</strong>/<strong>$totalPages</strong>页 "
?>
</td>
<td>
<form action="index.php" method="get">
转到:<input type=hidden name=BoardID value=31> <input type="text" name="goPage" size=5 maxlength=10 value=1>
<input class=buttonface type="submit" value=" Goto " name="B1">
</form>
</td>
</tr>
</table>
<?php
///////////////////////////////////////////////////
//函数由此开始。
//showTime the test function
function showTime()
{
$strtime = "2000-04-14 10:33:35";
$array = explode("-",$strtime);
$year = $array[0];
$month = $array[1];
$array = explode(":",$array[2]);
$minute = $array[1];
$second = $array[2];
$array = explode(" ",$array[0]);
$day = $array[0];
$hour = $array[1];
$timestamp = mktime($hour,$minute,$second,$month,$day,$year);
echo "字符串时间:$strtime<br>";
echo "年:$year<br>";
echo "月:$month<br>";
echo "日:$day<br>";
echo "时:$hour<br>";
echo "分:$minute<br>";
echo "秒:$second<br>";
echo "转换为timestamp:" . $timestamp . "<br>";
echo "从timestamp转换回来:" . date("Y-m-d H:i:s",$timestamp) . "<br>";
}
//分级显示需要一个递归函数
//$id是当前帖子的ID,$level是当前帖子的层次
function ShowAll($id,$level)
{
global $forumconn;
global $m;
$level=$level+1;
//当层次不大于最大层次时递归
if($level > $m)
return;
else
{
$sqlThisLevel="select * from forum where f_level=".$level." and f_reply_id=".$id." order by f_post_time desc";
//用数组保存结果集,避免下层生成的结果集覆盖上层的结果集
$rsThisLevel[$level]=odbc_exec($forumconn,$sqlThisLevel);
if ($rsThisLevel[$level])
{
while (odbc_fetch_into($rsThisLevel[$level],&$thisLevel))
{
$sqlUser="select * from forum_users where u_id=".$thisLevel[4];
$resultUser=odbc_exec($forumconn,$sqlUser);
odbc_fetch_into($resultUser,&$thisuser);
echo "<ul><li><a href=viewmessage.php?id=".$thisLevel[0]." target=view>".$thisLevel[1]."</a> -<b>【".$thisuser[1]."】</b> ";
echo $thisLevel[3]."[ID:".$thisLevel[0]." 点击:".$thisLevel[7]."] ";
echo " <font color=red>(".$thisLevel[8].")</font>";
if (time()-changeTime($thisLevel[3])<259200)
echo " <img src=imgs/new.gif>";
ShowAll($thisLevel[0],$thisLevel[5]);
echo "</li></ul>";
}
}
}
}
//把时间字符串转换为TimeStamp
function changeTime($strtime)
{
$array = explode("-",$strtime);
$year = $array[0];
$month = $array[1];
$array = explode(":",$array[2]);
$minute = $array[1];
$second = $array[2];
$array = explode(" ",$array[0]);
$day = $array[0];
$hour = $array[1];
$timestamp = mktime($hour,$minute,$second,$month,$day,$year);
return $timestamp;
}
//函数至此结束。
///////////////////////////////////////////////////////////
//分级显示需要取得最大级数
$sqlMaxLevel="select max(f_level) from forum";
$rsMaxLevel=odbc_exec($forumconn,$sqlMaxLevel);
odbc_fetch_into($rsMaxLevel,&$max);
$m=$max[0];
if($forumconn)
{
$sqlShowAll="select * from forum where f_level=0 order by f_post_time desc";
$result=odbc_exec($forumconn,$sqlShowAll);
if($result)
{
$firstShow=($currentPage-1)*$pageSize+1;
odbc_fetch_into($result,$firstShow,&$firstMessage);
if ($currentPage==$totalPages)
$sqlPageShow="select * from forum where f_level=0 and f_id<=".$firstMessage[0]." order by f_id DESC";
else
$sqlPageShow="select top ".$pageSize." * from forum where f_level=0 and f_id<=".$firstMessage[0]." order by f_id DESC";
$rsPageShow=odbc_exec($forumconn,$sqlPageShow);
echo "<ul>";
while (odbc_fetch_into($rsPageShow,&$thisarray))
{
//从forum_users表中找出贴子相对应的用户信息
$sqlUser="select * from forum_users where u_id=".$thisarray[4];
$resultUser=odbc_exec($forumconn,$sqlUser);
odbc_fetch_into($resultUser,&$thisuser);
// echo "<ul><li>".$thisarray[1]." -<b>【".$thisuser[1]."】</b> ";
echo "<li><a href=viewmessage.php?id=".$thisarray[0]." target=view>".$thisarray[1]."</a> -<b>【".$thisuser[1]."】</b> ";
echo $thisarray[3]."[ID:".$thisarray[0]." 点击:".$thisarray[7]."] ";
echo " <font color=red>(".$thisarray[8].")</font>";
if (time()-changeTime($thisarray[3])<259200)
echo " <img src=imgs/new.gif>";
ShowAll($thisarray[0],$thisarray[5]);
// echo "</li></ul>";
echo "</li>";
}
echo "</ul>";
}
else
echo "连接数据表失败";
}
else
echo "连接数据库失败";
odbc_close_all();
?>
</body>
</html>
//////////////////////////////////////////////////////////////////////
附:数据库
if exists (select * from sysobjects where id = object_id(N'[dbo].[forum]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[forum]
GO
if exists (select * from sysobjects where id = object_id(N'[dbo].[forum_users]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[forum_users]
GO
CREATE TABLE [dbo].[forum] (
[f_id] [int] IDENTITY (1, 1) NOT NULL ,
[f_title] [varchar] (1000) NOT NULL ,
[f_content] [varchar] (4000) NULL ,
[f_post_time] [smalldatetime] NOT NULL ,
[f_uid] [int] NOT NULL ,
[f_level] [smallint] NOT NULL ,
[f_reply_id] [int] NOT NULL ,
[f_readed] [int] NOT NULL ,
[f_replies] [int] NOT NULL ,
[f_img] [smallint] NOT NULL ,
[f_great] [bit] NOT NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[forum_users] (
[u_id] [int] IDENTITY (1, 1) NOT NULL ,
[u_nickname] [varchar] (20) NOT NULL ,
[u_name] [varchar] (200) NOT NULL ,
[u_sex] [bit] NOT NULL ,
[u_pwd] [varchar] (20) NOT NULL ,
[u_mail] [varchar] (100) NOT NULL ,
[u_company] [varchar] (200) NULL ,
[u_homepage] [varchar] (200) NULL ,
[u_level] [smallint] NOT NULL ,
[u_sign] [varchar] (500) NULL
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[forum] WITH NOCHECK ADD
CONSTRAINT [PK_forum] PRIMARY KEY NONCLUSTERED
(
[f_id]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[forum_users] WITH NOCHECK ADD
CONSTRAINT [PK_forum_users] PRIMARY KEY NONCLUSTERED
(
[u_id]
) ON [PRIMARY]
GO
|