<?php

set_time_limit(0);
error_reporting(E_ALL);

?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="ko" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>페이징 예제</title>
<style type="text/css">
* { font-family: 굴림체; font-size: 12px; line-height: 120%; }
#pageindex a:link, #pageindex a:visited, #pageindex a:active { color: #4176a6; text-decoration: none; padding: 5px; }
#pageindex a:hover { border-style: solid; border-width: 0 1px 0 1px; border-color: #ccc; background-color: #eee; }
#pageindex .pi_c { color: #f00; font-weight: bold; }
</style>
</head>
<body>
<?php

$total_memo = 1237; // 전체 게시물 수
$memo_limit = 20; // 한 페이지에 보여줄 게시물 수
$page_limit = 15; // 한번에 보여줄 페이지 수

$total_page = ceil($total_memo/$memo_limit); // 전체 페이지 수
if ( !$total_page ) $total_page = 1;

$page = isset($_GET['page']) && is_numeric($_GET['page']) && $_GET['page'] && $_GET['page'] <= $total_page ? $_GET['page'] : 1; // 현재 페이지

$page_start = (ceil($page/$page_limit)-1)*$page_limit+1; // 페이지 시작 번호
$page_end = $page_start + ($page_limit-1); // 페이지 끝 번호
if ( $page_end > $total_page ) $page_end = $total_page;

for ($query='',$i=0,$s=count($_GET),$ks=array_keys($_GET);$i<$s;$i++) if ( $ks[$i]!='page' ) $query.=($i?'&':'').$ks[$i].'='.$_GET[$ks[$i]];
for ($page_index='',$i=$page_start;$i<=$page_end;$i++) $page_index.= '<a href=\'?'.$query.'&page='.$i.'\'>'.($i==$page?'<span class=\'pi_c\'>'.$i.'</span>':$i).'</a>';

if ( $page!=1 ) {
	$page_index = '<a href=\'?'.$query.'&page='.($page-1).'\'>◁</a>'.$page_index;
	if ( $page_start-$page_limit>0 ) {
		$page_index = '<a href=\'?'.$query.'&page='.($page_start-$page_limit).'\'>◀</a>'.$page_index;
		$page_index = '<a href=\'?'.$query.'&page=1\'>|◀</a>'.$page_index;
	}
}
if ( $page!=$total_page ) {
	$page_index.= '<a href=\'?'.$query.'&page='.($page+1).'\'>▷</a>';
	if ( $page_start+$page_limit<=$total_page ) {
		$page_index.= '<a href=\'?'.$query.'&page='.($page_start+$page_limit).'\'>▶</a>';
		$page_index.= '<a href=\'?'.$query.'&page='.$total_page.'\'>▶|</a>';
	}
}

echo <<<EOD
<xmp>
전체 게시물 수(total_memo)               : {$total_memo}
한 페이지에 보여줄 게시물 수(memo_limit) : {$memo_limit}
한번에 보여줄 페이지 수(page_limit)      : {$page_limit}
전체 페이지 수(total_page)               : ceil({$total_memo}/{$memo_limit}) = {$total_page}
현재 페이지 번호(page)                   : {$page}
페이지 범위(page_start ~ page_end)       : {$page_start} ~ {$page_end}
query (page 제외)                        : {$query}

|◀, ▶| : 첫 페이지, 마지막 페이지
 ◀, ▶  : 이전 범위, 다음 범위
 ◁, ▷  : 전 페이지, 다음 페이지
</xmp>
<div id="pageindex">{$page_index}</div>

EOD;
unset($page_index);

?>