[전체게시판] 각 기간별 게시물을 가져오는 함수
페이지 정보
작성자 운영자 작성일 25-05-02 21:37 조회 21 댓글 0본문
// 출력할 게시물 수
$rows = 10;
// 각 기간별 게시물을 가져오는 함수
function get_popular_posts($period, $rows) {
global $g5;
$today = date('Y-m-d');
switch($period) {
case 'daily':
$start_date = date('Y-m-d', strtotime('-3 day'));
break;
case 'weekly':
$start_date = date('Y-m-d', strtotime('-7 days'));
break;
case 'monthly':
$start_date = date('Y-m-d', strtotime('-30 days'));
break;
default:
$start_date = date('Y-m-d', strtotime('-3 day'));
}
$list = array();
// 모든 게시판 가져오기
$board_sql = "SELECT bo_table FROM {$g5['board_table']} WHERE bo_use_search = 1";
$board_result = sql_query($board_sql);
// 모든 게시판의 인기글을 통합하여 가져옴
$sql = " SELECT
a.bo_table,
a.wr_id,
a.wr_subject,
a.wr_comment,
a.wr_hit,
a.wr_datetime
FROM (";
$union_sql = array();
while ($board = sql_fetch_array($board_result)) {
$bo_table = $board['bo_table'];
// 게시판 테이블이 존재하는지 확인
$table_exists = sql_query("SHOW TABLES LIKE '{$g5['write_prefix']}{$bo_table}'", false);
if (sql_num_rows($table_exists) == 0) continue;
$union_sql[] = " SELECT
'{$bo_table}' as bo_table,
wr_id,
wr_subject,
wr_comment,
wr_hit,
wr_datetime
FROM {$g5['write_prefix']}{$bo_table}
WHERE wr_is_comment = 0
AND wr_datetime BETWEEN '{$start_date} 00:00:00' AND '{$today} 23:59:59'";
}
// UNION ALL 쿼리 생성
if (count($union_sql) > 0) {
$sql .= implode(" UNION ALL ", $union_sql);
$sql .= ") a ORDER BY a.wr_hit DESC, a.wr_comment DESC LIMIT 0, {$rows} ";
$result = sql_query($sql);
for ($i=0; $row = sql_fetch_array($result); $i++) {
$list[$i]['bo_table'] = $row['bo_table'];
$list[$i]['wr_id'] = $row['wr_id'];
$list[$i]['subject'] = $row['wr_subject'];
$list[$i]['comment_cnt'] = $row['wr_comment'];
$list[$i]['hit'] = $row['wr_hit'];
$list[$i]['datetime'] = $row['wr_datetime'];
// 게시판 제목 가져오기
$board_info = sql_fetch("SELECT bo_subject FROM {$g5['board_table']} WHERE bo_table = '{$row['bo_table']}'");
$list[$i]['bo_subject'] = $board_info['bo_subject'];
}
}
return $list;
}
관련링크
댓글목록 0
등록된 댓글이 없습니다.