페이징 처리 질문입니다.

페이징 처리 질문입니다.

QA

페이징 처리 질문입니다.

본문

<?php

//데이터 베이스 연결하기

include "db_info.php";

 

//LIST 설정

//1. 한 페이지에 보여질 게시물의 수

$page_size = 10;

 

//2. 페이지 나누기에 표시될 페이지의 수

$page_list_size = 10;

 

$no = $_GET['no'];

$PHP_SELF = $_SERVER['PHP_SELF'];

 

if( !$no || $no < 0 ){

$no = 1;

}

 

//데이터베이스에서 페이지의 첫 번째 글($no)부터

//$page_size 만큼의 글을 가져온다.

$query = "select * from board order by id desc limit $no, $page_size";

$result = mysqli_query($conn, $query);

 

//총 게시물 수를 구한다.

$result_count = mysqli_query($conn, "select count(*) from board");

$result_row = mysqli_fetch_row($result_count);

$total_row = $result_row[0];

//결과의 첫 번째 열이 count(*)의 결과다.

 

//총 페이지 계산

if($total_row <= 0){

$total_row = 0;

}

$total_page = ceil($total_row / $page_size);

 

echo "total_page -> "."(".$total_page.")";

 

//현재 페이지 계산

$current_page = ceil(($no+1)/$page_size);

 

echo "현재페이지 :".$current_page; //1

?>

<html>

<head>

<title>초 허접 게시판</title>

<style>

<!--

td {font-size : 9pt;}

A:link {font : 9pt; color : black; text-decoration : none; fontfamily : 굴림; font-size : 9pt;}

A:visited {text-decoration : none; color : black; font-size : 9pt;}

A:hover {text-decoration : underline; color : black; fon-size : 9pt;}

-->

</style>

</head>

<body topmargin="0" leftmargin="0" text="#464646">

<center>

<br />

<!-- 게시판 타이틀 -->

<font size="2">자~ 버그를 찾읍시다~ 버그를~~</font>

<br />

<br />

<!-- 게시물 리스트를 보이기 위한 테이블 -->

<table width="580" border="0" cellpadding="2" cellspacing="1" bgcolor="#777777">

<!-- 리스트 타이틀 부분 -->

<tr height="20" bgcolor="#999999">

<td width="30" align="center">

<font color="white">번호</font>

</td>

<td width="370" align="center">

<font color = "white">제목</font>

</td>

<td width="50" align="center">

<font color = "white">글쓴이</font>

</td>

<td width="60" align="center">

<font color="white">날 짜</font>

</td>

<td width="40" align="center">

<font color="white">조회수</font>

</td>

</tr>

<!-- 리스트 타이틀 끝 -->

<!-- 리스트 부분 시작 -->

<?php

while($row = mysqli_fetch_array($result))

{

?>

<!-- 행 시작 -->

<tr>

<!-- 번호 -->

<td height="20" bgcolor="white" align="center">

<a href="read.php?id=<?=$row[id] ?>&no=<?=$no?>"><?=$row['id']?></a>

</td>

<!-- 번호 끝 -->

<!-- 제목 -->

<td height="20" bgcolor="white"> 

<a href="read.php?id=<?=$row['id']?>&no=<?=$no?>">

<?=strip_tags($row['title'], '<b><i>');?></a>

</td>

<!-- 제목 끝 -->

<!-- 이름 -->

<td align="center" height="20" bgcolor="white">

<font color="black">

<a href="mailto:<?=$row['email']?>"><?=$row['name']?></a>

</font>

</td>

<!-- 이름 끝 -->

<!-- 날짜 -->

<td align="center" height="20" bgcolor="white">

<font color="black"><?=$row['wdate']?></font>

</td>

<!-- 날짜 끝 -->

<!-- 조회수 -->

<td align="center" height="20" bgcolor="white">

<font color="black"><?=$row['view']?></font>

</td>

<!-- 조회수 끝 -->

</tr>

<!-- 행 끝 -->

<?php

}// end while

