글쓰기 / 관리자
헤니l
헤니의 개발 공부
헤니l
전체 방문자
오늘
어제
  • 분류 전체보기 (100)
    • study (46)
      • JAVA (5)
      • PHP (24)
      • JS , jQuery (7)
      • 서버 (1)
      • 코딩테스트 (9)
    • 회사일기 (1)
    • 취준 (2)
    • 인천일보아카데미 교육과정 (51)
      • 인천일보아카데미 (51)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • JS
  • HTML공부
  • 날씨어플만들기
  • 인천일보아카데미
  • php게시판
  • 자바스크립트
  • PHP
  • 비전공자개발공부
  • php게시판리스트
  • 비전공개발자
  • 비전공개발공부
  • 코딩
  • 코딩공부
  • CSS
  • php계층형게시판
  • 개발공부
  • 게시판구현
  • HTML
  • 비전공코딩공부
  • php회원가입

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
헤니l

헤니의 개발 공부

study/PHP

[php] 계층형 게시판 답글쓰기

2022. 12. 29. 18:02

계층형 게시판의 로직을 이해했으면 답글을 써보자 

 

 

read.php

더보기
<button class="lbtn" onclick="location.href='/stairBoard/write_re.php?bno=<?=$bno?>&ref=<?=$row['ref']?>&step=<?=$row['step']?>&depth=<?=$row['depth']?>'">답글쓰기
</button>

글 읽는 페이지에 답글쓰기 버튼을 추가해줬다 

 

bno, ref,step,depth 컬럼들의 값을 넘겨주기 위해

 기존 write.php 파일이 아닌 write_re.php 파일을 새로 만들어줬다 

 

글쓰는 화면은 동일하기때문에 이걸 새로 만들지 않고 기존 write.php로  충분할 것 같기도 ,,?

 

 

write_re.php

<input type="hidden" name="bno" size=20 value="<?php echo $_GET['bno']?>">  
<input type="hidden" name="ref"  size=20 value="<?php echo $_GET['ref']?>">  
<input type="hidden" name="step"  size=20 value="<?php echo $_GET['step']?>">  
<input type="hidden" name="depth"  size=20 value="<?php echo $_GET['depth']?>">

write.php와 동일한 구조에 위의 값들을  넘겨주기 위한 input만 추가되었다 

타입을 hidden으로 줘서 사용자에게는 보이지 않게 해줬다 

 

 

 

write_ok.php

<?php

include 'index.php';
session_start();

// write에서 넘어온 데이터 저장 및 출력
$userid = $_SESSION['userid'];                 
$title = $_POST[title];               
$content = $_POST['content'];  
$date = date('Y-m-d H:i:s'); 
$bno = $_POST['bno'];

$ref = $_POST['ref'];
$step = $_POST['step'];
$depth = $_POST['depth'];


$URL = '/stairBoard/list.php';

echo "ref : " . $ref . "<br>";
echo "step : " . $step . "<br>";
echo "depth : " . $depth . "<br>";


// 원래는 ref의 max값에서 +1 을 하려고 했는데 그럴 필요가 없음!
// 새글 작성시 ref는 bno의 값과 동일하게 할 수 있도록 max값 찾기
$query2 = "select max(bno) from board";
$result2 = mysqli_query($connect,$query2);    //쿼리전송
$row = mysqli_fetch_array($result2);         // 배열로 저장

// bno의 max + 1 = ref
if($row[0]){ 
    $number = $row[0]+1;
}else{
     $number=1;
}

// 새글일 경우 
if(!$bno){
    $ref = $number;
    $step = 0;
    $depth = 0;    
  
}else{      
    /* 답글일 경우 
        ref : 부모글과 동일
        step : 부모글 ref +=1
        depth" 부모글(step) +=1 */
    // ref, step값이 계속 안나왔는데 submit한 값을 post로 안가져와서 그랬음 !!!!
    $query3 = "update board set step = step +1
                    where ref = $ref and step > $step";
    $result3 = mysqli_query($connect,$query3); 

    $step = $step + 1;
    $depth = $depth + 1; 
    $ref = $ref;
}

///insert문 통해 db에 form값 저장 
$query = "insert into board(userid,title,content,date,ref,step,depth)
            values('$userid','$title','$content','$date',$ref,$step,$depth)"; 
var_dump($query); 

$result = mysqli_query($connect,$query);    //쿼리전송
?>

<script>
    alert("글이 등록되었습니다.");
    //href:히스토리 기록 / replace:기록x, 뒤로가기 불가
    location.replace("<?php echo $URL?>");
</script>

ok.php는 새글 작성과 동일한 파일에서 구현했다 

 

1. POST로 값들을 다 가져온다

 

2. ref 값을 넣어줄때 bno와 동일하게 할 수 있도록 bno의 max값을 select한다 

// 새글 작성시 ref는 bno의 값과 동일하게 할 수 있도록 max값 찾기
$query2 = "select max(bno) from board";
$result2 = mysqli_query($connect,$query2);    //쿼리전송
$row = mysqli_fetch_array($result2);         // 배열로 저장

 

3. ref = bno의 max + 1 

if($row[0]){ 
    $number = $row[0]+1;
}else{
     $number=1;
}

 

 

4.

if(!$bno){
    $ref = $number;
    $step = 0;
    $depth = 0;    
  
}else{      
    $query3 = "update board set step = step +1
                    where ref = $ref and step > $step";
    $result3 = mysqli_query($connect,$query3); 

    $step = $step + 1;
    $depth = $depth + 1; 
    $ref = $ref;
 
}

if(새글이라면) { 

    ref = bno의 max + 1 

    step과 depth는 0 

}

step과 depth는 default값이 있어서 변수 지정 안해줘도 될 거라고 생각했는데  해줘야하더라 ! 

 

else (새글이 아닐경우 -> 답글일 경우 {

   업데이트문을 통해 step의 값을 바꿔준다   (글 리스트에서 최신 답글이 위로 올라오게 하기 위함 )

   그룹(ref)이 같고  step이 db의 step보다 클 경우 step을 1 증가시킨다

}

ref : 부모글과 동일
step : 부모글 step + 1
depth : 부모글(step)의 depth +1

 

5. insert문

$query = "insert into board(userid,title,content,date,ref,step,depth)
            values('$userid','$title','$content','$date',$ref,$step,$depth)";

 

 

 

아 정리하면서도 너무 헷갈린다 ....

이거는 직접 만들어보고 글 번호들 들어가는 거 봐야 이해가 간다 ! 

어렵고만요 ,, 

    'study/PHP' 카테고리의 다른 글
    • [PHP] 게시판 전체 정리
    • [PHP] 계층형 게시판 리스트, 답글 들여쓰기
    • [PHP] 계층형 게시판(답글) 로직
    • [PHP] 게시판 댓글 삭제
    헤니l
    헤니l

    티스토리툴바