로그인회원의 전체게시글 뽑는 소스인데 페이징이 안되네요 채택완료

로그인회원의 전체게시글 뽑는 소스인데 페이징이 안되서 이용할 수가 없습니다. 아래 페이지처리부분을 도와 주세요

Copy
<?php

include_once('./_common.php'); // 공통 파일 포함

 

// 로그인 체크

if (!$is_member) {

alert('로그인 후 이용해 주세요.', G5_BBS_URL.'/login.php');

}

 

// 로그인한 회원의 ID

$logged_in_user_id = $member['mb_id']; // 실제 로그인한 회원 ID로 변경

 

// 게시판 정보 가져오기

$board_list = sql_query("SELECT bo_table FROM g5_board");

$total_posts = [];

$total_count = 0;

 

// 각 게시판에서 로그인한 회원의 글 개수 추출

foreach ($board_list as $board) {

$table_name = 'g5_write_' . $board['bo_table'];

$count_result = sql_query("SELECT COUNT(*) as cnt FROM $table_name WHERE mb_id = '$logged_in_user_id'");

if ($count_result) {

$count_row = mysqli_fetch_assoc($count_result);

$count = $count_row['cnt'];

if ($count > 0) {

$total_posts[$board['bo_table']] = $count;

$total_count += $count;

}

}

}

 

// 페이징 처리 변수 설정

$limit = 10; // 페이지당 표시할 게시글 수

$page = isset($_GET['page']) ? (int)$_GET['page'] : 1; // 현재 페이지

$offset = ($page - 1) * $limit; // SQL 쿼리의 OFFSET 값

 

// 전체 게시글 목록 조회 (LIMIT과 OFFSET을 사용)

$all_posts = [];

foreach ($board_list as $board) {

$table_name = 'g5_write_' . $board['bo_table'];

$result = sql_query("SELECT * FROM $table_name WHERE mb_id = '$logged_in_user_id' LIMIT $limit OFFSET $offset");

if ($result) {

while ($row = mysqli_fetch_assoc($result)) {

$all_posts[] = $row; // 게시글을 배열에 추가

}

} else {

echo "게시글 조회 오류: " . mysqli_error($conn); // 오류 메시지 출력

}

}

 

// 총 페이지 수 계산

$total_pages = ceil($total_count / $limit);

 

// 결과 출력

echo "<h2>로그인한 회원의 전체 게시글</h2>";

if (empty($all_posts)) {

echo "<p>게시글이 없습니다.</p>"; // 게시글이 없을 경우 메시지 출력

} else {

foreach ($all_posts as $post) {

// 제목 자르기

$subject = strip_tags($post['wr_subject']); // HTML 태그 제거

$subject_length = 20; // 제목 자르기 길이

// 제목 길이 확인 및 자르기

if (mb_strlen($subject, 'UTF-8') > $subject_length) {

$subject = mb_substr($subject, 0, $subject_length, 'UTF-8') . "...";

}

 

// 본문 자르기

$content = strip_tags($post['wr_content']); // HTML 태그 제거

$content_length = 100; // 본문 자르기 길이

// 본문 길이 확인 및 자르기

if (mb_strlen($content, 'UTF-8') > $content_length) {

$content = mb_substr($content, 0, $content_length, 'UTF-8') . "...";

}

// 결과 출력

echo "<div>";

echo "<h3>$subject</h3>";

echo "<p>$content</p>";

echo "<p>작성일: {$post['wr_datetime']}</p>";

echo "</div>";

}

}

 

// 페이징 처리 출력

// 설정

$posts_per_page = 10; // 페이지당 게시글 수

$current_page = isset($_GET['page']) ? (int)$_GET['page'] : 1; // 현재 페이지

$offset = ($current_page - 1) * $posts_per_page; // 오프셋 계산

 

// 게시글 가져오기

$sql = "SELECT * FROM {$g4['board_table']} WHERE wr_id = '$current_user_id' ORDER BY wr_num DESC LIMIT $offset, $posts_per_page";

$result = sql_query($sql);

$posts = array();

while ($row = sql_fetch_array($result)) {

$posts[] = $row;

}

 

// 총 게시글 수

$sql = "SELECT COUNT(*) AS cnt FROM {$g4['board_table']} WHERE wr_id = '$current_user_id'";

$result = sql_query($sql);

$row = sql_fetch_array($result);

$total_posts = $row['cnt'];

