Chinaunix首页 | 论坛 | 博客
  • 博客访问: 503482
  • 博文数量: 130
  • 博客积分: 3581
  • 博客等级: 中校
  • 技术积分: 1200
  • 用 户 组: 普通用户
  • 注册时间: 2005-02-18 10:51
文章分类

全部博文(130)

文章存档

2016年(1)

2015年(8)

2014年(6)

2013年(2)

2012年(9)

2011年(16)

2010年(5)

2009年(4)

2008年(2)

2007年(6)

2006年(50)

2005年(21)

我的朋友

分类:

2005-02-18 12:26:26

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

";
  echo "
";
  while ( $field = mysql_fetch_field($rows) ) {
     echo "
";
  }
  echo "
";

  for ( $I=1; $I <= 10; $I++ ) {
     if ( $row = mysql_fetch_row($rows) ) {
        echo "

";
        for ( $K=0 ; $K < count($row); $K++ ) {
           echo "
";
        }
        echo "
";
     }
  }
  echo "
No." . $field->name . "
" . ($No + $I) . "" . $row[$K] . "
";
}
?>
输出为一张表格。

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

  echo "

";
  echo "
";
  while ( $field = mysql_fetch_field($rows) ) {
     echo "
";
  }
  echo "
";

  for ( $I=1; $I <= 10; $I++ ) {
     if ( $row = mysql_fetch_row($rows) ) {
        echo "

";
        for ( $K=0 ; $K < count($row); $K++ ) {
           echo "
";
        }
        echo "
";
     }
  }
  echo "
No." . $field->name . "
" . ($No + $I) . "" . $row[$K] . "
";
}
?>



pagev1.php3, Version 1 - だ??мォ


  include("../db.func");
  $rows = GetRows( "kjsql", "Select * From stock" );

  ShowOnePage( $rows, 0 );
?>



相关说明: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 "

";
  echo "
";
  while ( $field = mysql_fetch_field($rows) ) {
     echo "
";
  }
  echo "
";

  for ( $I=1; $I <= 10; $I++ ) {
     if ( $row = mysql_fetch_row($rows) ) {
        echo "

";
        for ( $K=0 ; $K < count($row); $K++ ) {
           echo "
";
        }
        echo "
";
     }
  }
  echo "
No." . $field->name . "
" . ($No + $I) . "" . $row[$K] . "
";
}
?>



pagev2.php3, Version 2 - だ??мォ


  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 );
?>




?Ω:/


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

  echo "

";
  echo "
";
  while ( $field = mysql_fetch_field($rows) ) {
     echo "
";
  }
  echo "
";

  for ( $I=1; $I <= 10; $I++ ) {
     if ( $row = mysql_fetch_row($rows) ) {
        echo "

";
        for ( $K=0 ; $K < count($row); $K++ ) {
           echo "
";
        }
        echo "
";
     }
  }
  echo "
No." . $field->name . "
" . ($No + $I) . "" . $row[$K] . "
";
}
?>



pagev3.php3, Version 3 - ?Ω?聅凝


  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 );
?>




if ( $Page <> 1 ) {  // ?狦ぃ琌??材??
   echo "材???";
   echo "????";
}

if ( $Page <> $Pages ) {  // ?狦ぃ琌??程???
   echo "????";
   echo "程????";
}
?>
?Ω:/



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

  echo "

";
  echo "
";
  while ( $field = mysql_fetch_field($rows) ) {
     echo "
";
  }
  echo "
";

  for ( $I=1; $I <= 10; $I++ ) {
     if ( $row = mysql_fetch_row($rows) ) {
        echo "

";
        for ( $K=0 ; $K < count($row); $K++ ) {
           echo "
";
        }
        echo "
";
     }
  }
  echo "
No." . $field->name . "
" . ($No + $I) . "" . $row[$K] . "
";
}
?>



pagev4.php3, だ?聅凝(ЧΘ?)


  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 );
?>



Method=GET>

if ( $Page <> 1 ) {  // ?狦ぃ琌??材??
   echo "材???";
   echo "????";
}

if ( $Page <> $Pages ) {  // ?狦ぃ琌??程???
   echo "????";
   echo "程????";
}
?>
块??Ω:
?Ω:/



作者:(台湾)王国荣

阅读(1120) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~