댓글은 어디에 저장이 되나요~~ 채택완료

로그인한 회원의 댓글을 추출하여 리스트로 출력하고 싶은데 댓글을 어떤 테이블에 저장되는지 모르겠습니다

답변 4개

채택된 답변
+20 포인트

게시판 게시글 테이블에 똑같이 저장 됩니다.

wr_is_comment 값이 1이면 코멘트 0이면 게시글입니다.

thumb-978180634_1729210399.9012_730x198.png

로그인 후 평가할 수 있습니다

답변에 대한 댓글 1개

감사합니다

댓글을 작성하려면 로그인이 필요합니다.

마르스 컴퍼니님 고견 감사합니다~~

로그인하면 이리 저리 글읽다가 그동안 로그인한 회원이 쓴 글을 동태를 알 수 있지 않을까 해서 필요하지 않을까 해서 view 페이지에 넣으려고 했습니다  님의 의견을 읽고나니 마이페이지 등에 넣어야 할듯합니다

로그인 후 평가할 수 있습니다

댓글을 작성하려면 로그인이 필요합니다.

마르스컴퍼니님 제가 올린것도 좀 봐주세요~~ 로그인한 회원의 최근게시글과 댓글 뽑는 건데 view.skin.php 에 넣어서 출력하려고 했지만  게시판이 아닌 곳에서는 정상 출력되는데 view.skin.php 에서는 최신글과 댓글은 출력되는데 페이지에 오류가 생겨요

Copy
<?php

if (!defined("_GNUBOARD_")) exit; // 개별 페이지 접근 불가

global $member; // 로그인한 회원 정보

 

// 로그인한 회원의 ID

$logged_in_user_id = $member['mb_id'] ?? null; // 로그인한 경우에만 ID를 가져옴

 

// 전체 게시판에서 게시글과 댓글을 가져오기 위한 쿼리

$all_posts = [];

$all_comments = [];

 

// 로그인한 사용자가 있는 경우에만 게시글과 댓글을 가져옵니다.

if ($logged_in_user_id) {

// 모든 게시판 테이블을 조회하여 게시글을 가져옵니다.

$board_list = sql_query("SELECT * FROM {$g5['board_table']}"); // 게시판 목록을 가져옵니다.

while ($board = sql_fetch_array($board_list)) {

$board_name = $board['bo_table'];

 

// 게시글 쿼리

$post_query = "SELECT *, '$board_name' AS bo_table FROM {$g5['write_prefix']}{$board_name} WHERE mb_id = '$logged_in_user_id' AND wr_is_comment = 0 ORDER BY wr_datetime DESC LIMIT 10";

$post_result = sql_query($post_query);

while ($post = sql_fetch_array($post_result)) {

if (!empty($post['wr_subject']) && !empty($post['wr_datetime'])) { // 게시글 제목과 날짜가 있는 경우만 추가

$all_posts[] = $post;

}

}

 

// 댓글 쿼리 (wr_is_comment = 1인 경우)

$comment_query = "SELECT *, '$board_name' AS bo_table FROM {$g5['write_prefix']}{$board_name} WHERE mb_id = '$logged_in_user_id' AND wr_is_comment = 1 ORDER BY wr_datetime DESC LIMIT 10";

$comment_result = sql_query($comment_query);

while ($comment = sql_fetch_array($comment_result)) {

if (!empty($comment['wr_content']) && !empty($comment['wr_datetime'])) { // 댓글 내용과 날짜가 있는 경우만 추가

$all_comments[] = $comment;

}

}

}

}

else {

// 로그아웃 상태에서도 최근 게시글을 가져옵니다.

$board_list = sql_query("SELECT * FROM {$g5['board_table']}"); // 게시판 목록을 가져옵니다.

while ($board = sql_fetch_array($board_list)) {

$board_name = $board['bo_table'];

 

// 로그인 여부와 관계없이 최근 게시글을 가져옵니다.

$post_query = "SELECT *, '$board_name' AS bo_table FROM {$g5['write_prefix']}{$board_name} WHERE wr_is_comment = 0 ORDER BY wr_datetime DESC LIMIT 10";

$post_result = sql_query($post_query);

while ($post = sql_fetch_array($post_result)) {

if (!empty($post['wr_subject']) && !empty($post['wr_datetime'])) { // 게시글 제목과 날짜가 있는 경우만 추가

$all_posts[] = $post;

}

}

 

// 댓글 쿼리 (로그아웃 상태에서도 전체에서 최근 댓글을 가져옴)

$comment_query = "SELECT *, '$board_name' AS bo_table FROM {$g5['write_prefix']}{$board_name} WHERE wr_is_comment = 1 ORDER BY wr_datetime DESC LIMIT 10";

$comment_result = sql_query($comment_query);

while ($comment = sql_fetch_array($comment_result)) {

if (!empty($comment['wr_content']) && !empty($comment['wr_datetime'])) { // 댓글 내용과 날짜가 있는 경우만 추가

$all_comments[] = $comment;

}

}

}

}

?>

<!-- 게시글 출력 -->

<div id="lt_side">

<fieldset id="recent_posts">

<legend>최근 게시글</legend>

