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

특정 카테고리만 추출하여 최신글 뽑는데 어찌해야할지

스킨자료실에 특정 카테고리만 뽑는 스킨이 있어서 적용해 보니 잘 됩니다.

허나 A,B,C,D,E 카테고리중 딱 한개만 지정 할 수 있더라구요

A, B 이렇게 2개를 지정해서 뽑을려면 어찌 해야 할까요?

아래는 스킨에 있는 내용입니다.

최신글 스킨에는 변함이 없고 단지 아래 내용을 인클루드 해서 사용합니다.

최신글 카테고리로 뽑는거 메인에 출력 방법은

<?
include_once("$g4[path]/skin/latest/123cate/inc_lib.php");
echo latest_cate('123cate', 'inchun', 5, 15, "카테고리명");

?>

이렇게 사용하고 있구요

아래는 inc_lib.php 파일 내용 입니다.


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

function latest_cate($skin_dir="", $bo_table, $rows=10, $subject_len=40, $ca_name="")
{
global $g4;

if ($skin_dir)
$latest_skin_path = "$g4[path]/skin/latest/$skin_dir";
else
$latest_skin_path = "$g4[path]/skin/latest/basic";

$list = array();

$sql = " select * from $g4[board_table] where bo_table = '$bo_table'";
$board = sql_fetch($sql);

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

if ($ca_name)
$sql = " select * from $tmp_write_table where ca_name = '$ca_name' and wr_is_comment = 0 order by wr_num limit 0, $rows ";
else
$sql = " select * from $tmp_write_table where wr_is_comment = 0 order by wr_num limit 0, $rows ";

$result = sql_query($sql);
for ($i=0; $row = sql_fetch_array($result); $i++)
$list[$i] = get_list($row, $board, $latest_skin_path, $subject_len);

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

return $content;
}

?>

댓글 작성

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

로그인하기

댓글 6개

ca_name을 배열로 받으셔서

$sql 부분중

ca_name = '$ca_name'을

( ca_name = '".implode("' or ca_name = '", $ca_name)."' )

처럼 재 가공 해보시는것도 좋은 방법 같네요.

원칙은 쿼리부분을 쿼리 작성 부분에 삽입보다는 위에서 가공하시는게 좋습니다.
답변 감사합니다. 그런데 초보라서 정확하게 어떻게 소스를 수정해야 할지 모르겠습니다.

알려주신대로

ca_name = '$ca_name'을

( ca_name = '".implode("' or ca_name = '", $ca_name)."' )

이렇게 수정하였으나 에러가 납니다.

그리고 또한

<?
include_once("$g4[path]/skin/latest/123cate/inc_lib.php");
echo latest_cate('123cate', 'inchun', 5, 15, "카테고리명");

?>

최신글 뽑을때 위에서 카테고리명 이 부분은 어떻게 해야 하는지요

부탁드리겠습니다.
제가 말씀드린거처럼 수정시 기존꺼는 에러가 발생될수 있습니다.

말로 설명하기 어려움이 있네요.

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

function latest_cate($skin_dir="", $bo_table, $rows=10, $subject_len=40, $ca_name="")
{
global $g4;

$ca_name_sql = "";
if($ca_name){
$ca_name = explode("|", $ca_name);
$ca_name_sql = " and ( ";
for($i = 0 ; $i < count($ca_name); $i++){
if($i !=0) $ca_name_sql .= " or ";
$ca_name_sql .= " ca_name = '$ca_name[$i]' ";
}
$ca_name_sql = " ) ";
}

if ($skin_dir)
$latest_skin_path = "$g4[path]/skin/latest/$skin_dir";
else
$latest_skin_path = "$g4[path]/skin/latest/basic";

$list = array();

$sql = " select * from $g4[board_table] where bo_table = '$bo_table'";
$board = sql_fetch($sql);

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

if ($ca_name)
$sql = " select * from $tmp_write_table where wr_is_comment = 0 $ca_name_sql order by wr_num limit 0, $rows ";
else
$sql = " select * from $tmp_write_table where wr_is_comment = 0 order by wr_num limit 0, $rows ";

$result = sql_query($sql);
for ($i=0; $row = sql_fetch_array($result); $i++)
$list[$i] = get_list($row, $board, $latest_skin_path, $subject_len);

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

return $content;
}

?>

위 코드 테스트 않해본상태라 오타가 있을수 있습니다. 체크 하시기 바라구요.
좀 더 꼼꼼하게 하다보니 약간 길어진 면이 있네요;;
사용시는

echo latest_cate('123cate', 'inchun', 5, 15, "A|B|C");
select * from g4_write_inchun where wr_is_comment = 0 ) order by wr_num limit 0, 5

1064 : You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near ') order by wr_num limit 0, 5' at line 1

error file : /index.htm


이런 에러가 뜹니다.
$ca_name_sql = "";
if($ca_name){
$ca_name = explode("|", $ca_name);
$ca_name_sql = " and ( ";
for($i = 0 ; $i < count($ca_name); $i++){
if($i !=0) $ca_name_sql .= " or ";
$ca_name_sql .= " ca_name = '$ca_name[$i]' ";
}
$ca_name_sql .= " ) ";
}

. 한개가 빠졌군요;
완벽합니다.
고맙습니다.

게시글 목록

번호 제목
284508
284499
284492
284490
284484
284481
284478
284476
284474
284472
284470
284458
284457
284454
284453
284447
284446
284444
284441
284440