모든 게시판 페이지 (list.php, search_result.php)에 정렬을 처리하는 기능을 추가한다
table 위에 select 태그 입력
** list.php
<div>
<form method="post" action="array.php">
<select name = "array">
<option value="udate">오래된 순</option>
<option value="title">제목 순</option>
<option value="hit">조회수 순</option>
<option value="liked">좋아요 순</option>
</select>
<input type="submit" value="정렬"/>
</form>
</div>
option value의 값을 board DB의 컬럼 이름으로 설정한다
날짜 → udate
제목 → title
조회 수 → hit
좋아요 → liked

post 방식으로 array 값을 보낸다.
정렬을 처리할 array.php는 다른 게시판 페이지(list.php, search_result.php)와 같은 stylesheet
$array가 입력되면 $array 순으로 게시글을 정렬한다
<?php
session_start();
$conn = mysqli_connect('localhost','root','mysql','logindb');
$array = $_POST['array'];
if($array) {
$sql2 = "SELECT * FROM board ORDER BY $array DESC LIMIT $page_start, $list_num";
}
?>
** array.php 정렬 코드
<table align=center>
<thead align="center">
<tr>
<th width=70>번호</th>
<th width=300>제목</th>
<th width=120>작성자</th>
<th width=120>작성일</th>
<th width=70>조회수</th>
<th width=70>좋아요</th>
</tr>
</thead>
<?php
$n_sql = "SELECT count(*) FROM board";
$n_result = mysqli_query($conn,$n_sql);
$row = mysqli_fetch_row($n_result);
$num = $row[0];
$list_num=5; //한 페이지에 보여줄 개수
$page_num=3; //블록 당 보여줄 페이지 개수
$page=isset($_GET['page'])? $_GET['page'] :1;
$total_page = ceil($num/$list_num); //전체 페이지 수
$now_block = ceil($page/$page_num); //현재 블록 번호
$block_start=(($now_block-1)*$page_num)+1;// 블록의 시작 번호
if($block_start <= 0){
$block_start =1;
}
$block_end=$now_block*$page_num; //블록의 마지막 번호
if($block_end>$total_page){
$block_end=$total_page;
}
$page_start=($page-1) * $list_num;
if($array) {
$sql2 = "SELECT * FROM board ORDER BY $array DESC LIMIT $page_start, $list_num";
}
$result = mysqli_query($conn, $sql2);
$cnt = $page_start+1;
while($row = mysqli_fetch_array($result)){
?>
<tbody>
<tr align=center style="background: #ffffff;font-size:20px; color: black;">
<td><?php echo $cnt;?></td>
<td width="500" align="center"><a href="read.php?idx=<?=$row['idx']?>"><?php echo $row['title'];?></a></td>
<td width="100" align="center"><?php echo $row['name'];?></td>
<td width="200" align="center"><?php echo $row['udate'];?></td>
<td><?php echo $row['hit'];?></td>
<td><?php echo $row['liked'];?></td>
</tr>
<?php
$cnt++;
?>
</tbody>
<?php } ?>
</table>
※ 날짜, 제목, 조회 순 정렬 구현
게시판 페이지

오래된 순으로 정렬

제목 순으로 정렬

조회수 순으로 정렬

좋아요 순으로 정렬

'Web Hacking Study > Web Page 만들기' 카테고리의 다른 글
| 문의게시판 (문의 글 비밀번호 설정) (0) | 2022.06.22 |
|---|---|
| 문의 게시판 (연락처 포함한 문의 글 작성) (0) | 2022.06.22 |
| 마이페이지 (개인정보 확인, 개인정보 수정, 비밀번호 변경 기능) (0) | 2022.06.19 |
| 게시판 페이지 개발 (좋아요/좋아요 취소) (0) | 2022.06.15 |
| 게시판 페이지 개발 (파일 업로드, 파일 다운로드) (0) | 2022.06.07 |
댓글