소스 파일 : /yoyak/yoyak_contents_db_qry.php (2018-07-21)     소스 설명 : (분류요약관리) 요약관리 목록 db 쿼리
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
<?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;
}

?>


Copyrightⓒ written by 차재복 (Cha Jae Bok)               기술용어해설 후원
"본 웹사이트 내 모든 저작물은 원출처를 밝히는 한 자유롭게 사용(상업화포함) 가능합니다"