최신글 공지글/일반글 랜덤 추출 (2018-07-06 수정) > 그누보드5 팁자료실

그누보드5 팁자료실

최신글 공지글/일반글 랜덤 추출 (2018-07-06 수정) 정보

최신글 공지글/일반글 랜덤 추출 (2018-07-06 수정)

본문

2017-06-08 기능에 문제가 확인되어 수정작업후 다시 올리겠습니다.
2017-06-12 수정 재등록
2018-05-23 적용사례를 추가
2018-07-06 랜덤추출에서 공지글 존재여부 체크로직에 심각한 오류가 발견되어 수정했습니다.


공지글만 추출하거나,  공지가 아닌 글을 체크하기 위해 코드를 만들었습니다.

1. 그누보드5 / lib / latest_noticeRand.lib.php  파일을 생성해서 아래 내용을 저장

<?php
if (!defined('_GNUBOARD_')) exit;

// 최신글 공지글 랜덤 추출
// $cache_time 캐시 갱신시간
function latest_noticeRand($skin_dir='', $bo_table, $rows=10, $subject_len=40, $cache_time=1, $options='')
{
    global $g5;

    if (!$skin_dir) $skin_dir = 'basic';

    if(preg_match('#^theme/(.+)$#', $skin_dir, $match)) {
        if (G5_IS_MOBILE) {
            $latest_skin_path = G5_THEME_MOBILE_PATH.'/'.G5_SKIN_DIR.'/latest/'.$match[1];
            if(!is_dir($latest_skin_path))
                $latest_skin_path = G5_THEME_PATH.'/'.G5_SKIN_DIR.'/latest/'.$match[1];
            $latest_skin_url = str_replace(G5_PATH, G5_URL, $latest_skin_path);
        } else {
            $latest_skin_path = G5_THEME_PATH.'/'.G5_SKIN_DIR.'/latest/'.$match[1];
            $latest_skin_url = str_replace(G5_PATH, G5_URL, $latest_skin_path);
        }
        $skin_dir = $match[1];
    } else {
        if(G5_IS_MOBILE) {
            $latest_skin_path = G5_MOBILE_PATH.'/'.G5_SKIN_DIR.'/latest/'.$skin_dir;
            $latest_skin_url  = G5_MOBILE_URL.'/'.G5_SKIN_DIR.'/latest/'.$skin_dir;
        } else {
            $latest_skin_path = G5_SKIN_PATH.'/latest/'.$skin_dir;
            $latest_skin_url  = G5_SKIN_URL.'/latest/'.$skin_dir;
        }
    }

    $list = array();

    $sql = " select * from {$g5['board_table']} where bo_table = '{$bo_table}' ";
    $board = sql_fetch($sql);
    $bo_subject = get_text($board['bo_subject']);

    $tmp_write_table = $g5['write_prefix'] . $bo_table; // 게시판 테이블 전체이름 

    $arr_notice = preg_replace("/\n/",',', trim($board['bo_notice'])); 
    if(!$arr_notice) $arr_notice=0; 

    $sql = " select * from {$tmp_write_table} where wr_id IN({$arr_notice}) and wr_is_comment = 0 order by rand() limit 0, {$rows} ";
    $result = sql_query($sql);
    
    for ($i=0; $row = sql_fetch_array($result); $i++) {
        $list[$i] = get_list($row, $board, $latest_skin_url, $subject_len);
    }

    if($cache_fwrite) {
        $handle = fopen($cache_file, 'w');
        $cache_content = "<?php\nif (!defined('_GNUBOARD_')) exit;\n\$bo_subject='".$bo_subject."';\n\$list=".var_export($list, true)."?>";
        fwrite($handle, $cache_content);
        fclose($handle);
    }

    ob_start();
    include $latest_skin_path.'/latest.skin.php';
    $content = ob_get_contents();
    ob_end_clean();

    return $content;
}


