소스 파일 : /reform/reform_gen_del_update.php (2020-11-21)     소스 설명 : (개선중) reform 항목 생성,삭제 업데이트
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php // (2020.11.21, 차재복, Cha Jae Bok, http://www.ktword.co.kr) 

# id 자식,직전,직후 노드 생성 #

// id child 노드 생성
function id_child ($id, $str, $dbi, $table_name='reform') {

	// 해당 id의 자식 존재 및 자식 최대 번호(max sub_seq) 알아보기
	$query = "select * from {$table_name} where parent=$id";
	$result = mysqli_query($dbi,$query);
		if (mysqli_errno($dbi)) { $err_msg .= mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }

		// 자식이 하나도 없을 경우
		if( !($max_sub_seq = mysqli_num_rows($result)) ) {
			$max_sub_seq = 1;

			$query = "update {$table_name} set child=1,date=now() where id=$id";
			$result=mysqli_query($dbi,$query);
				if (mysqli_errno($dbi)) { $err_msg .= mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }
		// 자식이 있을 경우
		} else {
			$max_sub_seq = $max_sub_seq + 1;
		}

	// 전체 id 중 빈 번호 찾아냄
	$query = "select a.id + 1 as available from {$table_name} a left join {$table_name} b on b.id = (a.id + 1) 
					where b.id is null order by a.id limit 0,1";
	$result=mysqli_query($dbi,$query);
		if (mysqli_errno($dbi)) { $err_msg .= mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }
	$matched=mysqli_fetch_assoc($result);

		// $table_name 자체가 텅 비었으면, $available_id = 1
		if ( empty($matched[available]) ) {
			$available_id =	1 ;
		} else {
			$available_id =	$matched[available] ;
		}

	// 해당 id의 depth 및 path2node 확인
	$query = "select depth,path2node from {$table_name} where id=$id";
	$result = mysqli_query($dbi,$query);
		if (mysqli_errno($dbi)) { $err_msg .= mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }
	$matched=mysqli_fetch_assoc($result);
		$depth = $matched[depth] + 1;
		$path2node = $matched[path2node].','.$available_id;

	// 자식 항목 (id,parent,sub_seq,name,depth,path2node,date,child) 새로이 삽입
	$str = mysqli_real_escape_string($dbi,$str);
	$query = "insert into {$table_name} 
				(id, parent, sub_seq, name, depth, path2node, date, child)
				values 
				($available_id, $id, $max_sub_seq, '{$str}', $depth, '{$path2node}', now(), 0)";
	$result = mysqli_query($dbi,$query);
		if (mysqli_errno($dbi)) { $err_msg .= mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }

	// 실패시 에러메세지 리턴
	if (mysqli_affected_rows($dbi) > 0) {
		// 해당 id의 산하 자식 노드들의 정보
		$nextParentChilds = nextParentChilds($id, $dbi);
		$return = array('ok' => true, 'nextParentChilds'=>$nextParentChilds);
	} else {
		$err_msg .= $query;
		$return = array('err_msg' => $err_msg);
	}

	// 결과 json 포멧으로 송출
	echo json_encode($return, JSON_UNESCAPED_UNICODE);
}


