php-分页实例

 1<?
 2$query_string='cateid='.$cateid;
 3$sql="select id,title,author,nowtime,view_times from companynews where cate=$cateid and subcate=3 order by id desc";
 4$dblink->query($sql);
 5$num=$dblink->num_rows();
 6$rec_num=$num;
 7$rec_per_page=30;
 8if(isset($_GET["page"]) && floor($_GET["page"])>0)
 9    {
10        $page=floor(trim($_GET["page"]));
11    }
12else
13    {
14        $page=1;
15    }
16if(ceil($num/$rec_per_page)>=$page)
17    {
18        $start=$rec_per_page*($page-1);
19    }
20else
21    {
22        $page=1;
23        $start=0;
24    }
25if($num>$page*$rec_per_page)
26    {
27        $this_page_count=$rec_per_page;
28    }
29else
30    {
31        $this_page_count=$num-($page-1)*$rec_per_page;
32    }
33if($num==0)
34    {
35?> 
36                <tr> 
37                  <td colspan="5" height="50"><b><font color="#FF0000">对不起,数据库中暂时没有公司动态信息!</font></b></td>
38                </tr>
39                
40<?}
41else
42    {?> 
43                <tr bgcolor="#DDDDDD"> 
44                  <td height="30" align="center" width="40" nowrap>序号</td>
45                  <td >主题</td>
46                  <td width="60">来源</td>
47                  <td width="80">发布时间</td>
48                  <td align="center" width="0">&nbsp;</td>
49                </tr>
50                <?
51    for($i=0;$i<$num;$i++)
52    {
53        $article_data=$dblink->next_record();
54        if($i<$start || $i>=$start+$this_page_count)
55            {
56            continue;
57            }
58
59?> 
60                <tr bgcolor="#EFEFEF"> 
61                  <td align="center" height="20" bgcolor="#EFEFEF"><?echo $i+1;?></td>
62                  <td nowrap ><a href="article.php?id=<?echo trim($article_data["id"]);?>" title="<?echo trim($article_data["title"]);?>"><?echo csubstr(trim($article_data["title"]),0,70);?></a></td>
63                  <td ><?echo trim($article_data["author"]);?></td>
64                  <td nowrap><?echo trim($article_data["nowtime"]);?></td>
65                  <td align="center">&nbsp;</td>
66                </tr>
67                <?
68    }
69}?> 
70                <tr> 
71<td colspan="5" height="80"><b>
72<?
73echo "总共记录数目:<font color='#FF0000'><b>".$rec_num."</b></font>。
74        每页有<font color='#FF0000'><b>$rec_per_page</b></font>条记录。
75        共有<font color='#FF0000'><b>".ceil($rec_num/$rec_per_page)."</b></font>页。
76        当前是第<font color='#FF0000'><b>$page</b></font>页。<br><br>";
77if($page==1)
78    {
79    echo "<font color='#CCCCCC'>第一页 上一页 </font>";
80    }
81else
82    {
83    echo "<a href=\"$File_Name?page=1&$query_string\">第一页</a> "."<a href=\"$File_Name?page=".($page-1)."&$query_string\">上一页</a> ";
84    }
85if($this_page_count!=$rec_per_page || $rec_num==$page*$rec_per_page)
86    {
87    echo "<font color='#CCCCCC'>下一页 最后一页</font>";
88    }
89else
90    {
91    echo "<a href=\"$File_Name?page=".($page+1)."&$query_string\">下一页</a> "."<a href=\"$File_Name?page=".ceil($rec_num/$rec_per_page)."&$query_string\">最后一页</a>";
92    }
93if(ceil($rec_num/$rec_per_page)>1)
94    {
95    echo "转到<input type='input' name='input_page' class='input' style='width:30'  onKeyDown=\"if(window.event.keyCode==13){location.href='$File_Name?page='+input_page.value+'&$query_string'}\">页 <input type='button' name='go' value='GO' class='input' onClick=\"Javascript:location.href='$File_Name?page='+input_page.value+'&$query_string'\">";
96    }
97?> </b></td>
posted @ 2008-08-28 17:15  混混的IT生涯  阅读(782)  评论(0编辑  收藏  举报