$total_pages = ceil($total_posts / $posts_per_page); // 총 페이지 수

 

// 페이징 링크 생성

$pagination = '';

for ($i = 1; $i <= $total_pages; $i++) {

if ($i == $current_page) {

$pagination .= '<span>' . $i . '</span>';

} else {

$pagination .= '<a href="?page=' . $i . '">' . $i . '</a>';

}

}

 

// 게시글 출력

foreach ($posts as $post) {

// 게시글 내용 출력

echo '<h2>' . $post['wr_subject'] . '</h2>';

echo $post['wr_content'];

}

 

// 페이징 링크 출력

echo $pagination;

 

?>

답변 2개

채택된 답변
+20 포인트

아래의 코드를 한번 참고를 해보세요.

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

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

$logged_in_user_id = $member['mb_id'];

// 페이징 처리 변수 설정
$limit = 10; // 페이지당 표시할 게시글 수
$page = isset($_GET['page']) ? max(1, (int)$_GET['page']) : 1; // 현재 페이지
$offset = ($page - 1) * $limit; // SQL 쿼리의 OFFSET 값

// 전체 게시글 수 계산
$total_count = 0;
$board_list = sql_query("SELECT bo_table FROM {$g5['board_table']}");
while ($board = sql_fetch_array($board_list)) {
    $table_name = 'g5_write_' . $board['bo_table'];
    $count_result = sql_fetch("SELECT COUNT(*) as cnt FROM $table_name WHERE mb_id = '$logged_in_user_id'");
    $total_count += $count_result['cnt'];
}

// 전체 게시글 목록 조회 (LIMIT과 OFFSET을 사용)
$all_posts = array();
$board_list = sql_query("SELECT bo_table FROM {$g5['board_table']}");
while ($board = sql_fetch_array($board_list)) {
    $table_name = 'g5_write_' . $board['bo_table'];
    $result = sql_query("
        SELECT *, '$table_name' AS board_table 
        FROM $table_name 
        WHERE mb_id = '$logged_in_user_id' 
        ORDER BY wr_datetime DESC
    ");
    while ($row = sql_fetch_array($result)) {
        $all_posts[] = $row;
    }
}

// 게시글 정렬 및 페이징 적용
usort($all_posts, function($a, $b) {
    return strtotime($b['wr_datetime']) - strtotime($a['wr_datetime']);
});
$paged_posts = array_slice($all_posts, $offset, $limit);

// 총 페이지 수 계산
$total_pages = ceil($total_count / $limit);

// 결과 출력
echo "<h2>로그인한 회원의 전체 게시글 ($total_count)</h2>";
if (empty($paged_posts)) {
    echo "<p>게시글이 없습니다.</p>";
} else {
    foreach ($paged_posts as $post) {
        $subject = strip_tags($post['wr_subject']);
        $subject = mb_strlen($subject) > 20 ? mb_substr($subject, 0, 20, 'UTF-8')."..." : $subject;
        
        $content = strip_tags($post['wr_content']);
        $content = mb_strlen($content) > 100 ? mb_substr($content, 0, 100, 'UTF-8')."..." : $content;
        
        echo "<div>";
        echo "<h3>$subject</h3>";
        echo "<p>$content</p>";
        echo "<p>작성일: {$post['wr_datetime']}</p>";
        echo "<p>게시판: {$post['board_table']}</p>";
        echo "</div>";
    }
}

// 페이징 링크 생성
$start_page = max(1, $page - 2);
$end_page = min($total_pages, $page + 2);

echo '<div class="pagination">';
if ($page > 1) {
    echo '<a href="?page=1">&laquo; 처음</a> ';
    echo '<a href="?page='.($page-1).'">&lsaquo; 이전</a> ';
}

for ($i = $start_page; $i <= $end_page; $i++) {
    if ($i == $page) {
        echo '<span class="current">'.$i.'</span> ';
    } else {
        echo '<a href="?page='.$i.'">'.$i.'</a> ';
    }
}

if ($page < $total_pages) {
    echo '<a href="?page='.($page+1).'">다음 &rsaquo;</a> ';
    echo '<a href="?page='.$total_pages.'">마지막 &raquo;</a>';
}
echo '</div>';

?>

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

답변에 대한 댓글 1개

감사합니다~ 덕분에 잘해결 하였습니다~ 아래와 같이 코드를 완성하였습니다~ 한번 보시고 조언부탁드립니다

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

로그인한 회원의 전체 게시글을 뽑는 소스입니다

Copy
<?php

include_once('./_common.php');

 

// 로그인 체크

if (!$is_member) {

alert('로그인 후 이용해 주세요.', G5_BBS_URL.'/login.php');

}

 

$logged_in_user_id = $member['mb_id'];

 

// 페이징 처리 변수 설정

$limit = 5; // 페이지당 표시할 게시글 수

$page = isset($_GET['page']) ? max(1, (int)$_GET['page']) : 1; // 현재 페이지

$offset = ($page - 1) * $limit; // SQL 쿼리의 OFFSET 값

 

// 전체 게시글 수 계산

$total_count = 0;

$board_list = sql_query("SELECT bo_table FROM {$g5['board_table']}");

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

$table_name = 'g5_write_' . $board['bo_table'];

$count_result = sql_fetch("SELECT COUNT(*) as cnt FROM $table_name WHERE mb_id = '$logged_in_user_id'");

$total_count += $count_result['cnt'];

}

 

