首页 > 开发 > PHP > 正文

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

2024-05-04 22:57:23
字体:
来源:转载
供稿:网友

一,三个函数,一个观念:函数都有输入输出。
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>

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表