테스트 사이트 - 개발 중인 베타 버전입니다

게시판 카테고리 중복 체크 가능하게 하기

· 5년 전 · 7312 · 14

기존에 팁에 올라왔던게 있었는데

리스트에서는 어떻게 처리할지 고민하다가 제가 해결한 방법 올립니다.

 

ca_name 방식으로 불러오는것이 아닌 sca로 불러오도록 하였습니다.

 

991938377_1574728917.7518.jpg

 

 

1. 해당 게시판 스킨의 write.skin.php 파일 카테고리 부분을 아래와 같이 변경

[code]
<tr>

<th>카테고리</th>
<td>

<?php
    $arr = explode("|",$board['bo_category_list']);
    foreach($arr as $str) { ?>
        <input type="checkbox" name="chk_ca_name[]" value="<?php echo $str; ?>"> <?php echo $str; ?>
    <?php } ?>
      <script>
        var f = document.fwrite;
        var str=",<?php echo $write[ca_name]?>,";
        for (var i=0; i<f.length; i++) {
            if (f.elements[i].name == "chk_ca_name[]") {
                if (str.indexOf(','+f.elements[i].value+',')>=0) {
                    f.elements[i].checked = true;
                }
            }
        }
    </script>
</td>
</tr>

[/code]

 

 

2. write_update.head.skin.php 파일 생성

[code]
<?php
if (!defined('_GNUBOARD_')) exit; // 개별 페이지 접근 불가

$ca_name='';

foreach($_POST[chk_ca_name] as $var) {

 $ca_name.=",$var";

}

if (strlen($ca_name)) $ca_name=substr($ca_name,1);

?>

[/code]

 

 

3. bbs/list.php에서 아래부분(11번째줄쯤)을

[code]
    if ($sca=='')

        $category_option .= ' id="bo_cate_on"'; 
[/code]

 

아래와 같이 변경 (&& stx==''추가)

[code]
    if ($sca=='' && $stx=='') 

        $category_option .= ' id="bo_cate_on"';

[/code]

 

 

 

4. bbs/list.php에서 아래부분(17~27번째줄쯤)을

[code]
for ($i=0; $i<count($categories); $i++) {

            $category = trim($categories[$i]);
            if ($category=='') continue;
            $category_option .= '<li><a href="'.($category_href."&amp;sca=".urlencode($category)).'"';
            $category_msg = '';
            if ($category==$sca) { // 현재 선택된 카테고리라면
                $category_option .= ' id="bo_cate_on"';
                $category_msg = '<span class="sound_only">열린 분류 </span>';
            }
            $category_option .= '>'.$category_msg.$category.'</a></li>';
        }
[/code]

 

아래 소스로 교체해주세요. 

[code]
 if ($bo_table == "게시판아이디") {


        for ($i=0; $i<count($categories); $i++) {
                $category = trim($categories[$i]);
                if ($category=='') continue;
                $category_option .= '<li><a href="'.($category_href."&amp;sfl=ca_name%2C1&amp;&stx=".urlencode($category)).'&amp;sop=or"'; 
                $category_msg = '';
                if ($category==$stx) {
                    $category_option .= ' id="bo_cate_on"';
                    $category_msg = '<span class="sound_only">열린 분류</span>';
                }
                $category_option .= '>'.$category_msg.$category.'</a></li>';
        }
      } else {
          /* original */
          for ($i=0; $i<count($categories); $i++) {
            $category = trim($categories[$i]);
            if ($category=='') continue;
            $category_option .= '<li><a href="'.($category_href."&amp;sca=".urlencode($category)).'"';
            $category_msg = '';
            if ($category==$sca) { // 현재 선택된 카테고리라면
                $category_option .= ' id="bo_cate_on"';
                $category_msg = '<span class="sound_only">열린 분류 </span>';
            }
            $category_option .= '>'.$category_msg.$category.'</a></li>';
        }
           /* original */
      }
[/code]

 

 

 

5. bbs/write_update.php 에서

if($board['bo_use_category']) { } 위아래를 if ($bo_table !== "게시판아이디") { }로 감싸주세요.

 

[code]

if ($bo_table !== "게시판아이디") {  //추가

 

        /* original */

        if($board['bo_use_category']) {
       
            $ca_name = trim($_POST['ca_name']);
            if(!$ca_name) {
                $msg[] = '<strong>분류</strong>를 선택하세요.';
            } else {
                $categories = array_map('trim', explode("|", $board['bo_category_list'].($is_admin ? '|공지' : '')));
                if(!empty($categories) && !in_array($ca_name, $categories))
                    $msg[] = '분류를 올바르게 입력하세요.';
       
                if(empty($categories))
                    $ca_name = '';
            }
       
        } else {
            $ca_name = '';
       
        }
       /* original */
   
}  //추가


[/code]

 

해당게시판에서만 분류 선택을 안하더라도 경고창 안뜨게 했습니다.

체크박스로 바꾸니 값이 없는것처럼 받아들여서 일단은 해당 스킨에서는 분류가 필수값이 아니도록 설정했는데 일반 회원들이 글을 쓰게 하는 게시판의 경우에는 조금 더 손을 보셔야할것 같습니다.

댓글 작성

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

로그인하기

댓글 14개

1년 전

write_update.head.skin.php 

 

<?php
if (!defined('_GNUBOARD_')) exit; // 개별 페이지 접근 불가
$ca_name='';
foreach($_POST[chk_ca_name] as $var) {
 $ca_name.=",$var";
}
if (strlen($ca_name)) $ca_name=substr($ca_name,1);
?>
 

 

