본문 바로가기

[게시판] 게시글 검색

1. 게시글 검색: search.php

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<!DOCTYPE html>
<?php
    include "./connectDB.php";
    include "./session.php";
    
    $conn = connectDB();
    $query = "SELECT name FROM users WHERE id=\"$id\"";
    $result = mysqli_query($conn$query);
    $arr = mysqli_fetch_array($result, MYSQLI_ASSOC);
    $name = $arr["name"];    
?>
 
<html>
    <head>
        <meta charset="utf-8" />
        <title> 검색 결과 </title>
    </head>
 
    <body>
        <table align="center" border="1" width="1000">
            <tr>
                <td align="right" colspan="4">
                    현재 사용자: <?=$name?>    
                <td>    
            </tr>
            <tr align="center">
                <td width="80"> 번호 </td>
                <td width="500"> 제목 </td>
                <td width="260"> 작성자 </td>
                <td width="160"> 작성 시각 </td>
            </tr>   
        <?php
            if(!isset($_GET["searchValue"]) || !isset($_GET["searchType"])) {
        ?>
                <script>
                    alert("검색 실패");
                    document.location.href="./list.php";
                </script>
        <?php
            } else {
 
                if(preg_match("/\"/i"$_GET["searchValue"])) { 
        ?>
                    <script>
                        alert("검색 실패");
                        document.location.href="./list.php";
                    </script>
        <?php
                    exit();
                }
 
                if(!($_GET["searchType"=== "1"|| !($_GET["searchType"=== "2"|| !($_GET["searchType"=== "3") ){
        ?>
                    <script>
                        alert("검색 실패");
                        document.location.href="./list.php";
                    </script>
        <?php
                    exit();
                }
        
                $searchValue = $_GET["searchValue"];
                $searchType = $_GET["searchType"];
            } 
 
            $query = "SELECT * FROM posts";
            $result = mysqli_query($conn$query);
            $listSize = 10//한 페이지 당 목록에 담기는 게시글 수
            $oneSecSize = 10//한 페이지에서 보여줄 페이지 수 (1~10, 11~20)
            $query = "";
    
            if($searchType === "1") { //제목
                $query = "SELECT * FROM posts WHERE subject LIKE \"%$searchValue%\"";
            } else if($searchType === "2") { //내용
                $query = "SELECT * FROM posts WHERE content LIKE \"%$searchValue%\"";
            } else if($searchType === "3") { //제목 + 내용
                $query = "SELECT * FROM posts WHERE subject LIKE \"%$searchValue%\" OR content LIKE \"%$searchValue%\""
            }  
            
            $result = mysqli_query($conn$query);
            $postsNum = mysqli_num_rows($result); //총 게시글 수
            $pageSize = ceil($postsNum / $listSize); //총 페이지 수. ceil()은 소수점 아래를 무조건 올린다.
            $arr = mysqli_fetch_array($result);
 
            if(!$result) {
        ?>    
                <script>
                    alert("검색 실패");
                    document.location.href="./list.php";
                </script>
        <?php
                exit();
            }
    
            if(mysqli_num_rows($result== 0) {
        ?>
                <script>
                    alert("검색 결과 없음");
                    document.location.href="./list.php";
                </script>
        <?php
                exit();
            } 
 
            if(isset($_GET["idx"])) { 
                $idx = $_GET["idx"]; 
            } else { 
                $idx = 1;  
            }
 
            if(preg_match("/[^0-9]+/"$_GET["idx"])) {   
        ?>
                <script>
                    alert("No Hack\'~\'");
                    document.location.href="./list.php";
                </script>
        <?php
            }    
 
            if($idx < 1) {
                $idx = 1;
            } else if($idx > $pageSize) {
                $idx = $pageSize;
            }
        
            $curSection = ceil($idx / $oneSecSize); 
            $secSize = ceil($pageSize / $oneSecSize);
        
            $startPage = ($curSection - 1)*$oneSecSize + 1;
               $endPage = $curSection * $oneSecSize;
    
               if($endPage > $pageSize) { $endPage = $pageSize; }
        
            $start = ($idx - 1* 10 + 1;
            $end = $start + 9;
    
            $temp = $secSize - ($idx - 1);
            $query .= "ORDER BY seq DESC LIMIT 10"
            $result = mysqli_query($conn$query);    
 
            while($arr = mysqli_fetch_array($result)) {
                $seq = $arr["seq"];
                $subject = $arr["subject"];
                $writer = $arr["writer"];
                $posted = $arr["posted"];
        ?>
                <tr align="center">
                    <td width="80"<?=$seq?> </td>
                    <td width="500"> <a href="./view.php?postNum=<?=$seq?>"<?=$subject?> </a> </td>
                    <td width="260"<?=$writer?> </td>
                    <td width="160"<?=$posted?> </td>
                </tr>
        <?php
            } 
        ?>
            <tr>
                <td align="right" colspan="4">
                    <button type="button" onclick="location.href='./logout.php'">로그아웃</button>
                    <button type="button" onclick="location.href='./list.php'">목록으로</button>
                </td>
            </tr>
        </table>
        <div name="list" align="center">
        <?php
            if($curSection > 1) {
                $prevPage = $startPage - 1;
        ?>
                <a href="./search.php?idx=<?=$prevPage?>&searchType=<?=$searchType?>&searchValue=<?=$searchValue?>">[이전]</a>
        <?php
            }
 
            for($idxes = $startPage$idxes <= $endPage$idxes++) {
                   if($idxes == $idx) {
        ?>
                    <a href="./search.php?idx=<?=$idxes?>&searchType=<?=$searchType?>&searchValue=<?=$searchValue?>"><b>[<?=$idxes?>]</b></a>
        <?php
                } else {
        ?>
                    <a href="./search.php?idx=<?=$idxes?>&searchType=<?=$searchType?>&searchValue=<?=$searchValue?>">[<?=$idxes?>]</a>
        <?php
                }
            }
 
            if($curSection < $secSize) {
                $nextPage = $endPage + 1;
        ?>
                <a align="center" href="./search.php?idx=<?=$nextPage?>&searchType=<?=$searchType?>&searchValue=<?=$searchValue?>">[다음]</a>
        <?php
            }
        ?>
        </div>
    </body>
</html>
cs

 

기본적으로 list.php와 흡사하다.

list.php가 모든 글을 목록에 띄운다면 search.php는 검색 조건에 맞는 글을 목록에 띄울 뿐이다.

 

 

33행: 검색 내용과 검색 유형은 GET 방식으로 받아온다. 

  서버의 정보를 바꾸는 게 아니라, 서버에 값을 전달하고 그 값을 토대로 

  데이터를 얻어오는 것이기 때문이다.

  이 때 두 값 중 하나라도 정의되어 있지 않으면 게시글 목록으로 이동한다.

 

72행 ~ 78행: searchType 값에 따라 제목, 내용, 제목+내용으로 검색한다.

 

95행: 쿼리 실행 결과의 행 수가 0이면 검색 결과가 없다는 뜻이다.

 

142행: 앞의 검색 쿼리에  ORDER BY seq DESC LIMIT 10 을 덧붙인다.

 

168행, 175행, 179행, 187행: 기본적으로 list.php와 동일하지만 searchType와 searchValue를

   GET 방식으로 전달하기 위해 URL 뒤에 덧붙인다는 점이 다르다.

 

'Programming > Web' 카테고리의 다른 글

[게시판] 댓글 수정  (0) 2019.07.21
[게시판] 댓글 작성  (0) 2019.07.21
[게시판] 게시글 조회  (0) 2019.07.18
[게시판] 게시글 목록  (0) 2019.07.18
[게시판] 게시글 삭제  (0) 2019.07.18