<?php // (2018.7.21, 차재복, Cha Jae Bok, http://www.ktword.co.kr)
## 쿼리문 (db 쿼리 -> php 배열)
# 현재 id를 기준으로, 상위 hierarchy 전체 레코드셋 결과를 배열로 주는 함수
function tbl_read($id, $dbi) {
// 전달 변수 : $id
if( empty($id) ) {
$id = $_REQUEST['id'];
if ( isset($_REQUEST['id']) and !empty($id) and !is_numeric($id) or $id<0 ) exit; // 해킹방지 (수치>0)
$id = substr($id,0,10); // 해킹 방지 (글자 수 제한)
if(empty($id)) $id=0; // 디폴트
}
// 루트부터 대상 id까지 path 추출
$query = "select id, substring_index(getpath_v2(id),'|',-1) as path
from gubun_tree_v2 where id=$id"; //
$result=mysqli_query($dbi,$query);
if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}
if (mysqli_num_rows($result) == 0) {
// 일치 레코드 없으면 `루트 (0)` 만 포함
$cur_path = '0';
} else {
// cur_path 정보에 `루트` 및 `자기자신` 포함
$matched=mysqli_fetch_assoc($result);
$cur_path = '0,'.str_replace('::',',',$matched['path']);
}
// 각 레별별 해당 노드들 쿼리
$query = "select a.id, a.parent, a.sub_seq, a.name, a.linked_num, a.yoyak,
@path:=getpath_v2(a.id),
@temp:=substring_index(substring_index(@path,'|',2),'|',-1) as pre_ord,
(char_length(@temp)-char_length(replace(@temp,'.','')) + 1) as depth,
substring_index(@path,'|',-1) as path,
count(b.id) as sub_cnt
from gubun_tree_v2 a
left join gubun_tree_v2 b on a.id=b.parent
where a.id<>0 and a.parent in (".$cur_path.")
group by a.id
order by pre_ord"
;
$result=mysqli_query($dbi,$query);
if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}
$prev=1; $i=0;
while ( $matched = mysqli_fetch_assoc($result) ) {
// 매 쿼리 결과 레코드를 배열화
$set[] = $matched;
// $set에서 현재 $id의 $cur_row 확보
if ($id == $matched['id']) $cur_row = $matched;
// cur_path 항목별로, 직속 자식들 별도 array 확보
if ( strpos(','.$cur_path.',',$matched['parent']) !== false) {
$per_parent_rows[$matched['parent']][] = $matched;
}
$i=$i+1;
}
return array('set' => $set, 'cur_path' => $cur_path);
}
# 현재 id를 기준으로, 그 하위 자식들의 레코드셋 결과 만을 배열로 주는 함수
function children_read($id, $dbi) {
if(empty($id)) return;
//
$query = "select id, name, sub_seq, linked_num, yoyak, substring_index(getpath_v2(id),'|',-1) as path
from gubun_tree_v2
where parent={$id}
order by sub_seq";
$result=mysqli_query($dbi, $query);
if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}
while ( $matched = mysqli_fetch_assoc($result) ) {
// 매 쿼리 결과 레코드를 배열화
$set[] = $matched;
}
return $set;
}
?>
"본 웹사이트 내 모든 저작물은 원출처를 밝히는 한 자유롭게 사용(상업화포함) 가능합니다"