(PHP) 페이지 매김

// Gnuboard 기반이므로 DB 관련 코드 수정이 필요할 수 있습니다.

// GNU 게시판의 페이지 기능을 사용할 수 있으나 게시판이 아닌 경우 사용이 복잡하여 아래 코드를 사용합니다.

$tbname="g5_company_list";
$n_limit = 15;
$pg = $_GET('pg');
$bo_table = $_GET('bo_table');
$stx = $_GET('stx');

$qstr = "bo_table=".$_GET('bo_table');
if($_GET('stx')){ $qstr .= "&stx=".$_GET('stx'); }
$qstr .= "&pg=";

$sql_where="";
if($stx){ $sql_where .= "where comp_name like '%{$stx}%' "; }

$sql_order = "order by comp_name ";

$sql_cnt = "select count(*) as cnt from {$tbname} ";
$row_cnt = sql_fetch($sql_cnt.$sql_where);
$total_count = $row_cnt('cnt');
$n_page = ceil($total_count / $n_limit);
if($pg=="") $pg = 1;
$n_from = ($pg - 1) * $n_limit;

$sql = "select * from {$tbname} ";
$sql .= $sql_where;
$sql .= $sql_order;
$sql .= "limit ".$n_from.", ".$n_limit;
$result = sql_query($sql);
$cnt = sql_num_rows($result);

if($n_page>1) {
  echo page_listing($pg, $n_page, $_SERVER('PHP_SELF')."?".$qstr);
}

function page_listing($cur_page, $total_page, $url, $link_id="") {
    $retValue="<div class="pg_wrap">";

    if($cur_page > 1) {
        if($cur_page > 5){ $retValue .= '<a href="'.$url.'1'.$link_id.'"><div class="pg_page pg_page2 pg_start"></div></a>'; }
        $retValue .= '<a href="'.$url.($cur_page-1).$link_id.'"><div class="pg_page pg_page2 pg_prev"></div></a>';
    }

    $start_page = ( ( (int)( ($cur_page - 1 ) / 5 ) ) * 5 ) + 1;
    $end_page = $start_page + 5;
    if($end_page >= $total_page){ $end_page = $total_page; }

    if($total_page > 1){
        for ($k=$start_page;$k<=$end_page;$k++){
            if($cur_page !
= $k){ $retValue .= '<a href="'.$url.$k.$link_id.'"><div class="pg_page pg_page2">'.$k.'</div></a>'; }else{ // 현재 페이지일 때 $retValue .= '<a href="'.$url.$k.$link_id.'"><div class="pg_page pg_page2 pg_current">'.$k.'</div></a>'; } } } if($cur_page < $total_page && $total_page > 1) { $retValue .= '<a href="'.$url.($cur_page+1).$link_id.'"><div class="pg_page pg_page2 pg_next"></div></a>'; if($cur_page<$total_page-5){ $retValue .= '<a href="'.$url.$total_page.$link_id.'"><div class="pg_page pg_page2 pg_end"></div></a>'; } } $retValue .= "</div>"; return $retValue; }