다른 테이블에서 데이터 가져오기

다른 테이블에서 데이터 가져오기

QA

다른 테이블에서 데이터 가져오기

본문

관리자페이지에 페이지를 하나 만들어서 
그 페이지에 DB의 A라는 테이블의 데이터들을 출력하려고하는데요 
한 DB의 A,B,C 라는 테이블이 있고 각 테이블의 칼럼명이나 모든구조가 동일할 경우에 
관리자페이지에 만들어둔 페이지에 
각 테이블의값을 업로드 날짜순으로 출력하고자 합니다. 

<?php
$sub_menu = '400100';
include_once('./_common.php');

auth_check($auth[$sub_menu], "r");

$g5['title'] = '최고관리자전용 DB';
if ($fm_subject) $g5['title'] .= ' : '.$fm_subject;
include_once (G5_ADMIN_PATH.'/admin.head.php');

$sql_common = " from g5_endball  ";
$sql_search = " where (1) ";

$sql = " select count(*) as cnt {$sql_common} {$sql_search}  ";
$row = sql_fetch($sql);
$total_count = $row['cnt'];

$rows = $config['cf_page_rows'];
$total_page  = ceil($total_count / $rows);  // 전체 페이지 계산
if ($page < 1) { $page = 1; } // 페이지가 없으면 첫 페이지 (1 페이지)
$from_record = ($page - 1) * $rows; // 시작 열을 구함

$sql = "select * $sql_common order by wr_id desc limit {$from_record}, {$rows}";
$result = sql_query($sql);

$listall = '<a href="'.$_SERVER['SCRIPT_NAME'].'" class="ov_listall">전체목록</a>';
?>

현재 페이지 소스 상단에 이렇게 테이블의 데이터값을 불러오고있는데요 
위의 소스는 g5_endball이라는 테이블의 소스만 가져오고 있는 상태입니다. 
g5_endball을 포함한 다른 테이블들의 데이터를 불러오려면 어떻게해야할까요..

검색해서 union 과 join등 다 써봤지만 제가 잘 몰라서 그러는건지 정상작동이 안되네요 ㅠㅠ...

이 질문에 댓글 쓰기 :

답변 2

모든 테이블의 구조가 동일하고

순서에 상관없이 출력하시렴녀

 

$where = " where 1=1 ";

$sql = " select * from (

                 select * from table1

                 union all

                 select * from table2

                 union all

                 select * from table3

                )

              {$where }

              order by wr_datetime desc

             ";

의 방법으로 쿼리를 작성하시고

where 구문은 적절하게 넣으시면 됩니다.

관리자페이지에서 모든 게시판의 글을 불러오는 작업을 한적 있습니다.

해당 소스는 통체로 올려드릴테니

필요한 부분 수정해서 사용하셔야할겁니다.

 


<?php
$sub_menu = "700100";
include_once('./_common.php');
auth_check($auth[$sub_menu], 'r');
$sql_common = " from {$g5['board_new_table']}";
$sql_search = " where wr_id = wr_parent ";
if ($stx) {
    $sql_search .= " and ( ";
    switch ($sfl) {
        case 'mb_point' :
            $sql_search .= " ({$sfl} >= '{$stx}') ";
            break;
        case 'mb_level' :
            $sql_search .= " ({$sfl} = '{$stx}') ";
            break;
        case 'mb_tel' :
        case 'mb_hp' :
            $sql_search .= " ({$sfl} like '%{$stx}') ";
            break;
        default :
            $sql_search .= " ({$sfl} like '{$stx}%') ";
            break;
    }
    $sql_search .= " ) ";
}

