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

통합게시판으로 게시판명 불러오기 채택완료

dlsdo04 9년 전 조회 7,000

제가 여러 게시판을 묶어서 통합게시판을 만들어 각 게시글들과, 테이블명을 불러오려고 합니다.

여러개의 테이블을 union all 로 통합을 했거든요

g5_board에서 테이블명을 가져오려고 하는건데

 

근데 g5_write_테이블과, g5_board 디비는 서로 공유하고 있는 필드가 없는데 어떻게 테이블명을 가져올까요?

 

결국은 저는 union all로 통합한 g5_write 게시판들의 게시판 명을 가져와서 이 전체 통합한 게시판을 페이징 처리 하려고 합니다.

 

아래와 같이 테이블을 배열로 넣고

 

</p><p style="box-sizing: border-box; margin-left: 40px; word-break: break-all; line-height: 21.6px; color: rgb(51, 51, 51); font-family: dotum, sans-serif; font-size: 12px; background-color: rgb(255, 255, 255);">$tableList = array("g5_write_B01","g5_write_B02");  </p><p style="box-sizing: border-box; margin-left: 120px; word-break: break-all; line-height: 21.6px; color: rgb(51, 51, 51); font-family: dotum, sans-serif; font-size: 12px; background-color: rgb(255, 255, 255);"> </p><p style="box-sizing: border-box; margin-left: 80px; word-break: break-all; line-height: 21.6px; color: rgb(51, 51, 51); font-family: dotum, sans-serif; font-size: 12px; background-color: rgb(255, 255, 255);">//g5_write_를 자르고 테이블 명만 남김</p><p style="box-sizing: border-box; word-break: break-all; line-height: 21.6px; color: rgb(51, 51, 51); font-family: dotum, sans-serif; font-size: 12px; background-color: rgb(255, 255, 255);"><span class="Apple-tab-span" style="box-sizing: border-box; white-space: pre;">			</span>$bo_table_name[0] = substr($tableList[0], -10, 12);</p><p style="box-sizing: border-box; word-break: break-all; line-height: 21.6px; color: rgb(51, 51, 51); font-family: dotum, sans-serif; font-size: 12px; background-color: rgb(255, 255, 255);"> </p><p style="box-sizing: border-box; margin-left: 80px; word-break: break-all; line-height: 21.6px; color: rgb(51, 51, 51); font-family: dotum, sans-serif; font-size: 12px; background-color: rgb(255, 255, 255);">//아래는 union all로 통합하는 쿼리</p><p style="box-sizing: border-box; word-break: break-all; line-height: 21.6px; color: rgb(51, 51, 51); font-family: dotum, sans-serif; font-size: 12px; background-color: rgb(255, 255, 255);"><span class="Apple-tab-span" style="box-sizing: border-box; white-space: pre;">			</span>$sql = "(select * from ".$tableList[0]." as t, g5_board as b where b.bo_table=".$bo_table_name[0].")";</p><p style="box-sizing: border-box; word-break: break-all; line-height: 21.6px; color: rgb(51, 51, 51); font-family: dotum, sans-serif; font-size: 12px; background-color: rgb(255, 255, 255);"> </p><p style="box-sizing: border-box; word-break: break-all; line-height: 21.6px; color: rgb(51, 51, 51); font-family: dotum, sans-serif; font-size: 12px; background-color: rgb(255, 255, 255);"><span class="Apple-tab-span" style="box-sizing: border-box; white-space: pre;">			</span>for($i = 1; $i < count($tableList); $i++) { </p><p style="box-sizing: border-box; word-break: break-all; line-height: 21.6px; color: rgb(51, 51, 51); font-family: dotum, sans-serif; font-size: 12px; background-color: rgb(255, 255, 255);"><span class="Apple-tab-span" style="box-sizing: border-box; white-space: pre;">				</span>$bo_table_name[$i] = substr($tableList[$i], -10, 12);</p><p style="box-sizing: border-box; word-break: break-all; line-height: 21.6px; color: rgb(51, 51, 51); font-family: dotum, sans-serif; font-size: 12px; background-color: rgb(255, 255, 255);"><span class="Apple-tab-span" style="box-sizing: border-box; white-space: pre;">				</span>$sql .= " union all (select * from ".$tableList[$i]." as t, g5_board as b where b.bo_table=".$bo_table_name[$i].")" ; </p><p style="box-sizing: border-box; word-break: break-all; line-height: 21.6px; color: rgb(51, 51, 51); font-family: dotum, sans-serif; font-size: 12px; background-color: rgb(255, 255, 255);"><span class="Apple-tab-span" style="box-sizing: border-box; white-space: pre;">			</span>}</p><p style="box-sizing: border-box; word-break: break-all; line-height: 21.6px; color: rgb(51, 51, 51); font-family: dotum, sans-serif; font-size: 12px; background-color: rgb(255, 255, 255);"><span class="Apple-tab-span" style="box-sizing: border-box; white-space: pre;">				</span>$sql .= " limit {$from_record}, {$rows}";</p><p style="box-sizing: border-box; word-break: break-all; line-height: 21.6px; color: rgb(51, 51, 51); font-family: dotum, sans-serif; font-size: 12px; background-color: rgb(255, 255, 255);">
 

 

