[Web Application-개발] 2023-11-22 웹 어플리케이션 제작 프로젝트 : 게시판 만들기 #9-4

2023. 11. 29. 08:39Project/Web Application-개발

 

프로젝트 일지

 

  지난 작업에서 게시판의 CRUD 기능까지 구현하여 어느 정도 게시판의 역할을 다한다. 이번 작업에서는 게시글 조회 기능을 구현하면서 게시판 작업을 마무리할 예정이다. 게시판 조회 기능은 board.php 페이지에서만 이루어지며 찾고자 하는 문자열을 입력하면 입력한 문자열을 포함한 게시글을 조회한다.

 

게시글 조회

 

1) board.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
<!DOCTYPE html>
<html lang="ko">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Board</title>
</head>
<body>
<table border="1" bordercolor="black" width ="700" height="100" align = "center" >
    <th>번호</th>
    <th>제목</th>
    <th>글쓴이</th>
    <th>작성일</th>
    <?php 
        require_once("db.php");
        require_once("paging.php");
 
        $conn = db_conn();
        $search = $_GET['search'];  
        $search_case = $_GET['search_case'];
 
        // 초기 $_GET['page']에 값이 없으면 $page = 1로 초기화 => 게시판 클릭하면 처음 1페이지가 디폴트
        if (!isset ($_GET['page']) ) $page = 1;  
        else $page = $_GET['page'];  
 
        // 한번에 볼 수 있는 페이지 수
        $view_page = 5;
 
        // 한 페이지에 보일 게시글 표시
        $limit_page = ($page-1* $view_page;
 
        
        if($search)
        {
            // 페이징 URL 작업
            $url = "http://192.168.74.128:1018/php/board.php?".$search_case."&search=".$search."&page=";
 
            if($search_case == 'title'$where = "where title like '%$search%'";
            else if($search_case == 'content'$where = "where content like '%$search%'";
            else if($search_case == 'title_content'$where = "where title like '%$search%' or content like '%$search%'";
            else $where = "where user_id like '%$search%'";
 
            // 총 게시글 수
            $total_content = mysqli_fetch_array(mysqli_query($conn"select count(*) from test_board $where"));
            $sql = "select * from test_board $where order by reg_date DESC limit $limit_page,$view_page";
        }
 
        else
        {
             // 페이징 URL 작업
            $url = "http://192.168.74.128:1018/php/board.php?page=";
             // 총 게시글 수
            $total_content = mysqli_fetch_array(mysqli_query($conn"select count(*) from test_board"));
            $sql = "select * from test_board order by reg_date DESC limit $limit_page,$view_page";
 
        }
 
        $result = mysqli_query($conn$sql);    
        mysqli_close($conn);
 
        while($row = mysqli_fetch_array($result)){ ?>
            <tr align = "center">
                <td><?= $row['idx']; ?></td>
                <td><a href="http://192.168.74.128:1018/php/board_crud.php?mode=r&num=<?= $row['idx']; ?>"><?= $row['title']; ?></a></td>
                <td><?= $row['user_id']; ?></td>
                <td><?= $row['reg_date']; ?></td>
            </tr>                             
    <?php }        
    ?>
</table>
 
<div style="text-align:center;margin:20px;">
    <button button type = "button" onClick = "location.href = '/php/board.php'"> 전체글 </button>
    <button button type = "button" onClick = "location.href = '/php/board_crud.php?mode=c'"> 글쓰기 </button>
</div>
<p style="text-align:center;"><?= paging($view_page$page$total_content[0], $url); ?></p>
 
<form method="GET" action="" style="text-align:center;">
    <select name = "search_case">
        <option value="title"> 제목 </option>
        <option value="content"> 내용 </option>
        <option value="title_content"> 제목+내용 </option>        
        <option value="writer"> 작성자 </option>
    </select>
    <input type="text" name="search" > <button type="submit"> 조회 </button>
</form>
    
</body>
</html>
cs

 

  board.php 페이지에 게시글 조회를 위해 추가한 코드 위주로 어떤 역할을 담당하는지 알아보자.

 

[1] 콤보박스 및 문자열 입력

1
2
3
4
5
6
7
8
9
10
11
 
    <form method="GET" action="" style="text-align:center;">
    <select name = "search_case">
        <option value="title"> 제목 </option>
        <option value="content"> 내용 </option>
        <option value="title_content"> 제목+내용 </option>        
        <option value="writer"> 작성자 </option>
    </select>
    <input type="text" name="search" > <button type="submit"> 조회 </button>
    </form>
 
cs
  • 게시글 조회 시 어떤 속성을 기준으로 정할건지 선택하기 위해 콤보박스 형태로 선택지를 추가하였다.
  • 콤보박스에는 제목, 내용, 제목+내용, 작성자 속성이 존재하며 기준으로 정할 속성을 선택하면 된다.
  • input 태그에는 조회하고 싶은 문자열을 입력한다. 선택한 속성에 입력한 문자열이 포함되어 있는지 확인하기 위해 '조회' 버튼을 눌러 현재 페이지에 GET 메소드로 파라미터를 전송한다.

 

[2] 파라미터

1
2
3
4
5
6
<?php
 
    $search = $_GET['search'];
    $search_case = $_GET['search_case'];
 
?>
cs
  • $search = 조회 시 선택한 속성에 포함될 문자열을 저장한 변수
  • $search_case = 조회 시 선택한 속성을 저장한 변수

 

[3] 조회 시 조건문

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
<?php
 
    if($search)        
    {
        // 페이징 URL 작업
        $url = "http://192.168.74.128:1018/php/board.php?search_case=".$search_case."&search=".$search."&page=";
 
        if($search_case == 'title'$where = "where title like '%$search%'";
        else if($search_case == 'content'$where = "where content like '%$search%'";
        else if($search_case == 'title_content'$where = "where title like '%$search%' or content like '%$search%'";
        else $where = "where user_id like '%$search%'";
 
        // 총 게시글 수
        $total_content = mysqli_fetch_array(mysqli_query($conn"select count(*) from test_board $where"));
        $sql = "select * from test_board $where order by reg_date DESC limit $limit_page,$view_page";
    }
 
    else
    {
        // 페이징 URL 작업
        $url = "http://192.168.74.128:1018/php/board.php?page=";
        // 총 게시글 수
        $total_content = mysqli_fetch_array(mysqli_query($conn"select count(*) from test_board"));
        $sql = "select * from test_board order by reg_date DESC limit $limit_page,$view_page";
 
    }
?>
cs
  • $search 변수가 비어있지 않은 경우(문자열을 입력한 채로 조회 버튼을 클릭한 경우)와 일반적인 경우에 따라 값들이 달라진다.
  • $url 변수는 조회 시 페이징에 필요한 URL에 조회 당시 속성과 문자열을 파라미터로 담는다.
  • 또한 선택한 속성에 따라 쿼리문에 삽입될 조건문(where)의 내용을 조금씩 변경한다.
  • 조건문에 따라 조회되는 게시글의 수도 다르므로 $total_content(총 게시글 수)을 구하는 쿼리문에 조건문을 삽입하여 구한다.

 

[4] 작동 화면

 

앞으로 할 작업

 

  • 디자인 작업
  • 공통 헤더 작업
  • 게시글 파일 업로드 기능 구현