php中文章標題很多的情況下怎樣才能分頁顯示呢?
下面根據本人的體會把主要代碼公開,希望對初學網友有所幫助.
$page_size=10;(每頁顯示數量)
$sql='select count(*) as rnt from mydbf';
$sql .= ' order by mytime desc ';
$qh=mysql_query($sql);
$data=mysql_fetch_array($qh);
$nr=$data["cnt"];
$pages=intval($nr/$page_size);
if($nr%$page_size) $pages ;
if(empty($page))
{
$page=1;
$offset=0;
}
else
{
$offset=$page*$page_size-$page_size;
}
?>
<!--
$total = mysql_numrows($qh);
$total=$nr;
echo "<td colspan=3>";
//顯示所有文章
if (!isset($page)){
$page = 0;
}
//分頁開始
$start= $page*$page_size;
$offset=$start;
$totalpage =ceil($total/$page_size);
$pagestr ='[<font color=green>總數</font>]:'.$total;
$pagestr.= " ".($page 1).'/'.$totalpage.'頁'." ";
$xjz1= "<a href=$php_self?";
$sql = 'select mytxt,content_id,clicknum,from mydbf ';
$sql .= ' order by ltime desc limit '.$offset.','.$pageno;
$res = mysql_query($ls_sql, $dbh);
$countloop = 0;
while ($row = mysql_fetch_array($res)) {
....
}
$xjz1= "<a href=$php_self?";
$xjz2=substr($xjz1,8);
?>
<table border="0" width="100%" cellpadding="0" cellspacing="0" height="0">
<form action="<?echo $php_self.$xjz2."&page=$page";?>" method="post">
<td width="100%" height="9">
<table border="0" width="100%" cellspacing="0" cellpadding="0">
<tr><td width="72%" align="center" nowrap> <b>分頁</b>:<?if($page==1){?>首頁
上一頁 <?
}
else
{
$p=$page-1;
echo "$xjz1&page=1>首頁</a> $xjz1&page=$p>上一頁</a>";
}
if($page>=$pages){?> 下一頁 尾頁 <?
}
else
{
$pp=$page 1;
echo " $xjz1&page=$pp>下一頁</a> $xjz1&page=$pages>尾頁</a>";
}
?> 頁次:<b><?echo $page?></b>/<b><?echo $pages?></b>頁 <b><?echo $page_size?></b>個/頁 共<b><?echo $nr?></b>篇文章 轉到:
<!--<input type="text" name="page" size="4" class="p1" style="border-style: groove; border-width: 1">
<input type="submit" value="Go" name="B1" class="p1" style="border-style: ridge; border-width: 1">
</td><td>-->
<select name=pagex onChange="javascript:location.href=this.options[this.selectedIndex].value">
<?
for ($i=1;$i<=$pages;$i ){
if($i==$page)$selectxjz="selected";
else $selectxjz='';
echo "<option value="".$xjz2."&page=".$i."" ".$selectxjz.">".$i."</option>";
}
?>
</select> 頁
</td></table></td></form></table>
<?}
?>