위처럼 해봤는데 아무것도 뜨지 않습니다.

 

 

아래와 같이 하면 $tableList[$i] 안에 있는 게시글 제목만 출력이 됩니다.

저는 각 게시글에서 게시판명도 필요하거든요!

어떻게 하면 통합으로 묶어진 g5_write에서 게시판명을 가져와서 출력할 수 있을까요?

 

</p><p style="box-sizing: border-box; word-break: break-all; line-height: 21.6px; color: rgb(51, 51, 51); font-family: dotum, sans-serif; font-size: 12px; background-color: rgb(255, 255, 255);"><span class="Apple-tab-span" style="box-sizing: border-box; white-space: pre;">			</span>$sql = "(select * from ".$tableList[0].")";</p><p style="box-sizing: border-box; word-break: break-all; line-height: 21.6px; color: rgb(51, 51, 51); font-family: dotum, sans-serif; font-size: 12px; background-color: rgb(255, 255, 255);"> </p><p style="box-sizing: border-box; word-break: break-all; line-height: 21.6px; color: rgb(51, 51, 51); font-family: dotum, sans-serif; font-size: 12px; background-color: rgb(255, 255, 255);"><span class="Apple-tab-span" style="box-sizing: border-box; white-space: pre;">			</span>for($i = 1; $i < count($tableList); $i++) { </p><p style="box-sizing: border-box; word-break: break-all; line-height: 21.6px; color: rgb(51, 51, 51); font-family: dotum, sans-serif; font-size: 12px; background-color: rgb(255, 255, 255);"><span class="Apple-tab-span" style="box-sizing: border-box; white-space: pre;">				</span>$sql .= " union all (select * from ".$tableList[$i].")" ; </p><p style="box-sizing: border-box; word-break: break-all; line-height: 21.6px; color: rgb(51, 51, 51); font-family: dotum, sans-serif; font-size: 12px; background-color: rgb(255, 255, 255);"><span class="Apple-tab-span" style="box-sizing: border-box; white-space: pre;">			</span>}</p><p style="box-sizing: border-box; word-break: break-all; line-height: 21.6px; color: rgb(51, 51, 51); font-family: dotum, sans-serif; font-size: 12px; background-color: rgb(255, 255, 255);"><span class="Apple-tab-span" style="box-sizing: border-box; white-space: pre;">				</span>$sql .= " limit {$from_record}, {$rows}";</p><p style="box-sizing: border-box; word-break: break-all; line-height: 21.6px; color: rgb(51, 51, 51); font-family: dotum, sans-serif; font-size: 12px; background-color: rgb(255, 255, 255);"> 

 

아니면 가상 필드를 만들어서 각 투플마다 해당 게시판의 이름을 넣는다던지 이런 방법이 있을까요?

 

제가 초보라 자세한 답변 주시면 감사하겠습니다..  !!:)

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

답변 2개

채택된 답변
+20 포인트
9년 전

http://sir.kr/g5_tip/4103">http://sir.kr/g5_tip/4103 

여기에 보시면, 여러개의 게시판에서 글을 가져오는 방법이 있습니다. 

참고해 보세요. 

 

로그인 후 평가할 수 있습니다

답변에 대한 댓글 2개

d
dlsdo04
9년 전
이 방법을 보긴 했는데 일정 갯수만 가져오는거라.. 저는 여러게시판 게시글 전체를 다 가져오려고 하거든요! 혹시 다른 방법이 있을까요? ㅠㅠ
마스타
9년 전
모든 게시판에서 가져오려면,
조건문에서 게시판명 관련 조건을 빼 버리시면 될 것 같은데요.

find_in_set(a.bo_table, '{$bo_tables}')
이 부분이요.

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

</p><p>$boards = $u_query = array();
$query = my_sql_query("SELECT bo_table, bo_subject FROM g5_board");
while($rows = my_sql_fetch_array($query)) {
    $boards[$rows['bo_table']] = $rows['bo_subject'];
}

foreach($boards as $bo_table => $bo_subject) {
    $u_query[] = "SELECT wr_id, wr_subject, '".$bo_table."' AS tbl, '".$bo_subject."' AS nm FROM g5_write_".$bo_table." WHERE wr_is_comment = 0";
}

$query_str = implode(" UNION ALL ", $u_query);
$query = "SELECT * FROM (".$query_str.") AS mytbl ORDER BY wr_id DESC";

$result = mysql_query($query);
while($rows = mysql_fetch_array($result)) {
    echo $rows['tbl']." : ".$rows['nm']." : ".$rows['wr_subject']."
";
}</p><p>

참고로 그누에서 제공하는 함수(sql_*)는 union은 안 먹히게 되어 있습니다. 

로그인 후 평가할 수 있습니다

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

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

로그인