// 최신글 공지글 제외글 랜덤 추출
// $cache_time 캐시 갱신시간
function latest_NoNoticeRand($skin_dir='', $bo_table, $rows=10, $subject_len=40, $cache_time=1, $options='')
{
    //echo "latest_NoNoticeRand<br>";
    global $g5;

    if (!$skin_dir) $skin_dir = 'basic';

    if(preg_match('#^theme/(.+)$#', $skin_dir, $match)) {
        if (G5_IS_MOBILE) {
            $latest_skin_path = G5_THEME_MOBILE_PATH.'/'.G5_SKIN_DIR.'/latest/'.$match[1];
            if(!is_dir($latest_skin_path))
                $latest_skin_path = G5_THEME_PATH.'/'.G5_SKIN_DIR.'/latest/'.$match[1];
            $latest_skin_url = str_replace(G5_PATH, G5_URL, $latest_skin_path);
        } else {
            $latest_skin_path = G5_THEME_PATH.'/'.G5_SKIN_DIR.'/latest/'.$match[1];
            $latest_skin_url = str_replace(G5_PATH, G5_URL, $latest_skin_path);
        }
        $skin_dir = $match[1];
    } else {
        if(G5_IS_MOBILE) {
            $latest_skin_path = G5_MOBILE_PATH.'/'.G5_SKIN_DIR.'/latest/'.$skin_dir;
            $latest_skin_url  = G5_MOBILE_URL.'/'.G5_SKIN_DIR.'/latest/'.$skin_dir;
        } else {
            $latest_skin_path = G5_SKIN_PATH.'/latest/'.$skin_dir;
            $latest_skin_url  = G5_SKIN_URL.'/latest/'.$skin_dir;
        }
    }
    
    $list = array();
    
    $sql = " select * from {$g5['board_table']} where bo_table = '{$bo_table}' ";
    $board = sql_fetch($sql);

    // 답변글 출력제외 
    //$subqry = "&& wr_reply = ''";
    
    // 공지사항 출력제외 
    $arr_notice = preg_replace("/\n/",',', trim($board['bo_notice']));
    
    if($arr_notice) {  // 2018-07-06  해피정닷컴 수정
        $subqry = $subqry." && wr_id Not in ({$arr_notice}) ";
    }
    
    $bo_subject = get_text($board['bo_subject']);

    $tmp_write_table = $g5['write_prefix'] . $bo_table; // 게시판 테이블 전체이름

    $sql = " select * from {$tmp_write_table} where wr_is_comment = 0 {$subqry} order by rand() limit 0, {$rows} ";
    $result = sql_query($sql);
    
    for ($i=0; $row = sql_fetch_array($result); $i++) {
        $list[$i] = get_list($row, $board, $latest_skin_url, $subject_len);
    }

    if($cache_fwrite) {
        $handle = fopen($cache_file, 'w');
        $cache_content = "<?php\nif (!defined('_GNUBOARD_')) exit;\n\$bo_subject='".$bo_subject."';\n\$list=".var_export($list, true)."?>";
        fwrite($handle, $cache_content);
        fclose($handle);
    }

    ob_start();
    include $latest_skin_path.'/latest.skin.php';
    $content = ob_get_contents();
    ob_end_clean();

    return $content;
}
?>


2. 적용코드
최신글 - 공지글 포함 랜덤 추출
<?php include_once G5_LIB_PATH."/latest_noticeRand.lib.php"; ?>
<?php echo latest_noticeRand('basic', '게시판이름', 6, 40); ?>

최신글 - 공지글 제외 랜덤 추출
<?php include_once G5_LIB_PATH."/latest_noticeRand.lib.php"; ?>
<?php echo latest_NoNoticeRand('basic', '게시판이름', 6, 40); ?>


3. 적용샘플
http://www.happyjung.com/demo/latest_noticeRand/

 

추천
0

댓글 13개

공유 감사드립니다.

호출 방법이 궁금합니다.
<?php echo latest_NoNoticeRand('', freeboard, 10, 40, 1, ''); ?>

이게 맞는 건가요?
해보니 뜨긴 하는데 정상적으로 쓰는 방법이 아닌 것 같기도 하네요.

그리고 갱신 시간이 1분인지, 1시간인지 어떤 걸 가르키는지 잘 모르겠습니다 ㅠ
새로고침 할 때마다 랜덤으로 바뀌던데..
이미 사용중일지 모르겠지만 본문 업데이트 기념(?) 댓글 남깁니다.
사용법은 아래와 같습니다.  본문내용 업데이트 참고
<?php include_once G5_LIB_PATH."/latest_noticeRand.lib.php"; ?>
<?php echo latest_noticeRand('스킨이름', '게시판이름', 6, 40); ?>
yc5 용 쓰고 있는데요 우선 g5만 만지고 있는데
echo latest_NoNoticeRand('theme/pic_basic180522', 'facebook', 6, 40);

저는 이렇게 썼는데 아무것도 안나와서... 사용법이 잘못 됬나요?;;

기본값으로 불러들이면 잘되는데 알려주신 소스로 해보니 게시글이 없다는 ... ;;
코드 테스트 했는데 이상없이 추출됩니다.
본문 코드를 이용해서 아래와 같이 적용해봤습니다.
http://www.happyjung.com/demo/latest_noticeRand/
음... 무슨 문제인것인지... 저는 다시 해봐도 게시글이 없다고 나오네요 ;;
아예 파일을 별도로 저장 해서 last 파일을 인쿠르드 한담에 해도 마찬가지로 안나오네요 음...
공지글이 없는 게시판일 경우 아예 못 뽑아오는 오류 였네요 ;;;
공지글이 있을 경우에는 빼고 가져오지만 없을 경우 아예 싹다 안나오네요 ;;;

if 문을 추가해서 그냥 공지글이 숫자가 있으면 서브쿼리 실행
없으면 서브쿼리 뺀거 실행으로 수정 봤더니 잘되네요 아무쪼록 감사합니다 ^^
전체 2,411 |RSS
그누보드5 팁자료실 내용 검색

회원로그인

(주)에스아이알소프트 / 대표:홍석명 / (06211) 서울특별시 강남구 역삼동 707-34 한신인터밸리24 서관 1404호 / E-Mail: admin@sir.kr
사업자등록번호: 217-81-36347 / 통신판매업신고번호:2014-서울강남-02098호 / 개인정보보호책임자:김민섭(minsup@sir.kr)
© SIRSOFT