<div class="fw-bold fs-5 "> 최근 게시글

</div>

<ul>

<?php

// 최근 10개 게시글만 출력

$recent_posts = array_slice($all_posts, 0, 10);

if (empty($recent_posts)): // 게시글이 없는 경우

// echo '<li>최근 게시글이 없습니다.</li>';

else:

$post_count = 1; // 게시글 번호 초기화

foreach ($recent_posts as $post):

// 게시판 이름과 게시글 ID를 확인

$board_name = $post['bo_table']; // 게시판 이름

$post_id = $post['wr_id']; // 게시글 ID

$post_url = get_pretty_url($board_name, $post_id); // URL 생성

 

// 제목 길이 조절

$max_title_length = 50; // 최대 제목 길이 설정

$post_title = htmlspecialchars($post['wr_subject']);

if (strlen($post_title) > $max_title_length) {

$post_title = mb_substr($post_title, 0, $max_title_length) . '...'; // 길이 초과 시 '...' 추가

}

?>

<li>

<?php echo $post_count++; ?>. <a href="<?php echo $post_url; ?>"><?php echo $post_title; ?></a>

- <?php echo htmlspecialchars($post['wr_datetime']); ?>

</li>

<?php endforeach; ?>

<?php endif; ?>

</ul>

</fieldset>

</div>

<div id="lt_side">

<!-- 댓글 출력 -->

<fieldset id="recent_comments">

<legend>최근 댓글</legend>최근 댓글

<ul>

<?php

// 최근 10개 댓글만 출력

$recent_comments = array_slice($all_comments, 0, 10);

if (empty($recent_comments)): // 댓글이 없는 경우

echo '<li>최근 댓글이 없습니다.</li>';

else:

$comment_count = 1; // 댓글 번호 초기화

foreach ($recent_comments as $comment):

// 댓글이 작성된 게시글의 ID와 게시판 이름을 가져옵니다.

$post_id = $comment['wr_parent']; // 댓글이 달린 게시글의 ID

$board_name = $comment['bo_table']; // 게시판 이름

$post_url = get_pretty_url($board_name, $post_id); // URL 생성

 

// 댓글 내용 길이 조절

$max_length = 30; // 최대 길이 설정

$comment_content = htmlspecialchars($comment['wr_content']);

if (strlen($comment_content) > $max_length) {

$comment_content = mb_substr($comment_content, 0, $max_length) . '...'; // 길이 초과 시 '...' 추가

}

?>

<li>

<?php echo $comment_count++; ?>. <a href="<?php echo $post_url; ?>"><?php echo $comment_content; ?></a>

- <?php echo htmlspecialchars($comment['wr_datetime']); ?>

</li>

<?php endforeach; ?>

<?php endif; ?>

</ul>

</fieldset>

 

</div>
로그인 후 평가할 수 있습니다

답변에 대한 댓글 1개

글 작성자의 최근글/댓글을 보여주는 것인지,
로그인 유저의 최근글/댓글을 보여주는 것인지 명확히 해야 합니다.

view.skin.php 는 글 뷰 페이지입니다.
글 뷰페이지에서 (글을 읽고 있는) '로그인 유저'의 최근글/댓글을 보여주는 것은 이상해 보입니다.

글 작성자의 최근글/댓글을 보여주는 것이 자연스러워 보입니다.

* 글 작성자의 최근 게시글 및 댓글
[code]
<?php
if (!defined("_GNUBOARD_")) exit; // 개별 페이지 접근 불가

$author_id = $view['mb_id'];

// 최근 게시글 조회 (원글만)
$recent_posts_query = "
SELECT *
FROM {$g5['board_new_table']}
WHERE mb_id = '$author_id'
AND wr_id = wr_parent
ORDER BY bn_datetime DESC
LIMIT 10
";
$recent_posts_result = sql_query($recent_posts_query);

// 최근 댓글 조회
$recent_comments_query = "
SELECT *
FROM {$g5['board_new_table']}
WHERE mb_id = '$author_id'
AND wr_id <> wr_parent
ORDER BY bn_datetime DESC
LIMIT 10
";
$recent_comments_result = sql_query($recent_comments_query);
?>

<!-- 게시글 출력 -->
<div id="lt_side">
<fieldset id="recent_posts">
<legend>작성자의 최근 게시글</legend>
<div class="fw-bold fs-5">작성자의 최근 게시글</div>
<ul>
<?php if (sql_num_rows($recent_posts_result) == 0): ?>
<li>최근 게시글이 없습니다.</li>
<?php else: ?>
<?php for ($i = 1; $row = sql_fetch_array($recent_posts_result); $i++):
$post_url = get_pretty_url($row['bo_table'], $row['wr_id']);
$post_info = sql_fetch(" SELECT wr_subject FROM {$g5['write_prefix']}{$row['bo_table']} WHERE wr_id = '{$row['wr_id']}' ");
$post_title = $post_info['wr_subject'];
if (mb_strlen($post_title) > 50) {
$post_title = mb_substr($post_title, 0, 50) . '...';
}
?>
<li><?php echo $i; ?>. <a href="<?php echo $post_url; ?>"><?php echo htmlspecialchars($post_title); ?></a> - <?php echo $row['bn_datetime']; ?></li>
<?php endfor; ?>
<?php endif; ?>
</ul>
</fieldset>
</div>

