카운트조정 실행파일?
본문
안녕 하세요
특정 게시판 1개
카운트조정 을 cron 으로 돌리려고 하는데
/adm/board_form_update.php 에서
복사 해서 파일 만들면 된다고 하길래
걍 복사만 하면 되는줄 알고
아래와 같이 파일을 만들엇는데
에러나고 안됩니다
고수님
실행되는 소스좀 부탁 드립니다
감사 합니다
<?php include_once('./common.php');
if (isset($_POST['proc_count'])) {
// 원글을 얻습니다.
//$sql = " select wr_id from {$g5['write_prefix']}{$bo_table} where wr_is_comment = 0 ";
$sql = " select a.wr_id, (count(b.wr_parent) - 1) as cnt from {$g5['write_prefix']}{$bo_table} a, {$g5['write_prefix']}{$bo_table} b where a.wr_id=b.wr_parent and a.wr_is_comment=0 group by a.wr_id ";
$result = sql_query($sql);
for ($i=0; $row=sql_fetch_array($result); $i++) {
/*
// 코멘트수를 얻습니다.
$sql2 = " select count(*) as cnt from {$g5['write_prefix']}$bo_table where wr_parent = '{$row['wr_id']}' and wr_is_comment = 1 ";
$row2 = sql_fetch($sql2);
*/
// 다운로드수를 얻습니다.
$row2 = sql_fetch(" select sum(bf_download) as download from {$g5['board_file_table']} where bo_table = '{$bo_table}' and wr_id = '{$row['wr_id']}' ");
sql_query(" update {$g5['write_prefix']}{$bo_table} set wr_comment = '{$row['cnt']}', as_download = '{$row2['download']}' where wr_id = '{$row['wr_id']}' ");
}
}
// 공지사항에는 등록되어 있지만 실제 존재하지 않는 글 아이디는 삭제합니다.
$bo_notice = "";
$lf = "";
if ($board['bo_notice']) {
$tmp_array = explode(",", $board['bo_notice']);
for ($i=0; $i<count($tmp_array); $i++) {
$tmp_wr_id = trim($tmp_array[$i]);
$row = sql_fetch(" select count(*) as cnt from {$g5['write_prefix']}{$bo_table} where wr_id = '{$tmp_wr_id}' ");
if ($row['cnt'])
{
$bo_notice .= $lf . $tmp_wr_id;
$lf = ",";
}
}
}
$sql = " update {$g5['board_table']}
set bo_notice = '{$bo_notice}',
bo_count_write = '{$bo_count_write}',
bo_count_comment = '{$bo_count_comment}',
{$sql_common}
where bo_table = '{$bo_table}' ";
sql_query($sql);
}
?>
답변 3
아래 코드를 lib/recount.lib.php 로 생성하시고
function recount($bo_table){
global $g5;
if(!sql_num_rows(sql_query(" show tables like '{$g5['write_prefix']}{$bo_table}' "))) {
return false;
}
// 게시판의 글 수
$sql = " select count(*) as cnt from {$g5['write_prefix']}{$bo_table} where wr_is_comment = 0 ";
$row = sql_fetch($sql);
$bo_count_write = $row['cnt'];
// 게시판의 코멘트 수
$sql = " select count(*) as cnt from {$g5['write_prefix']}{$bo_table} where wr_is_comment = 1 ";
$row = sql_fetch($sql);
$bo_count_comment = $row['cnt'];
// 글수 조정
/*
엔피씨님의 팁으로 교체합니다. 130308
http://sir.kr/g5_tiptech/27207
*/
$sql = " select a.wr_id, (count(b.wr_parent) - 1) as cnt from {$g5['write_prefix']}{$bo_table} a, {$g5['write_prefix']}{$bo_table} b where a.wr_id=b.wr_parent and a.wr_is_comment=0 group by a.wr_id ";
$result = sql_query($sql);
for ($i = 0; $row = sql_fetch_array($result); $i++) {
/*
// 코멘트수를 얻습니다.
$sql2 = " select count(*) as cnt from {$g5['write_prefix']}$bo_table where wr_parent = '{$row['wr_id']}' and wr_is_comment = 1 ";
$row2 = sql_fetch($sql2);
*/
sql_query(" update {$g5['write_prefix']}{$bo_table} set wr_comment = '{$row['cnt']}' where wr_id = '{$row['wr_id']}' ");
}
// 공지사항에는 등록되어 있지만 실제 존재하지 않는 글 아이디는 삭제합니다.
$bo_notice = "";
$lf = "";
if ($board['bo_notice']) {
$tmp_array = explode(",", $board['bo_notice']);
for ($i = 0; $i < count($tmp_array); $i++) {
$tmp_wr_id = trim($tmp_array[$i]);
$row = sql_fetch(" select count(*) as cnt from {$g5['write_prefix']}{$bo_table} where wr_id = '{$tmp_wr_id}' ");
if ($row['cnt']) {
$bo_notice .= $lf . $tmp_wr_id;
$lf = ",";
}
}
}
$sql = " update {$g5['board_table']}
set bo_notice = '{$bo_notice}',
bo_count_write = '{$bo_count_write}',
bo_count_comment = '{$bo_count_comment}'
where bo_table = '{$bo_table}' ";
sql_query($sql);
return true;
}
/skin/list.skin.php
파일 상단에
$tmp = sql_fetch(" select count(*) as cnt from {$g5['write_prefix']}{$bo_table} where wr_is_comment = 0 ");
if($tmp['cnt'] != $board['bo_count_write']){
include_once(G5_LIB_PATH."/recount.lib.php");
$recount = recount($bo_table);
}
특정 게시판 1개 카운트조정 을 cron 으로 돌리려고 하는데<---무슨 까닭으로 이런 터무니 없는 실행을
하려고 하는가요?
카운트가 맞지않게 되는 근본 원인을 찾아야 합니다
잘 된다는 답변의 코드에 대한 조언을 하자면 별로 좋지않은 방법입니다
하루에 게시물 수백개가 올라온다고 하였는데 목록을 볼때마다 그리고 페이지 이동마다 카운트 문제가 있는지 체크해야하니 불필요한 쿼리를 발생시키게 됩니다
그리고 삭제가 생긴 경우에는 게시물이 1천개면 1천번 쿼리를 실행 합니다
게시물이 많으면 디비의 트래픽이 상당히 상승하겠죠
삭제시 게시물 감소 코드를 넣으면 저렇게할 필요가 없는데 그게 어려운 것 같으니
이 소스를 사용하더라도 list스킨에 넣지말고 삭제코드가 있는 화일 삭제 코드 뒤에 넣으세요
불필요하니 삭제해야할 코드
--------------------------------------첫부분
function recount($bo_table){
global $g5;
if(!sql_num_rows(sql_query(" show tables like '{$g5['write_prefix']}{$bo_table}' "))) {
return false;
}
-------------------댓글만 삭제하는 경우가 없다면 불필요한 코드(게시물이 1천개면 1천번 실행되는 코드임)
$sql = " select a.wr_id, (count(b.wr_parent) - 1) as cnt from {$g5['write_prefix']}{$bo_table} a, {$g5['write_prefix']}{$bo_table} b where a.wr_id=b.wr_parent and a.wr_is_comment=0 group by a.wr_id ";
$result = sql_query($sql);
for ($i = 0; $row = sql_fetch_array($result); $i++) {
/*
// 코멘트수를 얻습니다.
$sql2 = " select count(*) as cnt from {$g5['write_prefix']}$bo_table where wr_parent = '{$row['wr_id']}' and wr_is_comment = 1 ";
$row2 = sql_fetch($sql2);
*/
sql_query(" update {$g5['write_prefix']}{$bo_table} set wr_comment = '{$row['cnt']}' where wr_id = '{$row['wr_id']}' ");
}
====================마지막 부분
return true;
}
=================
위 코드 일부는 관리자페이지에 있는 카운트 조정 소스인데 실행된다고 해서 아무 곳에 넣어서 사용하면 안됩니다
카운트 조정은 게시물을 phpmyadmin으로 삭제하거나
예상치 못한 오류로 카운트가 맞지않을 때 어쩌다 실행하는 것이지
비정상적인 삭제가 자주 발생하는 곳에서 상시적으로 실행해도 되는 코드가 아닙니다
게시물수가 많으면 게시물수만큼 update쿼리를 진행하므로 상당한 시간이 소요되고 디비 트래픽도 증가합니다