게시판 검색을 할 때 지정 기간의 게시물만 나오는 기능을 추가하자.
우선적으로 제목, 작성자, 내용 카테고리를 구분해서 완성해 놓은 상태이다.
그래서 코드 몇 줄만 추가하면 되는 작업이라 그리 어렵진 않았다.
[index.php]
1
2
3
4
5
6
7
8
9
10
|
<form action="search.php" method="get">
<select name="catgo">
<option value="title">제목</option>
<option value="id">작성자</option>
<option value="content">내용</option>
</select>
<input type="text" name="search" size="40" align="center" required="required" /> <button>검색</button>
<i class="far fa-calendar-alt"></i><input type = "date" name ="date_from" />
<i class="far fa-calendar-alt"></i><input type = "date" name = "date_to" />
</form>
|
cs |
fontawesome 을 통해 달력 이미지를 가져오고, input type을 date로 설정하면 날짜를 선택할 수 있게 페이지에 나타난다.
카테고리와 날짜를 선택해서 검색버튼을 누르면 search.php로 해당 정보들이 전송된다.
[search.php]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<?php
include './db_connect.php';
$category=$_GET['catgo'];
$keyword=$_GET['search'];
$start_date=$_GET['date_from'];
$end_date=$_GET['date_to'];
if($start_date && $end_date) {
$sql ="select * from board where $category like '%$keyword%' and date between '$start_date' and '$end_date' order by number desc";
$result = $connect->query($sql);
$total = mysqli_num_rows($result);
}
else {
$sql = "select * from board where $category like '%$keyword%' order by number desc";
$result = $connect->query($sql);
$total = mysqli_num_rows($result);
}
?>
|
cs |
날짜를 선택해서 기간을 정했을 경우 해당 기간내에 있는 게시물들만 나올 수 있게 SQL문을 작성한다. (10번 줄)
기간을 정하지 않았을 경우에는 키워드를 검색한 모든 게시물들이 나오게 한다. (15번 줄)
'WebHacking' 카테고리의 다른 글
SQL Injection 공백 문자 필터링 우회 (0) | 2023.01.15 |
---|---|
WebHacking - (11)마이페이지 만들기 (0) | 2022.12.28 |
WebHacking - (9)게시판 만들기_좋아요 기능 (0) | 2022.12.26 |
WebHacking - (1.1)CentOS7 MySQL 초기 root 비밀번호 설정 오류 (0) | 2022.12.23 |
WebHacking - (8)XSS 기본 개념 정리 (0) | 2022.11.11 |