if (!$sst) {
    $sst = "bn_id";
    $sod = "desc";
}
$sql_order = " order by {$sst} {$sod} ";
$sql = " select count(*) as cnt {$sql_common} {$sql_search} {$sql_order} ";
$boardQue = sql_query("select bo_table, bo_subject from {$g5['board_table']}");
while($board = sql_fetch_array($boardQue)){
    $query[] = "(select wr_id, wr_datetime, wr_name, wr_is_comment, wr_hit, wr_comment, wr_subject, ca_name, wr_option, '".$board['bo_table']."' as bo_table, '".$board['bo_subject']."' as bo_subject from g5_write_".$board['bo_table']." where wr_is_comment = '0' and (wr_subject like '%$stx%' or wr_content like '%$stx%'))";
}
$sql = implode(" union ", $query)." order by wr_datetime desc";
//echo $sql;
$cnt = sql_num_rows(sql_query($sql));
$total_count = $cnt; // 전체 게시글 수
$rows = $config['cf_page_rows'];
$total_page  = ceil($total_count / $rows);  // 전체 페이지 계산
if ($page < 1) $page = 1; // 페이지가 없으면 첫 페이지 (1 페이지)
$from_record = ($page - 1) * $rows; // 시작 열을 구함
/*
// 탈퇴회원수
$sql = " select count(*) as cnt {$sql_common} {$sql_search} and mb_leave_date <> '' {$sql_order} ";
$row = sql_fetch($sql);
$leave_count = $row['cnt'];
// 차단회원수
$sql = " select count(*) as cnt {$sql_common} {$sql_search} and mb_intercept_date <> '' {$sql_order} ";
$row = sql_fetch($sql);
$intercept_count = $row['cnt'];
*/
$listall = '<a href="'.$_SERVER['PHP_SELF'].'" class="ov_listall">전체목록</a>';
$g5['title'] = '게시판관리 - 게시글 관리';
$cf_title2 = ' 게시글을 관리하는 페이지입니다.';
include_once('./admin.head.php');
$sql = $sql." limit {$from_record}, {$rows} ";
$result = sql_query($sql);
$colspan = 16;
?>
<div class="local_ov01 local_ov">
    <?php echo $listall ?>
    총게시글수 <?php echo number_format($total_count) ?>개 중, 댓글 <?php echo number_format($intercept_count) ?></a>개
</div>
<form id="fsearch" name="fsearch" class="local_sch01 local_sch" method="get">
<label for="sfl" class="sound_only">검색대상</label>
<select name="sfl" id="sfl">
    <option value="mb_id"<?php echo get_selected($_GET['sfl'], "mb_id"); ?>>회원아이디</option>
    <option value="mb_nick"<?php echo get_selected($_GET['sfl'], "mb_nick"); ?>>닉네임</option>
    <option value="mb_name"<?php echo get_selected($_GET['sfl'], "mb_name"); ?>>이름</option>
    <option value="mb_level"<?php echo get_selected($_GET['sfl'], "mb_level"); ?>>권한</option>
    <option value="mb_email"<?php echo get_selected($_GET['sfl'], "mb_email"); ?>>E-MAIL</option>
    <option value="mb_tel"<?php echo get_selected($_GET['sfl'], "mb_tel"); ?>>전화번호</option>
    <option value="mb_hp"<?php echo get_selected($_GET['sfl'], "mb_hp"); ?>>휴대폰번호</option>
    <option value="mb_point"<?php echo get_selected($_GET['sfl'], "mb_point"); ?>>포인트</option>
    <option value="mb_datetime"<?php echo get_selected($_GET['sfl'], "mb_datetime"); ?>>가입일시</option>
    <option value="mb_ip"<?php echo get_selected($_GET['sfl'], "mb_ip"); ?>>IP</option>
    <option value="mb_recommend"<?php echo get_selected($_GET['sfl'], "mb_recommend"); ?>>추천인</option>
</select>
<label for="stx" class="sound_only">검색어<strong class="sound_only"> 필수</strong></label>
<input type="text" name="stx" value="<?php echo $stx ?>" id="stx" required class="required frm_input">
<input type="submit" class="btn_submit" value="검색">
</form>
<div class="local_desc01 local_desc">
    <p>
        전체 게시글을 수정, 삭제, 관리를 할 수 있습니다.
    </p>
