채택완료

함수 이렇게 만들면 되는지요?

2015-08-28 (금) 07:28:26 3,907

댓글중 내 댓글이 잇는경우 색상을 변경하는 함수를 만들엇는데 제대로 만든건지 모르겟네요..

마스터분들 보시기에는 한심할지모르지만 가르침좀 부탁합니다.

좀더 간편하게 만들방법이나 문제 지적요.

get_mydet($list[$i][wr_comment], $bo_table, $list[$i][wr_id]);

// 내댓글이 잇는지
function get_mydet($comment, $bo_table, $wr_id)
{
 global $g5, $member;
    
 $mydet= '';
 if ($comment) { //////////////////////////본인코멘트가 잇는경우
  
$sql = "select count(wr_id) cnt from $g5[write_prefix]$bo_table where wr_parent='$wr_id' and wr_is_comment=1 and mb_id='{$member[mb_id]}' ";
$cnt = sql_fetch($sql);

if ($cnt['cnt']>0) {
    $mydet= "<span class=\"det_my\" title=\"내가 작성한 댓글이 있음.\">".$comment."</span>";
}else{
 $mydet= "<span class=\"det\">".$comment."</span>";
}

 } //////////////////////////본인코멘트가 잇는경우

    return $mydet;
}​

|

답변 3개 / 댓글 2개

채택된 답변
+20 포인트

list.skin.php 상단에 위에 작성한 함수를 넣고,

Copy
$list = set_has_my_comment($list, $bo_table, $member['mb_id']);

이렇게 하면 $list[$i]['has_my_comment'] 가 설정됩니다. 즉, 내 댓글이 있는 글의 경우에는 'has_my_comment' 가 true 로 설정이 되죠. for 문 안에서 각 항목을 출력할 때, 사용하시면 됩니다.

히어1님 코드처럼 표현하자면..  

Copy
// list.skin.php 의 목록 출력 루틴 for($i=0; $i<count($list); $i++) {...       if($list[$i]['has_my_comment']) {            $mydet= "<span class=\"det_my\" title=\"내가 작성한 댓글이 있음.\">".​$list[$i]['comment_cnt']."</span>";     } else {          $mydet= "<span class=\"det\">".$list[$i]['comment_cnt']."</span>";     }  ...} // for

devdev 님 댓글처럼.. 리스트 항목 개수만큼 쿼리를 실행하게 해야 하는데 그것보다는 한번에 처리하는게 어떨까요..

Copy
function set_has_my_comment($list, $bo_table, $mb_id) {   $write_table = $GLOBALS['g5']['write_prefix'].$bo_table;   $wr_ids = array();   foreach($list as $row) {     array_push($wr_ids, $row['wr_id']);   }   // 게시판 목록의 wr_id 들을 콤마로 연결한 문자열로 만듬    $wr_ids_list = implode($wr_ids, ',');   $sql = "SELECT wr_id, wr_parent FROM $write_table WHERE wr_parent IN ($wr_ids_list) AND wr_is_comment=1 and mb_id='$mb_id' GROUP BY wr_parent";   $res = sql_query($sql);   $has_my_comment = array();   while($row = sql_fetch_array($res))   {     array_push($has_my_comment, $row['wr_parent']);   } for($i=0; $i<count($list); $i++)   {     if(in_array($list[$i]['wr_id'], $has_my_comment)) {       $list[$i]['has_my_comment'] = true;     } else {       $list[$i]['has_my_comment'] = false;     }   }   return $list; }    // 목록 항목마다 'has_my_comment' 설정   $list = set_has_my_comment($list, $bo_table, $member['mb_id']);   // 내 댓글이 있는 경우, 없는 경우.. 출력해봄  foreach($list as $row) {   if($row['has_my_comment']) echo $row['wr_subject'] . ' : has my comment <br/>';   else echo $row['wr_subject'] . ' : does not have my comment <br/>'; }
 

답변에 대한 댓글 1개

소스까지 직접 만들어주시고 정말 감사합니다.
제가 만든게 게시판 리스트,최신글등등에 써먹을거라 부하를 감안하더라도 사용이 불가피한데
만들어주신 소스는 제 수준으로는 활용을 어덯게 해야하는지 모르겟네요 ;;;

별 문제는 없어 보입니다.

다만.. 리스트에서 실행하면 목록마다 sql을 실행시키게 되니..

DB에 부하는 가겟죠. 

답변에 대한 댓글 1개

어차피 자신의 댓글이 잇는지 한번은 찾아야하기에 어쩔수 없어 보입니다 ㅡㅜ 답 감사합니다.

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