소소한 IT이야기/PHP_개발

[M2 MAMP] PHP 게시판 구축 - 06 (feat. 리스트 검색, 페이징)

Klaus 2023. 8. 12. 16:22

1. 게시판 화면 구현 (list.php)

가장 오랜시간이 걸렸다.

list의 경우 초반 11월 초에 작성을 하였으나, 중간중간 수정해야 할 부분이 많아 여러 번 작성되었습니다.

페이징 하는 부분을 보고 따라 하는데도 이해가 잘 되지 않아 며칠 고생한 것 같습니다.

<?php 
	include_once "./config.php";
	include_once "./db/db_con.php";
	
	if (isset($_GET["page"]))
		$page = $_GET["page"];
	else
		$page = 1;
?>
<!DOCTYPE html>
<html>

	<body>
		<nav class="navbar navbar-default">
			<?php include_once "./fragments/header.php";?>
		</nav>
		<div class="modal fade" id="modal_div">
			<div class="modal-dialog">
				<div class="modal-content">
					<!-- header -->
					<div class="modal-header">

						<button type="button" class="close" data-dismiss="modal">×</button>
						<!-- header title -->
						<h4 class="modal-title"><b>비밀글 입니다.</b></h4>
					</div>
					<!-- body -->
					<div class="modal-body">
						<form method="post" id="modal_form" data-action="./ck_read.php?idx=">
							<p>비밀번호  <input type="password" name="pw_chk" /> <input type="submit" class="btn btn-primary" value="확인" /></p>
						</form>
					</div>
			  	</div>
		  	</div>
		</div>
		<div class="container">
			<div id="board_area"> 
			  <h1><b>자유게시판</b></h1><br>
			  <table class="table table-striped" style="text-align: center; border: 1px solid #ddddda">
			  	<tr>
					<th style="background-color: #eeeeee; text-align: center;">번호</th>
					<th style="background-color: #eeeeee; text-align: center;">제목</th>
					<th style="background-color: #eeeeee; text-align: center;">작성자</th>
					<th style="background-color: #eeeeee; text-align: center;">작성일</th>
					<th style="background-color: #eeeeee; text-align: center;">조회수</th>
			    </tr>
			    
/*페이징 구현 22.11.21*/
			    <?php 
			    	$sql = mq("select
			    					*
			    			   from 
			    					board
			    			");
			    	$total_record = mysqli_num_rows($sql); 
			  		
			    	$list = 5;
			  		$block_cnt = 5;
			  		$block_num = ceil($page / $block_cnt); 
			  		$block_start = (($block_num - 1) * $block_cnt) + 1; 
			    	$block_end = $block_start + $block_cnt - 1; 
			    	
			    	
			    	$total_page = ceil($total_record / $list); 
			    	if($block_end > $total_page){ 
			    		$block_end = $total_page; 
			    	}
			    	$total_block = ceil($total_page / $block_cnt);
			    	$page_start = ($page - 1) * $list;
			    	
			    	$sql2 = mq("select 
			    					* 
			    				from 
			    					board 
			    				order by 
			    					idx desc limit $page_start, $list
			    			");
			    	while($board = $sql2->fetch_array()){
			    		$title=$board["title"];
			    		if(strlen($title)>30){
			    			$title=str_replace($board["title"],mb_substr($board["title"],0,30,"utf-8")."...",$board["title"]);
			    		}
			    
			    		$sql3 = mq("select
			    						*
			    				    from
			    						reply
			    					where
			    						con_num='".$board['idx']."'
			    				");
			    		$rep_count = mysqli_num_rows($sql3);
			    					  
			    ?>
			    
			      <tbody>
			      	<tr>
			          <td width="70"><?=$board['idx']; ?></td>
			          <td width="500"> 
			          <?php 
			          	$lockimg="<img src='./img/lock.png' alt='lock' title='lock' width='18' height='18'>";
			          	if($board['lock_post']=="1"){
			          ?>
			          		<span class="lock_check" style="cursor:pointer" data-action="./read.php?idx="
			          		data-check=<?=$role ?> data-idx="<?=$board['idx']?>" ><?=$title?> <?=$lockimg?></span>
			          <?php 
			          	}else{
			          ?>
			          		<span class="read_check" style="cursor:pointer" data-action="./read.php?idx=<?=$board['idx']?>" ><?=$title?></span> 
			          		<span style="color:blue;">[<?=$rep_count?>]</span></td>
			          <?php 		
			          	}
			          ?>
			          <td width="120"><?=$board['name'];?></td>
			          <td width="100"><?=$board['date'];?></td>
			          <td width="100"><?=$board['hit']; ?></td>
			        </tr>
			      </tbody>
			      <?php } ?>
			    </table>
			    <div id="page_num" style="text-align: center;">
			    	<?php
				    	if ($page <= 1){
				    		// 빈 값
				    	} else {
				    		echo "<a href='list.php?page=1'>처음</a>";
				    	}
				    	
				    	if ($page <= 1){
				    		// 빈 값
				    	} else {
				    		$pre = $page - 1;
				    		echo "<a href='list.php?page=$pre'>◀ 이전 </a>";
				    		
				    	}
				    	
				    	for($i = $block_start; $i <= $block_end; $i++){
				    		if($page == $i){
				    			echo "<b> $i </b>";
				    		} else {
				    			echo "<a href='list.php?page=$i'> $i </a>";
				    		}
				    	}
				    	
				    	if($page >= $total_page){
				    		// 빈 값
				    	} else {
				    		$next = $page + 1;
				    		echo "<a href='list.php?page=$next'> 다음 ▶</a>";
				    	}
					   	
				    	if($page >= $total_page){
				    		// 빈 값
				    	} else {
				    		echo "<a href='list.php?page=$total_page'>마지막</a>";
				    	}
					?>
				</div>	
			    <div id="write_btn">
			      <a href="write.php"><button class="btn btn-primary pull-right" >글쓰기</button></a>
			    </div>
			  <div id="search_box" style="text-align: center; padding-top: 50px;">
			  	<form action="search_result.php" method="get">
			  		<select name="category">
			  			<option value="title">제목</option>
			  			<option value="name">글쓴이</option>
			  			<option value="content">내용</option>
			  		</select>
			  		<input type="text" name="search" size="40" required="required">
			  			<button class="btn btn-primary">검색</button>
			  	</form>
			  </div>
		  </div>
		</div>
		<script src="./js/login.js"></script>
		
		<script>
		$(function(){
		    $(".lock_check").click(function(){
			    if($(this).attr("data-check")=="ADMIN") {
			    	var action_url = $(this).attr("data-action")+$(this).attr("data-idx");
					$(location).attr("href",action_url);
			    }
				$("#modal_div").modal();
				var action_url = $("#modal_form").attr("data-action")+$(this).attr("data-idx");
				$("#modal_form").attr("action",action_url);
			});
		});
	
		$(function(){
		    $(".read_check").click(function(){
			    var action_url = $(this).attr("data-action");
			    console.log(action_url);
			    $(location).attr("href",action_url);
			});
		});
		</script>
	</body>
</html>