</div>
<form name="fmemberlist" id="fmemberlist" action="./board_all_list_update.php" onsubmit="return fmemberlist_submit(this);" method="post">
<input type="hidden" name="sst" value="<?php echo $sst ?>">
<input type="hidden" name="sod" value="<?php echo $sod ?>">
<input type="hidden" name="sfl" value="<?php echo $sfl ?>">
<input type="hidden" name="stx" value="<?php echo $stx ?>">
<input type="hidden" name="page" value="<?php echo $page ?>">
<input type="hidden" name="token" value="">
<div class="tbl_head02 tbl_wrap">
    <table>
    <caption><?php echo $g5['title']; ?> 목록</caption>
    <thead>
    <tr>
        <th scope="col">
            <label for="chkall" class="sound_only">회원 전체</label>
            <input type="checkbox" name="chkall" value="1" id="chkall" onclick="check_all(this.form)">
        </th>
        <th scope="col">게시판<br>분류</th>
        <th scope="col">카테고리</th>
        <th scope="col">제목</th>
        <th scope="col">작성자</th>
        <th scope="col">작성일</th>
        <th scope="col">조회수</th>
        <th scope="col">댓글수</th>
    </tr>
    </thead>
    <tbody>
    <?php
    for ($i=0; $row2=sql_fetch_array($result); $i++) {
        $bg = 'bg'.($i%2);
        $board = sql_fetch("select * from g5_board where bo_table = '{$row2['bo_table']}' ");
        $row = sql_fetch("select * from g5_write_{$row2['bo_table']} where wr_id = '{$row2['wr_id']}' ");
    ?>
    <tr class="<?php echo $bg; ?>">
        <td headers="mb_list_chk" class="td_chk" >
           <input type="hidden" name="mb_id[<?php echo $i ?>]" value="<?php echo $row['wr_id'] ?>" id="mb_id_<?php echo $i ?>">
           <input type="checkbox" name="chk[]" value="<?php echo $i ?>" id="chk_<?php echo $i ?>">
        </td>
        <td headers="mb_list_name" class="td_category"><?php echo $row2['bo_subject']; ?></td>
        <td headers="mb_list_name" class="td_category"><?php echo $row2['ca_name']; ?></td>
        <td headers="mb_list_cert" class="td_subject"><?php echo $row2['wr_subject']; ?></td>
        <td class="td_name"><?php echo $row2['wr_name']; ?></td>
        <td class="td_datetime"><?php echo $row2['wr_datetime']; ?></td>
        <td class="td_num"><?php echo $row2['wr_hit']; ?></td>
        <td class="td_num"><?php echo $row2['wr_comment']; ?></td>
    </tr>
    
    <?php
    }
    if ($i == 0)
        echo "<tr><td colspan=\"".$colspan."\" class=\"empty_table\">자료가 없습니다.</td></tr>";
    ?>
    </tbody>
    </table>
</div>
<div class="btn_list01 btn_list">
    <input type="submit" name="act_button" value="선택수정" onclick="document.pressed=this.value">
    <input type="submit" name="act_button" value="선택삭제" onclick="document.pressed=this.value">
</div>
</form>
<?php
$qstr .= "&stx=".$stx;
echo get_paging(G5_IS_MOBILE ? $config['cf_mobile_pages'] : $config['cf_write_pages'], $page, $total_page, '?'.$qstr.'&page='); ?>
<script>
function fmemberlist_submit(f)
{
    if (!is_checked("chk[]")) {
        alert(document.pressed+" 하실 항목을 하나 이상 선택하세요.");
        return false;
    }
    if(document.pressed == "선택삭제") {
        if(!confirm("선택한 자료를 정말 삭제하시겠습니까?")) {
            return false;
        }
    }
    return true;
}
</script>
<?php
include_once ('./admin.tail.php');
?>
 
답변을 작성하시기 전에 로그인 해주세요.
전체 123,667 | RSS
QA 내용 검색

회원로그인

(주)에스아이알소프트 / 대표:홍석명 / (06211) 서울특별시 강남구 역삼동 707-34 한신인터밸리24 서관 1404호 / E-Mail: admin@sir.kr
사업자등록번호: 217-81-36347 / 통신판매업신고번호:2014-서울강남-02098호 / 개인정보보호책임자:김민섭(minsup@sir.kr)
© SIRSOFT