안효원 안효원 2021-07-30
답글 기능 구현 완료
@d1823ef72106f985fc22dd27c26c57fefcffd169
.idea/dataSources/482ad897-bf06-4872-a94f-ca218d669792.xml
--- .idea/dataSources/482ad897-bf06-4872-a94f-ca218d669792.xml
+++ .idea/dataSources/482ad897-bf06-4872-a94f-ca218d669792.xml
@@ -1088,44 +1088,54 @@
       <Position>7</Position>
       <DataType>varchar(300)|0s</DataType>
     </column>
-    <key id="343" parent="333" name="PRIMARY">
+    <column id="343" parent="333" name="listNum">
+      <Position>8</Position>
+      <DataType>int(11)|0s</DataType>
+      <NotNull>1</NotNull>
+      <DefaultExpression>0</DefaultExpression>
+    </column>
+    <column id="344" parent="333" name="depth">
+      <Position>9</Position>
+      <DataType>varchar(255)|0s</DataType>
+    </column>
+    <key id="345" parent="333" name="PRIMARY">
       <NameSurrogate>1</NameSurrogate>
       <ColNames>num</ColNames>
       <Primary>1</Primary>
     </key>
-    <column id="344" parent="334" name="id">
+    <column id="346" parent="334" name="id">
       <Position>1</Position>
       <DataType>varchar(255)|0s</DataType>
       <NotNull>1</NotNull>
     </column>
-    <column id="345" parent="334" name="pw">
+    <column id="347" parent="334" name="pw">
       <Position>2</Position>
       <DataType>varchar(255)|0s</DataType>
       <NotNull>1</NotNull>
     </column>
-    <column id="346" parent="335" name="num">
+    <column id="348" parent="335" name="num">
       <Position>1</Position>
       <DataType>int(11)|0s</DataType>
       <NotNull>1</NotNull>
       <SequenceIdentity>null</SequenceIdentity>
     </column>
-    <column id="347" parent="335" name="con_num">
+    <column id="349" parent="335" name="con_num">
       <Position>2</Position>
       <DataType>int(11)|0s</DataType>
     </column>
-    <column id="348" parent="335" name="reply_writer">
+    <column id="350" parent="335" name="reply_writer">
       <Position>3</Position>
       <DataType>varchar(50)|0s</DataType>
     </column>
-    <column id="349" parent="335" name="reply_content">
+    <column id="351" parent="335" name="reply_content">
       <Position>4</Position>
       <DataType>varchar(100)|0s</DataType>
     </column>
-    <column id="350" parent="335" name="reply_pw">
+    <column id="352" parent="335" name="reply_pw">
       <Position>5</Position>
       <DataType>int(11)|0s</DataType>
     </column>
-    <key id="351" parent="335" name="PRIMARY">
+    <key id="353" parent="335" name="PRIMARY">
       <NameSurrogate>1</NameSurrogate>
       <ColNames>num</ColNames>
       <Primary>1</Primary>
.idea/inspectionProfiles/Project_Default.xml
--- .idea/inspectionProfiles/Project_Default.xml
+++ .idea/inspectionProfiles/Project_Default.xml
@@ -9,5 +9,6 @@
       <option name="processLiterals" value="true" />
       <option name="processComments" value="true" />
     </inspection_tool>
+    <inspection_tool class="SqlRedundantOrderingDirectionInspection" enabled="false" level="WARNING" enabled_by_default="false" />
   </profile>
 </component>
(No newline at end of file)
 
answerWriteFront.php (added)
+++ answerWriteFront.php
@@ -0,0 +1,54 @@
+<?php
+/**
+ * @var $connStatus
+ */
+include("config/db.config.php");
+session_start();
+if (!isset($_SESSION['is_login'])){
+    header('Location:index.php');
+}
+?>
+<!doctype html>
+<html lang="ko">
+<head>
+    <meta charset="UTF-8">
+    <meta name="viewport"
+          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
+    <meta http-equiv="X-UA-Compatible" content="ie=edge">
+    <title>Document</title>
+    <style>
+        #page {width: 80%;margin: 0 auto;background-color: antiquewhite; height: 500px;}
+        #conn {width: 80%;margin: 0 auto;}
+        #title {text-align: center;}
+        #hr {border-style: solid;}
+        #contents {width: 80%; margin: 0 auto;}
+        #form {width: 56.5%;}
+        #text1 {width: 250px; float: right;}
+        #text2 {width: 250px; float: right;}
+        #text3 {width: 500px; height: 250px; float: right;}
+        .btn {font-weight: bolder; background-color: darkslategrey; border: black solid; color: white; height: 30px; width: 50px; float: right;}
+        .btn:hover {background-color: black;}
+        #textareaHeight {height: 250px;}
+    </style>
+</head>
+<body>
+<div id="conn">
+    <?php
+    echo $connStatus;     //DB 연결 상태 표시
+    ?>
+</div>
+<div id="page">
+    <div id="title"><h1>답글 쓰기</h1></div>
+    <hr id="hr">
+    <div id="contents">
+        <form id="form" method="post" action="config/answerWriteBack.php?whereValue=<?php echo $_GET['whereValue']?>&option=<?php echo $_GET['option']?>&searchText=<?php echo $_GET['searchText']?>">
+            <p>작성자 : <input id="text1" type="text" name="writer"></p>
+            제목 : <input id="text2" type="text" name="title" placeholder="최대 50자">
+            <p id="textareaHeight">글 : <textarea id="text3" name="contents" placeholder="최대 500자"></textarea></p>
+            <input type="submit" class="btn" value="작성">
+        </form>
+        <button class="btn" onclick="location.href='view.php?whereValue=<?php echo $_GET['whereValue'];?>&option=<?php echo $_GET['option']?>&searchText=<?php echo $_GET['searchText']?>'">취소</button>
+    </div>
+</div>
+</body>
+</html>(No newline at end of file)
 
