php 로 게시판 구현 중입니다.
페이징 구현을 위해서 http://blog.kurien.co.kr/529 해당 블로그를 참고하여 작업 중에 있습니다.
설명이 잘 되어있는 블로그이나...표시되는 게시글 수 적용이 안되어 이렇게 질문글을 남기게 되었습니다.
전체 총 13개의 게시글이 있고 5개의 게시글을 보여주게 끔 설정하였지만 13개 전체 게시글이 보입니다....
다소 길지만 도움 주시면 정말 감사하겠습니다....!
<?php
require_once("../lib/MYDB.php");
$pdo = db_connect();
if(isset($_REQUEST["mode"]))
$mode=$_REQUEST["mode"];
else
$mode="";
if(isset($_REQUEST["search"])) // search 쿼리스트링 값 할당 체크
$search=$_REQUEST["search"];
else
$search="";
if(isset($_REQUEST["find"]))
$find=$_REQUEST["find"];
else
$find="";
if($mode=="search"){
if(!$search){
?>
<script>
alert('검색할 단어를 입력해 주세요!');
history.back();
</script>
<?php
}//if end
$sql="select * from cccoda.notice where $find like '%$search%' order by num desc";
} else {
$sql="select * from cccoda.notice order by num desc";
}
try {
$stmh = $pdo->query($sql);
$count=$stmh->rowCount();
/* 페이징 시작 */
//페이지 get 변수가 있다면 받아오고, 없다면 1페이지를 보여준다.
if(isset($_GET['page'])) {
$page = $_GET['page'];
} else {
$page = 1;
}
$sql = 'select count(*) as cnt from cccoda.notice order by num desc';
$result = $pdo->query($sql);
// $row = $result->fetch_assoc();
$row = $result->fetch(PDO::FETCH_ASSOC);
$allPost = $row['cnt']; //전체 게시글의 수
$onePage = 5; // 한 페이지에 보여줄 게시글의 수.
$allPage = ceil($allPost / $onePage); //전체 페이지의 수
if($page < 1 || ($allPage && $page > $allPage)) {
?>
<script>
alert("존재하지 않는 페이지입니다.");
history.back();
</script>
<?php
exit;
}
$oneSection = 5; //한번에 보여줄 총 페이지넘버 개수(1 ~ 10, 11 ~ 20 ...)
$currentSection = ceil($page / $oneSection); //현재 섹션
$allSection = ceil($allPage / $oneSection); //전체 섹션의 수
$firstPage = ($currentSection * $oneSection) - ($oneSection - 1); //현재 섹션의 처음 페이지
if($currentSection == $allSection) {
$lastPage = $allPage; //현재 섹션이 마지막 섹션이라면 $allPage가 마지막 페이지가 된다.
} else {
$lastPage = $currentSection * $oneSection; //현재 섹션의 마지막 페이지
}
$prevPage = (($currentSection - 1) * $oneSection); //이전 페이지, 11~20일 때 이전을 누르면 10 페이지로 이동.
$nextPage = (($currentSection + 1) * $oneSection) - ($oneSection - 1); //다음 페이지, 11~20일 때 다음을 누르면 21 페이지로 이동.
$paging = '<div class="page_list">'; // 페이징을 저장할 변수
//첫 페이지가 아니라면 처음 버튼을 생성
if($page != 1) {
$paging .= '<span class="page_start"><a href="./list.php?page=1"><<</a></span>'; //처음
}
//첫 섹션이 아니라면 이전 버튼을 생성
if($currentSection != 1) {
$paging .= '<span class="page_prev"><a href="./list.php?page=' . $prevPage . '"><</a></span>'; //이전
}
for($i = $firstPage; $i <= $lastPage; $i++) {
if($i == $page) {
$paging .= '<span class="current">' . $i . '</span>';
} else {
$paging .= '<span><a href="./list.php?page=' . $i . '">' . $i . '</a></span>';
}
}
//마지막 섹션이 아니라면 다음 버튼을 생성
if($currentSection != $allSection) {
$paging .= '<span class="page_next"><a href="./list.php?page=' . $nextPage . '">></a></span>'; //다음
}
//마지막 페이지가 아니라면 끝 버튼을 생성
if($page != $allPage) {
$paging .= '<span class="page_end"><a href="./list.php?page=' . $allPage . '">>></a></span>'; //끝
}
$paging .= '</ul>';
/* 페이징 끝 */
$currentLimit = ($onePage * $page) - $onePage; //몇 번째의 글부터 가져오는지
$sqlLimit = ' limit ' . $currentLimit . ', ' . $onePage; //limit sql 구문
$sql = 'select * from cccoda.notice order by num desc' . $sqlLimit; //원하는 개수만큼 가져온다. (0번째부터 20번째까지
$result = $pdo->query($sql);
?>
<body>
<!-- nav -->
<div id="localMenu">
<div class="inner">
<?php include "../lib/top_login2.php"; ?>
</div>
</div>
<!-- header -->
<header id="mainHeader">
<?php include "../lib/top_menu2.php"; ?>
</header>
<!-- sub_wrap -->
<div id="sub_wrap" class="cf">
<!-- sub_tit -->
<div id="sub_title">
<div class="s_title"><h2>공지사항</h2></div>
</div><!-- // sub_tit -->
<div id="sub_main">
<div id="board">
<h2>공지사항</h2>
<span>▷ 총 <?= $count ?> 개의 게시물이 있습니다.</span>
<form name="board_form" method="post" action="list.php?mode=search">
<div id="search_box">
<ul class="cf">
<li class="search_option fl">
<select name="find" id="search_option">
<option value='subject'>제목</option>
<option value='content'>내용</option>
<option value='nick'>작성자</option>
<option value='name'>이름</option>
</select>
</li>
<li class="search_text fl"><input type="text" name="search" class="guide_text" id="search_text" placeholder="검색어를 입력해주세요"/></li>
<li class="search_btn fl"><input type="submit" id="search_btn" value="검색"/></li>
<li class="col_option fr">
<select name="col_option" id="col_option">
<option value="10">10개</option>
<option value="20">20개</option>
<option value="30">30개</option>
</select>
</li>
</ul>
</div><!-- // search_box -->
</form>
<div class="clear"></div>
<div id="board_title" class="b_list">
<div class="list cf">
<span class="num">번호</span>
<span class="title">제 목</span>
<span class="name">작성자</span>
<span class="date">작성일</span>
<span class="views">조회</span>
</div>
</div><!-- // board_title -->
<div id="board_list" class="b_list">
<?php // 글 목록 출력
while($row = $stmh->fetch(PDO::FETCH_ASSOC)) {
$item_num=$row["num"];
$item_id=$row["id"];
$item_name=$row["name"];
$item_nick=$row["nick"];
$item_hit=$row["hit"];
$item_date=$row["regist_day"];
$item_date=substr($item_date, 0, 10);
$item_subject=str_replace(" ", " ", $row["subject"]);
?>
<div class="list cf">
<span class="num"><?= $item_num ?></span>
<span class="title"><a href="view.php?num=<?=$item_num?>"><?= $item_subject ?></a></span>
<span class="name"><?= $item_nick ?></span>
<span class="date"><?= $item_date ?></span>
<span class="views"><?= $item_hit ?></span>
</div>
<?php
}
} catch (PDOException $Exception) {
print "오류: ".$Exception->getMessage();
}
?>
<div id="write_button">
<div class="list_btn fl">
<a href="list.php">목록</a>
</div>
<?php
if(isset($_SESSION["userid"]))
{
?>
<div class="list_btn fl">
<a href="write_form.php">글 작성</a>
</div>
<?php
}
?>
</div> <!-- id="write_button" -->
</div><!-- // board_list -->
<div id="page">
<?php echo $paging ?>
</div> <!-- // page -->
</div><!-- // board -->
</div><!-- // sub_main -->
</div><!-- // sub_wrap -->
$onePage = 5; // 한 페이지에 보여줄 게시글의 수.
위 부분이 게시글 수를 정하는 부분인 것으로 압니다. 5개로 설정했으나 총 게시물인 13개가 보여지고 하단에 페이지 넘버 (13개이므로) 3까지 잘 나옵니다. 페이지 넘버 클릭시 넘어가는 듯 하나 게시물이 계속 13개가 보이는 현상입니다...
도움 주시면 정말 감사하겠습니다!!