위 부분에 오류가 났습니다.

 

ChatGPT 답변을 받아서 처리하니 정상 작동하였습니다.

 

if (!defined('_GNUBOARD_')) exit; // Prevent direct access

$ca_name = ''; // Initialize the category name string

// Concatenate each selected category name with a comma
if (isset($_POST['chk_ca_name']) && is_array($_POST['chk_ca_name'])) {
    foreach($_POST['chk_ca_name'] as $var) {
        $ca_name .= ",$var";
    }
}

// Remove the leading comma if any categories were selected
if (strlen($ca_name)) {
    $ca_name = substr($ca_name, 1);
}
 

 

1년 전

write.skin.php.     chatGPT 수정분 

 

<?php
// Assuming $board['bo_category_list'] and $write['ca_name'] are properly defined earlier

// Explode the category list
$categories = explode("|", $board['bo_category_list']);

// Generate the checkboxes
foreach ($categories as $category) { ?>
    <div class="bo_w_ico write_div">
        <input type="checkbox" name="chk_ca_name[]" value="<?php echo htmlspecialchars($category, ENT_QUOTES, 'UTF-8'); ?>"> <?php echo htmlspecialchars($category, ENT_QUOTES, 'UTF-8'); ?>
    </div>
<?php } ?>

<script>
    // Get the form reference
    var f = document.forms['fwrite'];
    // Get the selected categories as a comma-separated string
    var selectedCategories = ",<?php echo htmlspecialchars($write['ca_name'], ENT_QUOTES, 'UTF-8'); ?>,";

    // Loop through the form elements
    for (var i = 0; i < f.elements.length; i++) {
        // Check if the element is a checkbox with the name 'chk_ca_name[]'
        if (f.elements[i].name == "chk_ca_name[]") {
            // Check if the checkbox value is in the selected categories string
            if (selectedCategories.indexOf(',' + f.elements[i].value + ',') >= 0) {
                f.elements[i].checked = true;
            }
        }
    }
</script>
 

2개월 전

@라이프게임 님 코드에서 label을 추가해서 글자 클릭해도 체크가 되도록 변형한 버전 입니다.

 

Gemini 수정분

 

<?php
// Explode the category list
$categories = explode("|", $board['bo_category_list']);
// Generate the checkboxes
foreach ($categories as $i => $category) {
    // 고유 ID를 생성합니다. (예: chk_ca_name_0, chk_ca_name_1)
    $checkbox_id = 'chk_ca_name_' . $i;
?>
    <div class="bo_w_ico write_div">
        <input type="checkbox" name="chk_ca_name[]" value="<?php echo htmlspecialchars($category, ENT_QUOTES, 'UTF-8'); ?>" id="<?php echo $checkbox_id; ?>">
        <!-- label의 'for' 속성을 체크박스의 ID와 일치시킵니다. -->
        <label for="<?php echo $checkbox_id; ?>">
            <?php echo htmlspecialchars($category, ENT_QUOTES, 'UTF-8'); ?>
        </label>
    </div>
<?php } ?>

<script>
    // Get the form reference
    var f = document.forms['fwrite'];
    // Get the selected categories as a comma-separated string
    var selectedCategories = ",<?php echo htmlspecialchars($write['ca_name'], ENT_QUOTES, 'UTF-8'); ?>,";

    // Loop through the form elements
    for (var i = 0; i < f.elements.length; i++) {
        // Check if the element is a checkbox with the name 'chk_ca_name[]'
        if (f.elements[i].name == "chk_ca_name[]") {
            // Check if the checkbox value is in the selected categories string
            if (selectedCategories.indexOf(',' + f.elements[i].value + ',') >= 0) {
                f.elements[i].checked = true;
            }
        }
    }
</script>

2개월 전

좋은 정보 감사합니다.

 

php 8.0 이상 부터 $write[ca_name] => $write['ca_name'] 이렇게 사용하지 않으면 에러가 나네요.

php 8.4버전인데 원글 방식 적용 후 '라이프게임'님 수정 대로 적용하니 잘되네요.

 

참고로 아래와 같이 카테고리를 출력하면 원래 카테고리가 쉼표(,)로 분류 될때 띄어쓰기가 안되어 있는데, 띄어쓰기가 적용됩니다.

 

Gemini 수정분

 

view.skin.php

원본

<?php echo $view['ca_name']; ?>

수정본

<?php
// 쉼표를 기준으로 문자열을 배열로 나눕니다.
$categories_array = explode(',', $view['ca_name']);
// 배열 요소를 '쉼표와 공백'으로 다시 연결합니다.
$formatted_categories = implode(', ', $categories_array);
// 변환된 문자열을 출력합니다.
echo $formatted_categories;
?>

 


list.skin.php

원본

<?php echo $list[$i]['ca_name'] ?>

수정본
<?php
// 쉼표를 기준으로 문자열을 배열로 나눕니다.
$categories_array = explode(',', $list[$i]['ca_name']);
// 배열 요소를 '쉼표와 공백'으로 다시 연결합니다.
$formatted_categories = implode(', ', $categories_array);
// 변환된 문자열을 출력합니다.
echo $formatted_categories;
?>

 

게시글 목록

번호 제목
24318
24317
24315
24309
24294
24293
24277
24262
24260
24253
24251
24236
24233
24228
24226
24221
24214
24203
24201
24199
24196
24195
24194
24192
24191
24187
24185
24183
24172
24168