//데이터베이스와의 견결을 끊는다.

mysqli_close($conn);

?>

</table>

<!-- 게시물 리스트를 보이기 위한 테이블 끝 -->

<!-- 페이지를 표시하기 위한 테이블 -->

<table border="0">

<tr>

<td width="600" height="20" align="center" rowspan="4">

<font color="gray">

 

<?php

$start_page = floor(($current_page - 1) / $page_list_size) * $page_list_size + 1;

 

echo "start_page -> "."(".$start_page.")";

 

// 페이지 리스트의 마지막 페이지가 몇 번째 페이지인지 구하는 부분이다.

$end_page = $start_page + $page_list_size -1;

 

echo "end_page -> "."(".$end_page.")";

 

//echo "total_page -> "."(".$total_page.")";

 

if ($total_page < $end_page){

$end_page = $total_page;

}

if ($start_page >= $page_list_size) {

# 이전 페이지 리스트값은 첫 번째 페이지에서 한 페이지 감소하면 된다.

# $page_size 를 곱해주는 이유는 글번호로 표시하기 위해서이다.

 

$prev_list = ($start_page - 2)*$page_size;

 

echo "<a href=\"$PHP_SELF?no=0\">처음</a>";  

echo "<a href=\"$PHP_SELF?no=$prev_list\">◀</a>";

}

//페이지 리스트 출력

for($i =$start_page; $i <= $end_page; $i++){

$page = ($i-1) * $page_size; //페이지값을 no 값으로 변환

if($no != $page){

echo "<a href=\"$PHP_SELF?no=$page\">";

}

echo " $i "; //페이지를 표시

 

if($no != $page){

echo "</a>";

}

}

//다음 페이지 리스트가 필요할때는 총 페이지가 마지막 리스트보다 클 때이다.

//리스트를 다 뿌리고도 더 뿌려줄 페이지가 남았을 때 다음 버튼이 필요할 것이다.

if($total_page > $end_page)

{

$next_list = $end_page * $page_size;

echo "next_list "."(".$next_list.")";

 

echo "<a href=\"$PHP_SELF?no=$next_list\">▶</a>";

}

?>

</font>

</td>

</tr>

</table>

<a href="write.php">글쓰기</a>

</center>

</body>

</html>

 

----------------------------------------------------

echo "<a href=\"$PHP_SELF?no=0\">처음</a>"; 

위와 같이 소스코드를 작성해서 처음을 클릭하면 페이징 처리의 첫번째 1번 페이지로 가게 했습니다.

 

문제는 페이지가 25개 있을때 "마지막"을 클릭하면 바로 25번째 페이지로 이동하는것을 구현을 못하고 있습니다. 알려 주시면 정말 감사 하겠습니다.

이 질문에 댓글 쓰기 :

답변 3


$list_cnt = 10; 
$max_data = sql_fetch("select count(*) as max_cnt from board  ".$whereStr);
$rec_max = $max_data['max_cnt'];
if ($page < 1) $page = 1;
$total_page = (int)($rec_max/$list_cnt) + ($rec_max%$list_cnt==0 ? 0 : 1);
$page_start = $list_cnt * ( $page - 1 ); 
 
$write_pages = get_paging(10, $page, $total_page, $PHP_SELF.'?page='.$page); 

 

그리고 페이지가 출력되는 곳에 $write_pages를 사용하시면 되시지요 

답변을 작성하시기 전에 로그인 해주세요.
전체 21
QA 내용 검색

회원로그인

(주)에스아이알소프트 / 대표:홍석명 / (06211) 서울특별시 강남구 역삼동 707-34 한신인터밸리24 서관 1402호 / E-Mail: admin@sir.kr
사업자등록번호: 217-81-36347 / 통신판매업신고번호:2014-서울강남-02098호 / 개인정보보호책임자:김민섭(minsup@sir.kr)
© SIRSOFT