소스 파일명 : items_utils.php
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
094
095
096
097
098
099
100
101
102
103
104
<?php // (2018.1.31, 차재복, Cha Jae Bok, cjbword@gmailcom) 

# leafnode path 추출을 위한 기초 데이터(id,path2node) 획득
function multi_path_rows($id,$m_temp1,$dbi) {

	// 용어 항목 보기
	if (!empty($m_temp1) ) {
		$query = "select id,path2node from (select no,tree_id from book_idx where no=$m_temp1) a left join gubun_tree_v2 b on a.tree_id=b.id";
//		$query = "select id,path2node from (select no,tree_id from $tbl_idx where no=$m_temp1) a left join $tbl_tree b on a.tree_id=b.id";
		$result=mysqli_query($dbi,$query);
			if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}
		$multi_rows=array();
		while ( $matched=mysqli_fetch_assoc($result) ) {
			if (!empty($id) and $id == $matched[id]) { 
				array_unshift($multi_rows,$matched);
			} else {
				$multi_rows[] = $matched;
			}
		}

	//echo "<pre>";print_r($multi_rows);echo "</pre>";

	// 분류 항목(1개) 보기
	} else if (empty($m_temp1) and !empty($id)) {
		$query = "select id,path2node,concat('0,',replace(substring_index(getpath_v2(id),'|',-1),'::',',')) as getpath from gubun_tree_v2 where id=$id";

		$result=mysqli_query($dbi,$query);
			if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}
		$matched=mysqli_fetch_assoc($result);
		$multi_rows[0] = $matched;

	// 에러 방지
	} else if (empty($m_temp1) and empty($id)) {
		$multi_rows[0] = array('id'=>0,'path2node'=>'0','getpath'=>'0');
	}

// echo 'count($multi_rows)='.count($multi_rows)."<br>";
// echo "<pre>";print_r($multi_rows);echo "</pre>";
	return $multi_rows;

}


//  내용 중에 `[ ... ]` 없애기
function strip_pair($m_str) {
		$rest=$m_str;
		$after='';
		while ( ($lp=strpos($rest,'[')) and ($rp=strpos($rest,']')) ) {
			$after = $after.substr($rest,0,$lp);
			$rest = substr($rest,$rp+1);
		}
		return (empty($after) ? $rest : $after);
}


# leafnode의 getpath(id1,id2,...)로부터 결과쿼리를 세트 배열로 리턴
function db_qry ($cur_path,$cur_id,$dbi) {

	// 각 레별별 해당 노드들 쿼리
	$query = "select a.id,a.parent,a.sub_seq,a.name,a.linked_num,a.yoyak,@path:=getpath_v2(a.id),
					@pre_ord:=substring_index(substring_index(@path,'|',2),'|',-1) as pre_ord, 
					char_length(@pre_ord)-char_length(replace(@pre_ord,'.','')) as depth,
					substring_index(@path,'|',-1) as path_id,
					substring_index(substring_index(@path,'|',-2),'|',1) as path_str
					,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"
				;
//				where a.id<>0 and a.parent in (0)

		$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에서 현재 $cur_id의 배열 index 확인
			if ($cur_id == $matched[id]) $cur_row = $matched;

			// cur_path 항목별로, 직속 자식들 별도 array 확보
			if ( strpos($cur_path.',',$matched[parent].',') !== false ) {
				$per_parent_rows[$matched[parent]][] = $matched;
			} 
			// 마지막 cur_id도 포함
//			if ($matched[id]==$cur_id and $matched[depth]) $per_parent_rows[$matched[id]][] = $matched;

			$i=$i+1;
		}
//echo $cur_id."<br>";
//echo $cur_path."<br>";
//echo 'count($per_parent_rows)='.count($per_parent_rows)."<br>";

//		return array('set' => $set, 'cur_idx' => $cur_idx, 'parent_idx_list' => $parent_idx_list);
		return array('set' => $set, 'cur_row'=> $cur_row,'per_parent_rows' => $per_parent_rows);

}

?>