Php应用实例--分页的技巧

发表于:2007-06-21来源:作者:点击数: 标签:
一,三个函数,一个观念:函数都有输入输出。 1,获取资料表的函数GetRows(): ? function GetRows( $Database, $Query ) { mysql _connect ("ftp", "root", ""); $db = mysql_select_db( $Database ); if ( ! $db ) { echo "礚猭秨币 kjsql 戈?畐?叫絋﹚

   
  

一,三个函数,一个观念:函数都有输入输出。
1,获取资料表的函数GetRows():
<?
function GetRows( $Database, $Query ) {
   mysql_connect ("ftp", "root", "");
   $db = mysql_select_db( $Database );

   if ( ! $db ) {
      echo "礚猭秨币 kjsql 戈?畐?叫絋﹚眤?竒?ミ kjsql 戈?畐!";
      exit;
   }

   $rows = mysql_query( $Query );
   if ( ! $rows ) {
      echo "SQL ??岿粇?礚猭匡?戈?!";
      exit;
   }
   return $rows;
}
?>
$rows是函数的输出,作为存取资料的依据。
2,连结数据库的函数ConnectDatabase():
<?php
 function ConnectDatabase( $Database ) {
   mysql_connect ("ftp", "root", "");
   $db = mysql_select_db( $Database );
   if ( ! $db ) {
      echo "礚猭秨币 kjsql 戈?畐?叫絋﹚眤?竒?ミ kjsql 戈?畐!";
      exit;
   }
}
?>
3,显示一页的函数ShowOnePage():
<?
function ShowOnePage( $rows, $No ) {
  mysql_data_seek( $rows, $No );

  echo "<TABLE Border=1 Align=Center>";
  echo "<TR BgColor=Cyan><TD>No.</TD>";
  while ( $field = mysql_fetch_field($rows) ) {
     echo "<TD>" . $field->name . "</TD>";
  }
  echo "</TR>";

  for ( $I=1; $I <= 10; $I++ ) {
     if ( $row = mysql_fetch_row($rows) ) {
        echo "<TR><TD Align=Right>" . ($No + $I) . "</TD>";
        for ( $K=0 ; $K < count($row); $K++ ) {
           echo "<TD Align=Right>" . $row[$K] . "</TD>";
        }
        echo "</TR>";
     }
  }
  echo "</TABLE>";
}
?>
输出为一张表格。

二,分页的技巧
Version1,只显示1到10条记录:
<?
function ShowOnePage( $rows, $No ) {
  mysql_data_seek( $rows, $No );

  echo "<TABLE Border=1 Align=Center>";
  echo "<TR BgColor=Cyan><TD>No.</TD>";
  while ( $field = mysql_fetch_field($rows) ) {
     echo "<TD>" . $field->name . "</TD>";
  }
  echo "</TR>";

  for ( $I=1; $I <= 10; $I++ ) {
     if ( $row = mysql_fetch_row($rows) ) {
        echo "<TR><TD Align=Right>" . ($No + $I) . "</TD>";
        for ( $K=0 ; $K < count($row); $K++ ) {
           echo "<TD Align=Right>" . $row[$K] . "</TD>";
        }
        echo "</TR>";
     }
  }
  echo "</TABLE>";
}
?>

<HTML>
<BODY bgcolor="#FFFFFF">
<H2 ALIGN=CENTER>pagev1.php3, Version 1 - だ??мォ<HR></H2>
<?
  include("../db.func");
  $rows = GetRows( "kjsql", "Select * From stock" );

  ShowOnePage( $rows, 0 );
?>
<HR>
</BODY></HTML>

相关说明:1,$No参数,由于显示某一页,利用此参数指定该页的第一笔资料;
     2,设定目前资料录的位子:mysql_data_seek($rows,$No);
          3,判断是否还有资料录:if ($row=mysql_fetch_row($rows)).

Version2,可以在IE位置栏输入页次:
<?
function ShowOnePage( $rows, $No ) {
  mysql_data_seek( $rows, $No );

  echo "<TABLE Border=1 Align=Center>";
  echo "<TR BgColor=Cyan><TD>No.</TD>";
  while ( $field = mysql_fetch_field($rows) ) {
     echo "<TD>" . $field->name . "</TD>";
  }
  echo "</TR>";

  for ( $I=1; $I <= 10; $I++ ) {
     if ( $row = mysql_fetch_row($rows) ) {
        echo "<TR><TD Align=Right>" . ($No + $I) . "</TD>";
        for ( $K=0 ; $K < count($row); $K++ ) {
           echo "<TD Align=Right>" . $row[$K] . "</TD>";
        }
        echo "</TR>";
     }
  }
  echo "</TABLE>";
}
?>

<HTML>
<BODY bgcolor="#FFFFFF">
<H2 ALIGN=CENTER>pagev2.php3, Version 2 - だ??мォ<HR></H2>
<?
  include("../db.func");
  $rows = GetRows( "kjsql", "Select * From stock" );

  $num = mysql_num_rows($rows);
  $Pages = intval(($num - 1) / 10) + 1;

  if ( $Page < 1 ) $Page = 1;
  if ( $Page > $Pages ) $Page = $Pages;

  ShowOnePage( $rows, ($Page-1)*10 );
?>
<HR>
<DIV Align=right>
?Ω:<Font color=Red><?echo $Page;?>/<?echo $Pages;?>
</DIV>
</BODY></HTML>

Version3,页次的浏览:
<?
function ShowOnePage( $rows, $No ) {
  mysql_data_seek( $rows, $No );

  echo "<TABLE Border=1 Align=Center>";
  echo "<TR BgColor=Cyan><TD>No.</TD>";
  while ( $field = mysql_fetch_field($rows) ) {
     echo "<TD>" . $field->name . "</TD>";
  }
  echo "</TR>";

  for ( $I=1; $I <= 10; $I++ ) {
     if ( $row = mysql_fetch_row($rows) ) {
        echo "<TR><TD Align=Right>" . ($No + $I) . "</TD>";
        for ( $K=0 ; $K < count($row); $K++ ) {
           echo "<TD Align=Right>" . $row[$K] . "</TD>";
        }
        echo "</TR>";
     }
  }
  echo "</TABLE>";
}
?>

<HTML>
<BODY bgcolor="#FFFFFF">
<H2 ALIGN=CENTER>pagev3.php3, Version 3 - ?Ω?聅凝<HR></H2>
<?
  include("../db.func");
  $rows = GetRows( "kjsql", "Select * From stock" );

  $num = mysql_num_rows($rows);
  $Pages = intval(($num - 1) / 10) + 1;

  if ( $Page < 1 ) $Page = 1;
  if ( $Page > $Pages ) $Page = $Pages;

  ShowOnePage( $rows, ($Page-1)*10 );
?>
<HR>
<DIV Align=right>
<?
if ( $Page <> 1 ) {  // ?狦ぃ琌??材??
   echo "<A HREF=$PHP_SELF?Page=1>材??</A>?";
   echo "<A HREF=$PHP_SELF?Page=" . ($Page-1) . ">???</A>?";
}

if ( $Page <> $Pages ) {  // ?狦ぃ琌??程???
   echo "<A HREF=$PHP_SELF?Page=" . ($Page+1) . ">???</A>?";
   echo "<A HREF=$PHP_SELF?Page=$Pages>程???</A>?";
}
?>
?Ω:<Font color=Red><?echo $Page;?>/<?echo $Pages;?>
</DIV>
</BODY></HTML>

Version 4,完成版:
<?
function ShowOnePage( $rows, $No ) {
  mysql_data_seek( $rows, $No );

  echo "<TABLE Border=1 Align=Center>";
  echo "<TR BgColor=Cyan><TD>No.</TD>";
  while ( $field = mysql_fetch_field($rows) ) {
     echo "<TD>" . $field->name . "</TD>";
  }
  echo "</TR>";

  for ( $I=1; $I <= 10; $I++ ) {
     if ( $row = mysql_fetch_row($rows) ) {
        echo "<TR><TD Align=Right>" . ($No + $I) . "</TD>";
        for ( $K=0 ; $K < count($row); $K++ ) {
           echo "<TD Align=Right>" . $row[$K] . "</TD>";
        }
        echo "</TR>";
     }
  }
  echo "</TABLE>";
}
?>

<HTML>
<BODY bgcolor="#FFFFFF">
<H2 ALIGN=CENTER>pagev4.php3, だ?聅凝(ЧΘ?)<HR></H2>
<?
  include("../db.func");
  $rows = GetRows( "kjsql", "Select * From stock" );

  $num = mysql_num_rows($rows);
  $Pages = intval(($num - 1) / 10) + 1;

  if ( $Page < 1 ) $Page = 1;
  if ( $Page > $Pages ) $Page = $Pages;

  ShowOnePage( $rows, ($Page-1)*10 );
?>
<HR>
<FORM Action=<?echo $PHP_SELF;?> Method=GET>
<DIV Align=right>
<?
if ( $Page <> 1 ) {  // ?狦ぃ琌??材??
   echo "<A HREF=$PHP_SELF?Page=1>材??</A>?";
   echo "<A HREF=$PHP_SELF?Page=" . ($Page-1) . ">???</A>?";
}

if ( $Page <> $Pages ) {  // ?狦ぃ琌??程???
   echo "<A HREF=$PHP_SELF?Page=" . ($Page+1) . ">???</A>?";
   echo "<A HREF=$PHP_SELF?Page=$Pages>程???</A>?";
}
?>
块??Ω:<INPUT TYPE=TEXT Name=Page SIZE=3>
?Ω:<Font color=Red><?echo $Page;?>/<?echo $Pages;?>
</DIV>
</FORM>
</BODY></HTML>

原文转自:http://www.ltesting.net