百度ページ関数の実現
28491 ワード
$recordcount=$rs[0];//合計14件の記録
$pagesize=1;//1ページあたり2レコード
$pagecount=ceil($recordcount/$pagesize);//全部で何ページありますか.
$currpage=isset($_GET['p']) ? $_GET['p']:1;//現在のページを表示
呼び出し関数
$pagesize=1;//1ページあたり2レコード
$pagecount=ceil($recordcount/$pagesize);//全部で何ページありますか.
$currpage=isset($_GET['p']) ? $_GET['p']:1;//現在のページを表示
呼び出し関数
<?php include 'inc/db_mysqli.php';?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>webrx-php</title>
<?php css1();?> //
</head>
<body>
<?php
$p = isset($_GET['p']) ? $_GET['p'] : 1;
- //( ,2 ,mname ,$p )
$rs = pager('member',$p,'mname,maccount',2);
$m->close();
echo $rs[1];
foreach($rs[0] as $v){
echo $v[0].'<br>';
}
echo $rs[1];
?>
</body>
</html>
<?php
$host = 'localhost';
$user = 'root';
$pass = '';
$dbname = 'db';
$charset = 'utf8';
$m = new mysqli($host,$user,$pass,$dbname);
$m->set_charset($charset);
function pager($tn,$currpage=1,$f='*',$pagesize=3,$w='1=1'){
global $m;
$stmt = $m->prepare("select count(*) from $tn where $w");
$stmt->execute();
$stmt->bind_result($recordcount);
$stmt->fetch();
$stmt->free_result();
$stmt->close();
$stmt = $m->prepare("select $f from $tn where $w limit ?,?");
$pagecount = ceil($recordcount/$pagesize);
$start = $currpage*$pagesize - $pagesize;
$stmt->bind_param('ii',$start,$pagesize);
$stmt->execute();
$result = $stmt->get_result();
$row = array();
$row[] = $result->fetch_all( MYSQLI_NUM);
$stmt->free_result();
$stmt->close();
$first = 1;
$end = 10;
$pages = '<div class="page">';
if($currpage>=7){
$first = $currpage-5;
$end = $first+$end-1;
}
if($currpage>1){
$prev = $currpage-1;
if($first>1){
$pages.="<a href=?p=1> </a><a href=?p=$prev> </a>";
}else{
$pages.="<a href=?p=$prev> </a>";
}
}
for($i=$first;$i<=$end;$i++){
if($i>$pagecount){
break;
}
if($i==$currpage){
$pages.='<a class="checked">'.$i.'</a>';
continue;
}
$pages.="<a href=?p=$i>$i</a>";
}
if($currpage<$pagecount){
$next = $currpage+1;
$pages.="<a href=?p=$next> </a>";
}
if($end<$pagecount){
$pages.="<a href=?p=$pagecount> </a>";
}
$row[] = $pages.'</div>';
$row[] = $pagesize;
$row[] = $pagecount;
$row[] = $recordcount;
$row[] = $currpage;
return $row;
}
function css1(){
$css = <<<css
<style>
.page{font-size:12px;height:30px;padding:15px 0;clear:both;overflow:hidden;text-align:center;}
.page a{text-decoration:none;line-height:25px;padding:0px 10px;display:inline-block;margin-right:5px;border:solid 1px #c8c7c7;}
.page a:hover,.page a.checked{text-decoration:none;border:solid 1px #0086d6;background:#0091e3;color:#fff;}
.page a:visited,.page a:link{color:#333;}
.page a:active{color:#3B3B3B;}
</style>
css;
echo $css;
}