내가 생각했던 PHP 답글 로직

김석재·2023년 3월 20일
0

PHP

목록 보기
2/6

답글(reply.php)

<?PHP
include $_SERVER['DOCUMENT_ROOT']."/include/header.php";
?>
<?php
include_once $_SERVER['DOCUMENT_ROOT']. "/classes/board.class.php";

$boardHandler = new BOARD();
$row = $boardHandler->detailBoard();


$searchDepth = "select ec_depth from educommunity e where parents = '" . $row['parents'] . "' order by ec_depth desc limit 1";

$stmt = $dbconn->prepare($searchDepth);
$stmt->execute();
$depth = $stmt->fetch();
$numDepth1=$depth['ec_depth'];

$searchDe_Depth = "select ec_de_depth from educommunity e where parents = '" . $row['parents'] . "' and ec_depth = '" . $row['ec_depth'] . "' order by ec_de_depth desc limit 1";
$stmt = $dbconn->prepare($searchDe_Depth);
$stmt->execute();
$de_Depth = $stmt->fetch();
$numDepth2 = $de_Depth['ec_de_depth'];

$searchDe_De_Depth = "select ec_de_de_depth from educommunity e where parents = '" . $row['parents'] . "' and ec_depth = '" . $row['ec_depth'] . "' and ec_de_depth = '" . $row['ec_de_depth'] . "' order by ec_de_de_depth desc limit 1";
$stmt = $dbconn->prepare($searchDe_De_Depth);
$stmt->execute();
$de_de_Depth = $stmt->fetch();
$numDepth3 = $de_de_Depth['ec_de_de_depth'];


?>

<br>
<br>
<br>

<html>
<head>
    <script>
        function writeCheck(){
            let writeSub = document.getElementById('subject').value;
            let writeContent = document.getElementById('content').value;
            if (writeSub.length == 0){
                alert("제목을 입력하세요")
                return false;
            }else if (writeContent.length == 0){
                alert("내용을 입력하세요");
                return false;
            } else {
                return true;
            }
        }
    </script>
</head>
<body>
<h2>답글</h2>
<? if($row['ec_depth'] > 0 && $row['ec_de_depth'] > 0){
?>
        <h3>de_de_depth</h3>
    <form name="writeForm" action="/board/bExec.php" onsubmit="return writeCheck()" method="post">
        <input type="hidden" name="mode" value="write">
        <input type="hidden" name="parents" value="<?echo $row['parents']?>">
        <input type="hidden" name="depth" value="<?echo $row['ec_depth']?>">
        <input type="hidden" name="de_depth" value="<?echo $row['ec_de_depth']?>">
        <input type="hidden" name="de_de_depth" value="<?echo $row['ec_de_de_depth'] + 1?>">
        제목 : <input type="text" name="subject" id="subject" placeholder="제목을 입력하세요"><br>
        내용 : <textarea name="content" id="content" rows="4" cols="50" placeholder="내용을 입력하세요"></textarea>
        <input type="submit" value="글 등록">
    </form>
<?
}   else if($row['ec_depth'] > 0 && $row['ec_de_de_depth'] < 1){?>
        <h3>de_depth</h3>
    <form name="writeForm" action="/board/bExec.php" onsubmit="return writeCheck()" method="post">
        <input type="hidden" name="mode" value="write">
        <input type="hidden" name="parents" value="<?echo $row['parents']?>">
        <input type="hidden" name="depth" value="<?echo $row['ec_depth']?>">
        <input type="hidden" name="de_depth" value="<?echo $row['ec_de_depth'] + 1?>">
        <input type="hidden" name="de_de_depth" value="<?echo $row['ec_de_de_depth']?>">
        제목 : <input type="text" name="subject" id="subject" placeholder="제목을 입력하세요"><br>
        내용 : <textarea name="content" id="content" rows="4" cols="50" placeholder="내용을 입력하세요"></textarea>
        <input type="submit" value="글 등록">
    </form>
<?
}   else if(($row['ec_de_depth'] && $row['ec_de_de_depth']) < 1){?>
    <h3>depth</h3>
    <form name="writeForm" action="/board/bExec.php" onsubmit="return writeCheck()" method="post">
        <input type="hidden" name="mode" value="write">
        <input type="hidden" name="parents" value="<?echo $row['parents']?>">
        <input type="hidden" name="depth" value="<?echo $row['ec_depth']+ 1?>">
        <input type="hidden" name="de_depth" value="<?echo $row['ec_de_depth']?>">
        <input type="hidden" name="de_de_depth" value="<?echo $row['ec_de_de_depth']?>">
        제목 : <input type="text" name="subject" id="subject" placeholder="제목을 입력하세요"><br>
        내용 : <textarea name="content" id="content" rows="4" cols="50" placeholder="내용을 입력하세요"></textarea>
        <input type="submit" value="글 등록">
    </form>
<?
}?>
</body>
</html>

<?PHP
include $_SERVER['DOCUMENT_ROOT']."/include/footer.php";
?>

글 리스트, 글 작성 class(board.class.php)

<?php
include_once dirname(__FILE__)."/page.class.php";
class BOARD extends PAGE {

    public function getBoardList()
    {
        global $dbconn, $_GET;
        $pageNo = $_GET['pageNo'];

        $this->_columns  = array("ec_no","ec.em_no","ec_subject","ec_regdt", "em.em_id", "em.em_name", "parents","ec_depth","ec_de_depth","ec_de_de_depth");
        $this->_tables  = array("educommunity ec left join edumember em on ec.em_no = em.em_no");
        $this->_orderby = array("parents desc, ec_depth asc, ec_de_depth asc, ec_de_de_depth asc");
        $this->setListQuery($pageNo);

        $stmt = $dbconn->prepare($this->query);
        $stmt->execute();
        $rows = $stmt->fetchAll();

        return $rows;
    }

    public function boardWrite()
    {
        if($_SESSION) {
            global $dbconn;

            $sql = "insert into educommunity (ec_subject, ec_content, em_no, em_id, parents, ec_depth, ec_de_depth, ec_de_de_depth) values ('" . $_POST['subject'] . "','" . $_POST['content'] . "','" . $_SESSION['no'] . "', '" . $_SESSION['id'] . "', '" . $_POST['parents'] . "', '" . $_POST['depth'] . "', '" . $_POST['de_depth'] . "', '" . $_POST['de_de_depth'] . "')";
            pre($sql);

            $stmt = $dbconn->prepare($sql);
            $stmt->execute();
            $row = $stmt->fetch();

            alertHref("작성되었습니다.", "/board/board.php?pageNo=1");
            return $row;
        } else {
            alertHref("로그인하세요", "/member/login.php");
            exit;
        }
    }
}
?>

db

0개의 댓글