<!-- 댓글 출력 -->
<div id="lt_side">
<fieldset id="recent_comments">
<legend>작성자의 최근 댓글</legend>
<div class="fw-bold fs-5">작성자의 최근 댓글</div>
<ul>
<?php if (sql_num_rows($recent_comments_result) == 0): ?>
<li>최근 댓글이 없습니다.</li>
<?php else: ?>
<?php for ($i = 1; $row = sql_fetch_array($recent_comments_result); $i++):
$post_url = get_pretty_url($row['bo_table'], $row['wr_parent']);
$comment_info = sql_fetch(" SELECT wr_content FROM {$g5['write_prefix']}{$row['bo_table']} WHERE wr_id = '{$row['wr_id']}' ");
$comment_content = $comment_info['wr_content'];
if (mb_strlen($comment_content) > 30) {
$comment_content = mb_substr($comment_content, 0, 30) . '...';
}
?>
<li><?php echo $i; ?>. <a href="<?php echo $post_url; ?>"><?php echo htmlspecialchars($comment_content); ?></a> - <?php echo $row['bn_datetime']; ?></li>
<?php endfor; ?>
<?php endif; ?>
</ul>
</fieldset>
</div>
[/code]

댓글을 작성하려면 로그인이 필요합니다.

그누보드5에서는 댓글이 g5_write_[게시판명] 테이블에 저장됩니다. 여기서 '게시판명'은 실제 게시판의 테이블명입니다.

그누보드5에서 로그인한 회원의 전체 게시판 댓글을 추출하여 리스트로 출력하는 샘플 코드

Copy
<?php
include_once('./_common.php');

// 로그인 체크
if (!$is_member) {
    alert('로그인 후 이용해 주세요.', G5_BBS_URL.'/login.php');
}

$mb_id = $member['mb_id']; // 로그인한 회원의 ID

// 페이지네이션 설정
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
$items_per_page = 20; // 한 페이지에 표시할 댓글 수
$start = ($page - 1) * $items_per_page;

// 전체 댓글 수 조회
$total_count = 0;
$sql = " SELECT COUNT(*) AS cnt FROM {$g5['board_new_table']}
         WHERE mb_id = '{$mb_id}' AND wr_id <> wr_parent ";
$row = sql_fetch($sql);
$total_count = $row['cnt'];

// 댓글 목록 조회 (board_new 테이블과 board 테이블 조인)
$sql = " SELECT bn.*, bo.bo_subject
         FROM {$g5['board_new_table']} AS bn
         LEFT JOIN {$g5['board_table']} AS bo ON bo.bo_table = bn.bo_table
         WHERE bn.mb_id = '{$mb_id}' AND bn.wr_id <> bn.wr_parent
         ORDER BY bn.bn_id DESC
         LIMIT {$start}, {$items_per_page} ";
$result = sql_query($sql);

// 페이지 제목 설정
$g5['title'] = '내 댓글 목록';
include_once(G5_PATH.'/head.php');
?>

<div id="my_comment_list" class="new_win">
    <h1><?php echo $g5['title'] ?></h1>
    <ul>
    <?php
    for ($i=0; $row=sql_fetch_array($result); $i++) {
        $bo_table = $row['bo_table'];
        $wr_id = $row['wr_id'];
        
        // 해당 게시판 테이블에서 댓글 정보 조회
        $write_table = $g5['write_prefix'] . $bo_table;
        $sql = " SELECT * FROM {$write_table} WHERE wr_id = '{$wr_id}' ";
        $write = sql_fetch($sql);
        
        if (!$write) {
            continue; // 댓글이 삭제되었거나 존재하지 않는 경우 스킵
        }

        $comment = get_text(cut_str($write['wr_content'], 100));
        $href = get_pretty_url($bo_table, $write['wr_parent'], '#c_'.$wr_id);
    ?>
        <li>
            <div class="bo_subject"><?php echo $row['bo_subject'] ?></div>
            <div class="comment_content">
                <a href="<?php echo $href ?>"><?php echo $comment ?></a>
            </div>
            <div class="datetime"><?php echo $write['wr_datetime'] ?></div>
        </li>
    <?php } ?>
    <?php if ($i == 0) echo '<li class="empty_list">댓글이 없습니다.</li>'; ?>
    </ul>

    <?php echo get_paging(G5_IS_MOBILE ? $config['cf_mobile_pages'] : $config['cf_write_pages'], $page, ceil($total_count / $items_per_page), $_SERVER['SCRIPT_NAME'].'?'.$qstr.'&amp;page='); ?>
</div>

<?php
include_once(G5_PATH.'/tail.php');
?>
로그인 후 평가할 수 있습니다

답변에 대한 댓글 1개

정말 정말 감사합니다~` 채택을 할 수 없어서 죄송합니다

댓글을 작성하려면 로그인이 필요합니다.

답변을 작성하려면 로그인이 필요합니다.

로그인
🐛 버그신고