본문 바로가기

[게시판] 게시글 조회

1. 게시글 조회: view.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
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
    </head>
 
    <body>
        <?php
            include "./connectDB.php";
            include "./session.php";
 
            $conn = connectDB();
 
            if(isset($_GET["postNum"])) {
                $postNum = $_GET["postNum"];
            } else {
        ?>            
                <script>
                    alert("오류");
                    history.back();
                </script>
        <?php
            }
        
            if(preg_match("/[^0-9]+/"$postNum)) {
        ?>  
                <script>
                    alert("No Hack \'~\'");
                    history.back();
                </script>
        <?php
            }  
            
            $query = "SELECT * FROM posts WHERE seq=$postNum";
            $result = mysqli_query($conn$query);
            $arr = mysqli_fetch_array($result, MYSQLI_ASSOC);
            $seq = $arr["seq"];
        
            if($postNum != $seq) {//읽어온 글 번호와 실제 글 번호가 다르면 뒤로 이동
        ?>
                <script>
                    history.back(); //history.go(-1)과 동일
                </script>
        <?php
              }
 
            $writer = $arr["writer"];
            $subject = $arr["subject"];
            $content = $arr["content"];
            $posted = $arr["posted"];
            $userID = $arr["id"];
        ?>
        <table align="center" border="1" width="800px">
            <tr>
                <td align="center" width="60px"<?=$postNum?> </td>
                <td align="left" width="740px"<?=$subject?> </td>
                <td align="center" width="200px"<?=$writer?> </td>
            </tr>
 
            <tr>
                <td colspan="4" align="center">
                    <textarea style="resize:none; height:500px; width:1000px;" readonly><?=$content?></textarea>
                </td>
            </tr>
 
            <tr>
                <td colspan="4" align="center" width="800px"><?=$posted?></td>
            </tr>
    
            <tr align="right">
                <td colspan="3" align="right">
                    <input type="submit" value="목록" onclick="location.href='./list.php'"/>
                    <input type="submit" value="댓글 작성" onclick="location.href='./comment_form.php?postNum=<?=$postNum?>&prnt=0'" />
                    <?php
                        if($id === $userID) {
                    ?>
                            <input type="submit" value="수정" onclick="location.href='./update_form.php?postNum=<?=$postNum?>'"/>    
                            <input type="submit" value="삭제" onclick="location.href='./remove.php?postNum=<?=$postNum?>'"/>
                    <?php
                        }
                    ?>
                </td>
            </tr>
        </table>
        </br>
        <div align="center"
            <table width="600px">
            <?php
                $commentQuery = "SELECT * FROM comments WHERE postSeq=$seq";
                $commentResult = mysqli_query($conn$commentQuery);
                $comments = mysqli_fetch_array($commentResult);
 
                while($comments) {
                    $writer = $comments["writer"];
                    $commented = $comments["commented"];
                    $content = $comments["content"];
                    $commentNum = $comments["seq"];
                    
                    if($comments["parent"== 0) {
            ?>
                    <!-- 댓글 --> 
                        <tr>
                            <td> &nbsp; </td>
                        </tr>
                        <tr align="center">
                            <td width="400px" align="left" colspan="2"<?=$writer?> </td>
                            <td width="200px" align="right"<?=$commented?> </td>
                        </tr>
                        <tr
                            <td colspan="3" width="600px" align="left"<?=$content?> </td>
                            </tr>
                        <tr>
                            <td colspan="3" width="600px" align="right">
                                <input type="submit" value="답글 작성" onclick="location.href='./comment_form.php?postNum=<?=$postNum?>&prnt=<?=$commentNum?>'"/>
                                <input type="submit" value="수정" onclick="location.href='./update_com_form.php?postNum=<?=$postNum?>&commentNum=<?=$commentNum?>'" />
                                <input type="submit" value="삭제" onclick="location.href='./remove_com.php?postNum=<?=$postNum?>&commentNum=<?=$commentNum?>'" />
                                </td>
                        </tr>
            <?php
                    }
                        $query ="SELECT * FROM comments where parent=$commentNum"//현재 댓글의 답글을 찾는 쿼리
                        $replyResult = mysqli_query($conn$query);
                        $replies = mysqli_fetch_array($replyResult);
    
                        while($replies) {
                            $writer = $replies["writer"];
                            $replied = $replies["commented"];
                            $replyContent = $replies["content"];
                            $replyNum = $replies["seq"];
 
                            echo " replyNum: $replyNum</br>";
            ?>
                            <!-- 답글  -->
                            <tr>
                                <td width="100px" rowspan="2" align="center"> &#x21AA;&nbsp;&nbsp;&nbsp; </td>
                                <td width="200px" align="left"<?=$writer?> </td>
                                <td width="300px" align="right"<?=$replied?> </td>
                            </tr>
                            <tr>
                                <td width="600px" align="left"<?=$replyContent?> </td>
                            </tr>
                            <tr>
                                <td colspan="3" width="600px" align="right"
                                    <input type="submit" value="수정" onclick="location.href='./update_com_form.php?postNum=<?=$postNum?>&commentNum=<?=$replyNum?>'" />
                                    <input type="submit" value="삭제" onclick="location.href='./remove_com.php?postNum=<?=$postNum?>&commentNum=<?=$replyNum?>'" />
                                </td>
                            </tr>
            <?php
                            $replies = mysqli_fetch_array($replyResult);
                        }
                    
                    $comments = mysqli_fetch_array($commentResult);
                } 
            ?>
            </table>
         </div>
    </body>
</html>
cs

 

 

39행: 이전 페이지에서 받은 글 번호와 실제 글 번호가 다르면 다시 이전 페이지로 이동한다.

 

47행 ~ 51행: 조회할 글의 작성자명, 제목, 내용, 작성일, 작성자의 ID를 읽어온다.

 

75행 ~ 81행: 현재 로그인 되어 있는 ID와 조회하고 있는 글의 작성자 ID가 같으면 

 수정 버튼과 삭제 버튼을 띄운다.

 

93행: 읽어올 댓글이 없을 때까지 반복

 

99행: parent 컬럼의 값이 0이면(상위 댓글이면) 답글 작성 버튼을 띄운다.

 

121행: 현재 댓글의 답글을 찾는 쿼리.

125행: 읽어올 답글이 없을 때까지 반복. 만약 없으면 반복문에 진입하지 않는다.

         

135행: &#x21AA;는 └> 모양을 나타내는 html entity다. 

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

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