// 전체 게시글 목록 조회 (LIMIT과 OFFSET을 사용)

$all_posts = array();

$board_list = sql_query("SELECT bo_table FROM {$g5['board_table']}");

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

$table_name = 'g5_write_' . $board['bo_table'];

$result = sql_query("

SELECT *, '$table_name' AS board_table

FROM $table_name

WHERE mb_id = '$logged_in_user_id'

ORDER BY wr_datetime DESC

");

while ($row = sql_fetch_array($result)) {

$all_posts[] = $row;

}

}

 

// 게시글 정렬 및 페이징 적용

usort($all_posts, function($a, $b) {

return strtotime($b['wr_datetime']) - strtotime($a['wr_datetime']);

});

$paged_posts = array_slice($all_posts, $offset, $limit);

 

// 총 페이지 수 계산

$total_pages = ceil($total_count / $limit);

 

// 결과 출력

echo "<h2>로그인한 회원의 전체 게시글 ($total_count)</h2>";

if (empty($paged_posts)) {

echo "<p>게시글이 없습니다.</p>";

} else {

foreach ($paged_posts as $post) {

$subject = strip_tags($post['wr_subject']);

$subject = mb_strlen($subject) > 20 ? mb_substr($subject, 0, 20, 'UTF-8')."..." : $subject;

 

// 게시물 링크 추가

$post_link = "board.php?bo_table=".str_replace('g5_write_', '', $post['board_table'])."&wr_id={$post['wr_id']}"; // 게시물 보기 링크

$subject_link = "<a href=\"$post_link\">$subject</a>"; // 제목을 링크로 감싸기

$content = strip_tags($post['wr_content']);

$content = mb_strlen($content) > 100 ? mb_substr($content, 0, 100, 'UTF-8')."..." : $content;

echo "<div>";

//echo "<h3>$subject</h3>";

echo "<h3>$subject_link</h3>"; // 수정된 부분

// 게시판 이름에서 'g5_write_' 제거

$board_name = str_replace('g5_write_', '', $post['board_table']);

echo "<p>$content</p>";

echo "<p>작성일: {$post['wr_datetime']}</p>";

echo "<p>게시판: $board_name</p>"; // 수정된 부분

echo "</div>";

}

}

 

// 페이징 링크 생성

$start_page = max(1, $page - 2);

$end_page = min($total_pages, $page + 2);

 

echo '<div class="pagination">';

if ($page > 1) {

echo '<a href="board.php?page=1&bo_table='.str_replace('g5_write_', '', $post['board_table']).'">« 처음</a> ';

echo '<a href="board.php?page='.($page-1).'&bo_table='.str_replace('g5_write_', '', $post['board_table']).'">‹ 이전</a> ';

}

 

for ($i = $start_page; $i <= $end_page; $i++) {

if ($i == $page) {

echo '<span class="current">'.$i.'</span> ';

} else {

echo '<a href="board.php?page='.$i.'&bo_table='.str_replace('g5_write_', '', $post['board_table']).'">'.$i.'</a> ';

}

}

 

if ($page < $total_pages) {

echo '<a href="board.php?page='.($page+1).'&bo_table='.str_replace('g5_write_', '', $post['board_table']).'">다음 ›</a> ';

echo '<a href="board.php?page='.$total_pages.'&bo_table='.str_replace('g5_write_', '', $post['board_table']).'">마지막 »</a>';

}

echo '</div>';

 

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

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

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

로그인
🐛 버그신고