// id 직전 직후 노드 생성
function id_prepend_append ($ch, $id, $str, $dbi, $table_name='reform') {

	// 해당 id의 부모,순서,depth,path2node를 알아냄
	$query = "select parent,sub_seq,depth,path2node from {$table_name} where id=$id";
	$result = mysqli_query($dbi,$query);
		if (mysqli_errno($dbi)) { $err_msg .= mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }
	$matched=mysqli_fetch_assoc($result);

		$parent = $matched[parent];
		if ( $ch == 'id_prepend' ) {
			$sub_seq = $matched[sub_seq] ;
		} else if ( $ch == 'id_append' ) {
			$sub_seq = $matched[sub_seq] + 1 ;
		} 
		$depth = $matched[depth];
		$path2node_prev = substr($matched[path2node],0,strrpos($matched[path2node],','));	// ???

	// 해당 id 부터 1씩 증가시킴
	$query = "update {$table_name} set sub_seq=(sub_seq+1) where parent=$parent and sub_seq>=$sub_seq";
	$result = mysqli_query($dbi,$query);

	// id 중 빈 번호 확인
	$query = "select a.id + 1 as available 
					from {$table_name} a left join {$table_name} b on b.id = (a.id + 1) 
					where b.id is null 
					order by a.id limit 0,1";
	$result = mysqli_query($dbi,$query);
		if (mysqli_errno($dbi)) { $err_msg .= mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }
	$matched = mysqli_fetch_assoc($result);
	$available_id = $matched[available];

	// path2node 설정
	$path2node = $path2node_prev.','.$available_id;

	// 분류항목 새로이 삽입
	$str = mysqli_real_escape_string($dbi,$str);
	$query = "insert into {$table_name}
					(id, parent, sub_seq, name, depth, path2node, date, child)
					values
					($available_id, $parent, $sub_seq, '{$str}', $depth, '{$path2node}', now(), 0)";
	$result = mysqli_query($dbi,$query);
		if (mysqli_errno($dbi)) { $err_msg .= mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }

	// 실패시 에러메세지 리턴
	if (mysqli_affected_rows($dbi) > 0) {
		// 해당 id의 parent의 산하 자식 노드들의 정보
		$nextParentChilds = nextParentChilds($parent, $dbi);

		$return = array('ok' => true, 'sub_seq'=>$sub_seq, 'nextParentChilds'=>$nextParentChilds);
	} else {
		$err_msg .= $query;
		$return = array('err_msg' => $err_msg);
	}

	// 결과 json 포멧으로 송출
	echo json_encode($return, JSON_UNESCAPED_UNICODE);

}


// id 노드 삭제 
function id_delete($ch, $id, $dbi, $table_name) {

	if ($ch != 'id_delete') return;

	// 선택 id의 parent,sub_seq 알아보기
	$query = "select a.parent,a.sub_seq
						,count(b.id) as sub_cnt
						,c.id as more_id
					from reform a 
						left join reform b on a.id=b.parent
						left join reform_more c on a.id=c.id
					where a.id=$id
					group by a.id";
	$result = mysqli_query($dbi,$query);
		if (mysqli_errno($dbi)) { $err_msg .= mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }
	$matched = mysqli_fetch_assoc($result);
		$parent = $matched[parent];
		$sub_seq = $matched[sub_seq];
		$sub_cnt = $matched[sub_cnt];
		$more_id = $matched[more_id];

	// 하부 id 있으면, 삭제 불가
	if ($sub_cnt > 0) 
		$return = array('err_msg'=>$err_msg . '하부 id 있어서, 삭제 불가');

	// reform_more 있으면, 삭제 불가
	if (!empty($more_id))
		$return = array('err_msg'=>$err_msg . 'reform_more 있어서, 삭제 불가');

	if (empty($return['err_msg'])) {
		// 해당 id를 삭제
		mysqli_query($dbi,"delete from reform where id=$id");
			if (mysqli_errno($dbi)) { $err_msg .= mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }
	
		// 삭제 성공하면, 직후 id들을 하나씩 상향 이동(sub_seq-1)하고, 형제 노드들의 수 갱신
		if ( mysqli_affected_rows($dbi) >= 1 ) {
			mysqli_query($dbi,"update reform set sub_seq=(sub_seq-1) where parent=$parent and sub_seq>=($sub_seq+1)");
				if (mysqli_errno($dbi)) { $err_msg .= mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }

			// 선택 id의 부모 노드 산하 자식노드들의 수
			$query = "select count(id) as sibling_cnt from reform where parent=$parent";
			$result = mysqli_query($dbi,$query);
				if (mysqli_errno($dbi)) { $err_msg .= mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }
			$matched = mysqli_fetch_assoc($result);
				$sibling_cnt = $matched[sibling_cnt];

			// 선택 id의 부모 노드 산하 자식노드들의 수 => child에 대입
			$query = "update reform set child = $sibling_cnt where id=$parent";
			$result = mysqli_query($dbi,$query);
				if (mysqli_errno($dbi)) { $err_msg .= mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }

			$return = array('ok'=>true,'sibling'=>$sibling_cnt);
		} else {
			$return = array('err_msg'=>$err_msg . '삭제 실패 에러');
		}
	} 

	echo json_encode($return, JSON_UNESCAPED_UNICODE);
	
}

?>


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