if(!$offset) //如果$offset=0不显示前一页
{
$preoffset = $offset-8;
echo "<a href=\"$PHP_SELF?offset=$preoffset\">前一页</a> \n";
}
改为:
if($offset>=8) //当$offset大于7,也就是说处在第二页面以上就会显示前一页
{
$preoffset = $offset-8;
echo "<a href=\"$PHP_SELF?offset=$preoffset\">前一页</a> \n";
}
{
$preoffset = $offset-8;
echo "<a href=\"$PHP_SELF?offset=$preoffset\">前一页</a> \n";
}
改为:
if($offset>=8) //当$offset大于7,也就是说处在第二页面以上就会显示前一页
{
$preoffset = $offset-8;
echo "<a href=\"$PHP_SELF?offset=$preoffset\">前一页</a> \n";
}
解决方案 »
- PHP 图片上传,压缩,保存到服务器问题?
- 国外多语言版本电子商务网站的一些问题
- 这行SQL语句如何才能正常使用?
- GB2312转换Unicode的问题...
- 用soapclient 这个返回的数组是什么意思
- linux php连接sqlserver,程序都写好了,跑不了!
- 请高手过来,怎么编译php?(但这种方法不行,有那位还有什么其他的方法吗?)
- 在red linux中jdk和tomcat放在哪里?
- 如何用php判断对方机器的分辨率,如果不是800*600的给出相应提示后,自动转入指定的一页??急!
- PHP中可以通过session id删除服务器上某个session吗?
- 正则表达式问题
- 提问,用PHP的函数怎么实现调用外部的程序呢?
{
//echo "<br>$str<br>$strnum";
$conn_id= db_connect();
$r_id=ifx_query($strnum,$conn_id);
$tmprow = ifx_fetch_row($r_id,1);
for (reset($tmprow);$fieldname=key($tmprow);next($tmprow))
{
$fieldvalue=$tmprow[$fieldname];
$rownums=intval($fieldvalue);
}
ifx_free_result($r_id);
$pages=ceil($rownums/$recordperpage); //总页数
if ($pages==0) $pages=1;
//判断偏移量参数是否传递给了脚本,如果没有就使用默认值0 global $offset;
if (empty($offset))
{
$offset=1;
}
$currentpage=ceil($offset/$recordperpage); //显示当前页
echo("共有".$rownums."条记录 ");
echo("分".$pages."页显示 ");
echo("当前页:".$currentpage."<br>\n");
$endset=$offset+$recordperpage;
$res_id = ifx_query($str,$conn_id);
$row = ifx_fetch_row($res_id,1);
for($k=1;$k<$offset;$k++)
$row = ifx_fetch_row($res_id,"NEXT");
for($j=$offset;$j<$endset&&$j<=$rownums;$j++)
{
echo "<tr>";
for(reset($row);$fieldname=key($row);next($row))
{
if(trim($fdvalue)=="")
$fdvalue=" ";
echo "<td align=center>".$fdvalue."</td>";
}
echo "</tr>";
$row=ifx_fetch_row($res_id,"NEXT");
}
//要写出到所有页面的链接
echo "</table><br>";
$nextoffset=$recordperpage*$currentpage+1;
$prevoffset=$recordperpage*($currentpage-2)+1;
$firstoffset=1;
$lastoffset=($pages-1)*$recordperpage+$firstoffet+1;
$strf="href=\"$PHP_SELF?offset=$firstoffset&type=".$type."\"";
$strl="href=\"$PHP_SELF?offset=$lastoffset&type=".$type."\"";
//判断是否需要上一页连接
if (($currentpage>1) && ($currentpage<=$pages))
$stra="href=\"$PHP_SELF?offset=$prevoffset&type=".$type."\"";
else $stra="";
//判断是否需要下一页连接
if (($pages!=0) && ($currentpage<$pages))
$strb="href=\"$PHP_SELF?offset=$nextoffset&type=".$type."\"";
else $strb="";
echo "<table width=\"40%\" border=\"0\" cellspacing=\"0\">
<tr>
<td>
<div align=\"center\"><a $strf>首页</a></div>
</td>
<td>
<div align=\"center\"><a $stra>前页</a></div>
</td>
<td>
<div align=\"center\"><a $strb>后页</a></div>
</td>
<td>
<div align=\"center\"><a $strl>尾页</a></div>
</td>
</tr></table>";
echo "<br><br><br>";
ifx_free_result($res_id);
ifx_close($conn_id);
}
这个函数用起来绝对好用,只要把数据库的操作改成象性的数据库就可以了
//include("../source.inc/global.php");
print <<<EOT
<link rel="stylesheet" href="../image/style.css">
<table width=75% cellpadding=6 cellspacing=0 border=0 align=center>
<tr><td><table cellpadding="0" cellspacing="5" border="0" bgcolor="#ffffff" width="100%" align="center">
<tr><td colspan="4">
<table cellpadding="4" cellspacing="1" border="0" width="100%">
<tr align="center" valign="middle">
<td background="../image/back.gif"><font face="ms shell dlg,tahoma,tahoma,verdana,arial,helvetica,宋体"><b><a href="namecard.php"><nobr>我的名片夹</nobr></a></b></font></td>
<td background="../image/back.gif"><font face="ms shell dlg,tahoma,tahoma,verdana,arial,helvetica,宋体"><b><a href="namecard.php"><nobr>公司名片夹</nobr></a></b></font></td>
<td background="../image/back.gif"><font face="ms shell dlg,tahoma,tahoma,verdana,arial,helvetica,宋体"><b><a href="addcard.php">添加名片</a></b></font></td>
<td background="../image/back.gif"><font face="ms shell dlg,tahoma,tahoma,verdana,arial,helvetica,宋体"><b><a href="searchcard.php">名片查询</a></b></font></td>
<td background="../image/back.gif"><font face="ms shell dlg,tahoma,tahoma,verdana,arial,helvetica,宋体"><b><a href="cardaction.php?action=delall&recoduser='$loginname'">删除所有名片</a></b></font></td>
</tr>
</table>
</td></tr></table>EOT;//显示名片夹的纪录
function show_card($row)
{
global $num;
$cardid = $row[cardid];
$company = $row[company];
$name = $row[name];
echo "<tr bgcolor=#ffffff>
<td align=center noWrap width=30 height=\"24\"><img src=\"../image/icon.gif\"></td>
<td align=center noWrap width=120 height=\"24\"><font face=verdana><a href=\"viewcard.php?cardid=$cardid\">$name</a></font></td>
<td align=center noWrap width=50%><font face=verdana><a href=\"viewcard.php?cardid=$cardid\">$company</a></font></td>
<td align=center noWrap><font face=verdana><a href=\"namecardaction.php?action=edit&cardid=$cardid\">编辑</a></font></td>
<td align=center noWrap><font face=verdana><a href=\"namecardaction.php?action=delone&cardid=$cardid\">删除</a></font></td>
</tr>";}//得到名片纪录function get_namecard()
{
global $loginname; //用户登录名
global $offset;
mysql_connect("localhost","root",""); //连接数据库
mysql_select_db("test");
$sql = "select cardid,company,name from namecard";//where owner='$loginname' order by cardid desc";
$result = mysql_query($sql) or die(mysql_error());
$num = mysql_num_rows($result) or die(mysql_error());
if($num !=0)
{
if(empty($offset)||$offset<0)
{
$offset = 0;
}
$sql2 = "select cardid,company,name from namecard LIMIT $offset,8";
$result2 = mysql_query($sql2);// or die(mysql_error()); /* for($i=0;$i<$num;$i++)
{
$row = mysql_fetch_array($result2);
show_card($row);
}*/
while ($row = mysql_fetch_array($result2))
{
show_card($row);
}
echo "<tr bgcolor=#ffffff><td align=center colspan=5>";
if($offset>=8) //当$offset大于7,也就是说处在第二页面以上就会显示前一页
{
$preoffset = $offset-8;
echo "<a href=\"$PHP_SELF?offset=$preoffset\">前一页</a> \n";
} $pages = ceil($num/8);
for ($i=1; $i <= $pages; $i++)
{
$newoffset=8*($i-1);
print "<a href=\"$PHP_SELF?offset=$newoffset\">$i</a> \n"; }
if ($pages!=0 && ($offset/8+1)<$pages)
{
print "<a href=\"$PHP_SELF?offset=".($offset+8)."\">下一页</a> \n";
}
echo "</td></tr></table>"; }
}echo "<br>
<TABLE border=0 cellPadding=0 cellSpacing=0 width=\"90%\" align=center>
<TR>
<TD bgColor=#000000>
<TABLE border=0 cellPadding=4 cellSpacing=1 width=\"100%\">
<TBODY>
<TR bgColor=#FFFFFF>
<TD width=30> </td>
<TD width=120 align=center noWrap><font color=\"#000000\"><B>姓 名</B></font></TD>
<TD align=center noWrap><font color=\"#000000\"><B>公司名称</B></font></TD>
<TD align=center noWrap><font color=\"#000000\"><B>编辑</B></font></TD>
<TD align=center noWrap><font color=\"#000000\"><B>删除</B></font></TD>
</TR>"; get_namecard();echo "
</TBODY>
</TABLE>
</TD>
</TR>
</TABLE>";echo "</td></tr></table>";?>
这个函数怎么使用,写个小例子吧,谢谢!
<?
include("../source.inc/global.php");
print <<<EOT
<link rel="stylesheet" href="../image/style.css">
<table width=75% cellpadding=6 cellspacing=0 border=0 align=center>
<tr><td><table cellpadding="0" cellspacing="5" border="0" bgcolor="#ffffff" width="100%" align="center">
<tr><td colspan="4">
<table cellpadding="4" cellspacing="1" border="0" width="100%">
<tr align="center" valign="middle">
<td background="../image/back.gif"><font face="ms shell dlg,tahoma,tahoma,verdana,arial,helvetica,宋体"><b><a href="namecard.php"><nobr>我的名片夹</nobr></a></b></font></td>
<td background="../image/back.gif"><font face="ms shell dlg,tahoma,tahoma,verdana,arial,helvetica,宋体"><b><a href="namecard.php"><nobr>公司名片夹</nobr></a></b></font></td>
<td background="../image/back.gif"><font face="ms shell dlg,tahoma,tahoma,verdana,arial,helvetica,宋体"><b><a href="addcard.php">添加名片</a></b></font></td>
<td background="../image/back.gif"><font face="ms shell dlg,tahoma,tahoma,verdana,arial,helvetica,宋体"><b><a href="searchcard.php">名片查询</a></b></font></td>
<td background="../image/back.gif"><font face="ms shell dlg,tahoma,tahoma,verdana,arial,helvetica,宋体"><b><a href="cardaction.php?action=delall&recoduser='$loginname'">删除所有名片</a></b></font></td>
</tr>
</table>
</td></tr></table>EOT;//显示名片夹的纪录
function show_card($row)
{
global $num;
$cardid = $row[cardid];
$company = $row[company];
$name = $row[name];
echo "<tr bgcolor=#ffffff>
<td align=center noWrap width=30 height=\"24\"><img src=\"../image/icon.gif\"></td>
<td align=center noWrap width=120 height=\"24\"><font face=verdana><a href=\"viewcard.php?cardid=$cardid\">$name</a></font></td>
<td align=center noWrap width=50%><font face=verdana><a href=\"viewcard.php?cardid=$cardid\">$company</a></font></td>
<td align=center noWrap><font face=verdana><a href=\"namecardaction.php?action=edit&cardid=$cardid\">编辑</a></font></td>
<td align=center noWrap><font face=verdana><a href=\"namecardaction.php?action=delone&cardid=$cardid\">删除</a></font></td>
</tr>";}//得到名片纪录function get_namecard()
{
global $loginname; //用户登录名
global $offset;
connect_db();
$sql = "select cardid,company,name from namecard";//where owner='$loginname' order by cardid desc";
$result = mysql_query($sql) or die(mysql_error());
$num = mysql_num_rows($result) or die(mysql_error());
if($num !=0)
{
if(empty($offset)||$offset<0)
{
$offset = 0;
}
$sql2 = "select cardid,company,name from namecard LIMIT $offset,8";
$result2 = mysql_query($sql2);// or die(mysql_error()); /* for($i=0;$i<$num;$i++)
{
$row = mysql_fetch_array($result2);
show_card($row);
}*/
while ($row = mysql_fetch_array($result2))
{
show_card($row);
}
echo "<tr bgcolor=#ffffff><td align=center colspan=5>";
if($offset>=8) //当$offset大于7,也就是说处在第二页面以上就会显示前一页
{
$preoffset = $offset-8;
echo "<a href=\"$PHP_SELF?offset=$preoffset\">前一页</a> \n";
} $pages = ceil($num/8);
for ($i=1; $i <= $pages; $i++)
{
$newoffset=8*($i-1);
print "<a href=\"$PHP_SELF?offset=$newoffset\">$i</a> \n"; }
if ($pages!=0 && ($offset/8+1)<$pages)
{
print "<a href=\"$PHP_SELF?offset=".($offset+8)."\">下一页</a> \n";
}
echo "</td></tr></table>"; }
}echo "<br>
<TABLE border=0 cellPadding=0 cellSpacing=0 width=\"90%\" align=center>
<TR>
<TD bgColor=#000000>
<TABLE border=0 cellPadding=4 cellSpacing=1 width=\"100%\">
<TBODY>
<TR bgColor=#FFFFFF>
<TD width=30> </td>
<TD width=120 align=center noWrap><font color=\"#000000\"><B>姓 名</B></font></TD>
<TD align=center noWrap><font color=\"#000000\"><B>公司名称</B></font></TD>
<TD align=center noWrap><font color=\"#000000\"><B>编辑</B></font></TD>
<TD align=center noWrap><font color=\"#000000\"><B>删除</B></font></TD>
</TR>"; get_namecard();echo "
</TBODY>
</TABLE>
</TD>
</TR>
</TABLE>";echo "</td></tr></table>";?>
<?php
function Page ($select, $id, $no, $page_count, $dbname, $tablename)
{
// +----------------------------------------------------+
// | 认证$page_count是否小于一 |
if ($page_count < 1) {
print "不能小于一";
exit;
}
// +----------------------------------------------------+
// +----------------------------------------------------+
// | 认证$id是否真 |
if (!$id) {
print '数据库不存在';
exit;
}
// +----------------------------------------------------+ $query = "select count(*) as " . $dbname . " from " . $tablename;
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result);
$rows_count = $row["$dbname"];
//$resrlt = @mysql_query($select); //sql语句执行
//$rows_count = @mysql_num_rows($resrlt); //统计行数
$rows = ceil($rows_count / $page_count); //获取页数
settype($no, 'integer'); //将$page的类型设为integer
$no --; // +----------------------------------------------------+
// | 认证$no是否符合条件 |
if ($no < 0) {
$no = 0;
} else if ($no > $rows - 1) {
//$no = $rows - 1;
print "没有这一页的数据";
exit;
}
// +----------------------------------------------------+ $start = $no * $page_count; //获取从第几行开始读
$reclue = @mysql_query($select." limit ".$start.",".$page_count); //获取数据
if ($no < $rows - 1) {
$no2 = $no + 2; //获取下一页
}
if ($no != 0) {
$no1 = $no; //获取上一页
}
$a['page_count'] = $rows;
$a['pre_page'] = $no1;
$a['down_page'] = $no2;
$a['data_page'] = $reclue;
return $a;
}
?>
这是使用:
<?php
include ('上面的文件');
$select = 'select * from 数据库表的名称 order by id desc';
$id = mysql_connect('localhost', '数据库', '密码');
if ($no) {
$no = $no;
} else {
$no = '2';
}
$page_count = '10';
$dbname = '数据库';
$tablename = '数据库表';
$fields = mysql_list_fields($dbname, $tablename);
$num_fields = mysql_num_fields($fields);
$b = Page ($select, $id, $no, $page_count, $dbname, $tablename);
print "<table width='100%' border='1' cellspacing='0' cellpadding='0'>";
print "<tr>";
for($j=0; $j<$num_fields; $j++) {
$field = @mysql_field_name($fields, $j);
print "<td bgcolor='#c6d7ea'>$field</td>";
}
print "</tr>";
while ($row = @mysql_fetch_object($b['data_page'])) {
print "<tr><td width='5%'>$row->id</td><td width='15%'>$row->title</td><td style='word-break:break-all'>$row->content</td><td width='10%'>$row->username</td><td width='10%'>$row->time</td><tr>";
}
print '</table>';
$print_count = "共" . $b['page_count'] . "页";
if ($b['pre_page'] != '') {
$print_pre = "<a href=no.php?no=" . $b['pre_page'] . ">上一页</a>";
} else {
$print_pre = '上一页';
}
if ($b['down_page'] != '') {
$print_down = "<a href=no.php?no=" . $b['down_page'] . ">下一页</a>";
} else {
$print_down = '下一页';
}
print "<table width='100%' border='0' cellspacing='0' cellpadding='0'>";
print "<tr><td width='34%'>$print_pre</td><td width='34%'><div align='center'>$print_count</div></td><td width='33%'><div align='right'>$print_down</div></td></tr>";
print "</table>";
?>
我一直用着,没有错误
本类没有提供连接数据库的功能,所以需在外部打开相应的数据库
本类也没有提供显示记录的功能,只是分页读取记录至 Result二维数组中
需在外部自定义数据显示格式。 改编说明:
本类代码除了改正了一些Bug外,还增加和改写了许多实用的类方法(函数),
除了增加提供了整合全部功能的showFullFunc类方法外,更改写和增加了其
他的许多代码,增加了类的可重定制能力。 用例:
require("inc/conn.inc"); //调用数据库连接
if !($offset) $offset = 0;
if !($pageline) $pageline = 15; //不要改变这两个变量的名称;
$classtest = new TViewPage("tablename",$pageline,$offset);
$recordSet = $classtest->getRecords();
$classtest->showFullFunc();
$k=count($recordSet);
for($i=0;$i<$k;$i++)
echo $recordSet[$i]["Field_Name"];
......
***********************************************/ class TViewPage { var $Table; //表名
var $MaxLine=15; //每页显示行数 var $Offset; //记录偏移量
var $Total; //记录总数
var $Number; //本页读取的记录数
var $Result; //读出的结果 var $TPages; //总页数
var $CPages; //当前页数 var $Condition; //数据库搜索条件
var $PageQuery; //分页显示要传递的参数 var $strPageFrist = '首页'; //设置导航条翻页提示符
var $strPagePrev = '上页';
var $strPageNext = '下页';
var $strPageLast = '末页'; var $strDispPageFrist; //设置导航条翻页(未激活状态)提示符
var $strDispPagePrev; //如果不设置,导航条将不区别显示激活与否
var $strDispPageNext; //本项配合图形可获得最佳效果
var $strDispPageLast; //******构造函数*************
//参数:表名、最大行数、偏移量 function TViewPage($TB,$ML,$OF){
$this->Table = $TB;
$this->MaxLine = $ML;
$this->Offset = (floor($OF/$ML)) * $ML;
} //********设置显示条件*********
//如:where id="$id" order by id desc
//要求是字串,符合SQL语法(本字串将加在SQL语句后) function setCondition($s){
$this->Condition=$s;
} //******设置传递参数************
// key参数名 value参数值
// 如:setpagequery("id",$id);如有多个参数要传递,可多次调用本函数 function setPageQuery($key,$value){
$tmp[key]=$key;$tmp[value]=$value;
$this->PageQuery[]=$tmp;
} //********设置导航条分页显示的字符或图形*********
//如:首页 或 <IMG SRC=frist.gif align=absmiddle> 等。
//要求是字串,符合HTML语法显示的要求
//如果不设置,导航条使用默认字符 function setPageFrist($s,$sd=""){
$this->strPageFrist=$s;
$this->strDispPageFrist=$sd;
} function setPagePrev($s,$sd=""){
$this->strPagePrev=$s;
$this->strDispPagePrev=$sd;
} function setPageNext($s,$sd=""){
$this->strPageNext=$s;
$this->strDispPageNext=$sd;
} function setPageLast($s,$sd=""){
$this->strPageLast=$s;
$this->strDispPageNext=$sd;
}
//如果单独使用此类方法(函数),请在调用此类方法时配套加入
//<form>元素及提交按钮,Action可以用$PHP_SELF值。
function selectPage() {
if ($this->TPages > 1) {
echo '<select size=1 name=offset>';
for($i=0 ; $i < $this->TPages ; $i++)
if ($this->Offset == ($i) * $this->MaxLine)
echo '<option value='.($i * $this->MaxLine).' selected>第'.($i+1).'页</option>';
else
echo '<option value='.($i * $this->MaxLine).'>第'.($i+1).'页</option>';
echo '</select>';
$k=count($this->PageQuery);
for($i=0;$i<$k;$i++){
echo '<input type=hidden name='.$this->PageQuery[$i][key].' Value='.$this->PageQuery
[$i][value].'>';
}
}
} //**********显示导航按钮或文字*************
//显示首页、下页、上页、未页,并加上要传递的参数 function showNavigator() {
$outstr='';
$first=0;
$next=$this->Offset+$this->MaxLine;
$prev=$this->Offset-$this->MaxLine;
$last=($this->TPages-1)*$this->MaxLine; $k=count($this->PageQuery);
$strQuery="";//生成一个要传递参数字串
for($i=0;$i<$k;$i++){
$strQuery.="&".$this->PageQuery[$i][key]."=".$this->PageQuery[$i][value];
} if($this->Offset>=$this->MaxLine)
$outstr.="<A href=$PHP_SELF?offset=".$first."&pageline=".$this-
>MaxLine.$strQuery.">".$this->strPageFrist."</A>│";
else
$outstr.=$this->strPageFrist."│";
if($prev>=0)
$outstr.="<A href=$PHP_SELF?offset=".$prev."&pageline=".$this-
>MaxLine.$strQuery.">".$this->strPagePrev."</A>│";
else
$outstr.=$this->strPagePrev."│";
if($next<$this->Total)
$outstr.="<A href=$PHP_SELF?offset=".$next."&pageline=".$this-
>MaxLine.$strQuery.">".$this->strPageNext."</A>│";
else
$outstr.=$this->strPageNext."│";
if($this->TPages!=0 && $this->CPages<$this->TPages)
$outstr.="<A href=$PHP_SELF?offset=".$last."&pageline=".$this-
>MaxLine.$strQuery.">".$this->strPageLast."</A>";
else
$outstr.=$this->strPageLast;
return $outstr;
} //**********分页显示的全部功能*************
//提供一个整合的解决方案,没有特殊要求的话直接用它就行了。
function showFullFunc($barBgColor="") {
echo '<table width=100% cellspacing=0 cellpadding=0 border=0>';
echo '<form Action="'.$PHP_SELF.'" method="POST">';
echo '<tr bgcolor="'.$barBgColor.'">';
echo '<td align=left> ';
echo $this->getPages().' ';
echo $this->showNavigator().' ';
echo '共 '.$this->getTotal().' 行';
echo '</td>';
echo '<td width=45% align=right>';
echo '每页';
$this->inputPageLines(1);
echo '行';
if ($this->TPages > 1) echo ' / 直接跳转到';
$this->selectPage();
echo ' <input type=Submit value="Reset">';
echo ' </td>';
echo '</tr></form></table>';
}
//******end class
}
?>