config/answerWriteBack.php (added)
+++ config/answerWriteBack.php
@@ -0,0 +1,70 @@
+<?php
+/**
+ * @var $mysqli
+ */
+ini_set("display_errors", 1);
+include("db.config.php");
+
+/* 변수 초기화 : start */
+$writer= $_POST['writer']; //작성자
+$query = ""; //쿼리문
+/* 변수 초기화 : end */
+$whereValue = $_GET['whereValue'];
+$option = $_GET['option'];
+$text = $_GET['searchText'];
+
+/* 게시물 입력 정보 POST : start */
+if($writer != null){
+    $writer= $_POST['writer'];
+}else{
+    $writer = "Anonymous";
+}
+$title = $_POST['title'];
+$contents = $_POST['contents'];
+$date = date('Y:m:d');
+/* 게시물 입력 정보 POST : end */
+
+/* 답글의 depth값 확인 */
+$queryDepthCheck = "select depth from data where num = '$whereValue'";
+$resultDepthCheck = $mysqli->query($queryDepthCheck);
+$rowDepthCheck = $resultDepthCheck->fetch_array();
+/***************************************************************************************/
+
+/* 최상위 부모글의 num */
+while($rowDepthCheck[0] == 0){
+    $rowDepthCheck[0] =  $rowDepthCheck[0] - 1;
+}
+$queryCheckTopListNum = "select listNum from data where depth = '$rowDepthCheck[0]'";
+$resultCheckTopListNum = $mysqli->query($queryCheckTopListNum);
+$rowCheckTopListNum = $resultCheckTopListNum->fetch_array();
+/***************************************************************************************/
+
+if($title != null){          /* 제목 공백일시 confirm */
+    if($rowDepthCheck[0] < 1){          /* 이중 답글인지 확인 */
+        $query = "insert into data (num, writer, title, contents, date, depth, listNum)                  
+                    values(null, '$writer', '$title', '$contents', '$date', '1', '$whereValue')";
+
+        $query2 = "update data set listNum = '$whereValue' where num='$whereValue'";
+        if($query && $query2){
+            $result = $mysqli->query($query);
+            $result2 = $mysqli->query($query2);
+            echo "<script>alert('글이 등록되었습니다.');</script>";
+            echo "<meta http-equiv='refresh' content='0.1; url=../main.php'>";
+        }else{
+            echo "FAIL";
+        }
+    }else{    /* 답글에 답글이 달릴때 */
+        $depthPlusOne = $rowDepthCheck[0]+1;
+        $query = "insert into data (num, writer, title, contents, date, depth, listNum) /*-> 최상위 부모글의 num  */                
+                    values(null, '$writer', '$title', '$contents', '$date', '$depthPlusOne', '$rowCheckTopListNum[0]')";
+        if($query){
+            $result = $mysqli->query($query);
+            echo "<script>alert('글이 등록되었습니다.');</script>";
+            echo "<meta http-equiv='refresh' content='0.1; url=../main.php'>";
+        }
+    }
+}else{                 //if title == null
+    echo "<script>alert('제목을 입력하세요!')</script>";
+
+    echo "<meta http-equiv='refresh' content='0.1; url=../answerWriteFront.php?whereValue=$whereValue&option=$option&searchText=$text'>";
+}(No newline at end of file)
config/delete.php
--- config/delete.php
+++ config/delete.php
@@ -1,18 +1,38 @@
 <?php
 /**
- * @var $mysqli
- */
-ini_set("display_errors", 0);
-include("db.config.php");
+* @var $mysqli
+*/
+    ini_set("display_errors", 0);
+    include("db.config.php");
 
-$num = $_GET['whereValue'];
+    $num = $_GET['whereValue'];
+    $zero = 0;
 
-$query = "delete from data where num=$num";
-$result = $mysqli->query($query);
+    /* 부모글의 listNum 가져오기 */
+    $queryCheck2 = "select listNum from data where num=$num";
+    $resultCheck2 = $mysqli->query($queryCheck2);
+    $row = $resultCheck2->fetch_array();
+    /**************************************************************************/
 
