게시판 - 댓글 목록

이태현·2025년 9월 18일

Web 개발

목록 보기
51/53
post-thumbnail

Comment Class

댓글 개수 증가


  // 댓글등록
  public function input($arr)
  {
    $sql = "INSERT INTO comment (pidx, id, content, create_at, ip) VALUES (
    :pidx, :id, :content, NOW(), :ip)";
    $stmt = $this->conn->prepare($sql);
    $params = [
      ':pidx' => $arr['pidx'],
      ':id' => $arr['ss_id'],
      ':content' => $arr['content'],
      ':ip' => $_SERVER['REMOTE_ADDR']
    ];
    $stmt->execute($params);

    $sql = "UPDATE board SET comment_cnt=comment_ent+1 WHERE idx=:idx";
    $stmt = $this->conn->prepare($sql);
    $params = [':idx' => $arr['pidx']];
    $stmt->execute($params);
  }

기존에 등록 부분에 댓글 개수를 증가시키는 부분을 추가하였습니다.

댓글 리스트

  // 댓글 목록
  public function list($pidx)
  {
    $sql = "SELECT * FROM comment WHERE pidx=:pidx";
    $stmt = $this->conn->prepare($sql);
    $params = [':pidx' => $pidx];
    $stmt->setFetchMode(PDO::FETCH_ASSOC);
    $stmt->execute($params);

    return $stmt->fetchAll();
  }

board_view.php - Main

// 댓글 목록
$comment = new Comment($db);
$commentRs = $comment->list($idx);

    <div class="mt-3">
      <table class="table">
        <?php foreach ($commentRs as $comRow) { ?>
          <tr>
            <td><?= $comRow['content'] ?></td>
            <td><?= $comRow['id'] ?></td>
            <td><?= $comRow['create_at'] ?></td>
          </tr>
        <?php } ?>
      </table>
    </div>

댓글 class에서 가져온 list 함수를 가지고 DB에 있는 댓글 table에서 글을 가져옵니다.

결과

마무리

다음 시간에는 댓글 삭제하는 부분을 해보겠습니다.

감사합니다.

profile
이해하고 분석하고 지배한다

0개의 댓글