-unlink("/home/hw/www/download/".$_GET['file']); //DB 행 삭제 시 서버측 파일까지 삭제
+    /* 삭제하려는 답글의 listNum을 가진 모든 행의 개수 구하기 */
+    $queryCheck = "select count(*) from data where listNum = $row[0]";
+    $resultCheck = $mysqli->query($queryCheck);
+    $rowCheck = $resultCheck->fetch_array();
+    /**************************************************************************/
 
-echo "<script>alert('삭제를 완료했습니다.');</script>";
-echo "<meta http-equiv='refresh' content='0.1; url=../main.php'>";
+    if($rowCheck[0] > 2){             /* 답글이 하나만 달린 부모 글의 답글이 삭제되면 해당 부모글의 listNum 0 처리 */
+        null;
+    }else{
+        $query2 = "update data set listNum = $zero where listNum = $row[0]";
+        $result2 = $mysqli->query($query2);
+    }
 
-mysqli_close($mysqli);
+    $query = "delete from data where num=$num";
+    $result = $mysqli->query($query);
+
+    unlink("/home/hw/www/download/".$_GET['file']); //DB 행 삭제 시 서버측 파일까지 삭제
+
+    echo "<script>alert('삭제를 완료했습니다.');</script>";
+    echo "<meta http-equiv='refresh' content='0.1; url=../main.php'>";
+
+    mysqli_close($mysqli);
config/select.php
--- config/select.php
+++ config/select.php
@@ -38,12 +38,12 @@
     $query = "select * from data where writer like '". "%" . $text . "%" ."' order by num desc limit $page_start, $list";
     $query2 = "select * from data where writer like '". "%" . $text . "%" ."' order by num desc";
 }else{
-    $query = "select * from data order by num desc limit $page_start, $list";      //MAX 7개 출력
+    $query = "select * from data order by listNum desc, depth asc limit $page_start, $list";      //MAX 7개 출력
     $query2 = "select * from data";
 }
 $result = $mysqli->query($query);    //전체 행 조회 for PRINT
 
-$resultCount = $mysqli->query($query2);    //전체 행 조회 for COUNT  cause LIMIT
+$resultCount = $mysqli->query($query2);    //전체 행 조회 for COUNT cause LIMIT
 $searchCount = $resultCount->num_rows;     // 검색시 나온 행 갯수 COUNT
 
 $total_page  = ceil($searchCount / $list);            //총 페이징 숫자 수
@@ -51,4 +51,4 @@
     $block_end = $total_page;
 }
 $total_block = ceil($total_page / $block_cnt);          //총 블록 수
-?>
+
main.php
--- main.php
+++ main.php
@@ -56,6 +56,7 @@
         #a {text-decoration:none; color: black; font-weight: bold;}
         #a:hover {background-color: tomato;}
         #selectNum {color: red; display: inline; }
+        #tableTitle {text-align: left;}
     </style>
 </head>
 <body>
@@ -95,8 +96,22 @@
                     ?>
                     <tbody>                                            <!-- 메인 컨텐츠 출력 -->
                     <tr>
-                        <td><?php echo $row['num']; ?></td>
-                        <td>
+                        <td><?php
+                                if($row['depth'] == 0){            /* 답글은 게시글 번호 출력 X */
+                                    echo $row['num'];
+                                }else{
+                                    null;
+                                }
+                            ?>
+                        </td>
+                        <td id="tableTitle">
+                            <?php
+                                if($row['depth'] > 0){
+                                    echo "<img height=1 alt='' width=" . $row['depth']*15 . ">└>";
+                                }else{
+                                    null;
+                                }
+                            ?>
                             <a id="a" href="./view.php?whereValue=<?php echo $row['num'];?>&cnt=<?php echo $row['view']+1;?>&option=<?php echo$option?>&searchText=<?php echo $text?>">     <!-- view.php -->
                                 <?php echo $row['title']; ?>
                             </a>
view.php
--- view.php
+++ view.php
@@ -81,6 +81,7 @@
         <br>첨부파일 : <a href="config/download.php?file=<?php echo $row['file']?>&whereValue=<?php echo $row['num']?>&option=<?php echo $_GET['option']?>&searchText=<?php echo $text?>"><?php echo $row['file'];?></a>
     </div>
     <div id="btnControl">
+        <button class="btnS" id="btnAnswer" onclick="location.href='answerWriteFront.php?whereValue=<?php echo $row['num'];?>&option=<?php echo $_GET['option']?>&searchText=<?php echo $text?>'">답글</button>
         <button class="btnS" id="btnUpdate" onclick="location.href='updateWrite.php?whereValue=<?php echo $row['num'];?>&option=<?php echo $_GET['option']?>&searchText=<?php echo $text?>'">수정</button>
         <button class="btnS" id="btnDelete" onclick="deleteCheck()">삭제</button>
         <button class="btnS" id="btnList" onclick="location.href='main.php?&option=<?php echo $_GET['option']?>&searchText=<?php echo $text?>'">목록</button>
Add a comment
List