소스 파일명 : ajax_content.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
<?php // (2017.2.5, 차재복, Cha Jae Bok, cjbword@gmailcom)

	// db 접속 및 편집자 설정
	include_once "km_db_conn.php";

	// 헤더부	
//	echo "<meta http-equiv='content-type' content='text/html; charset=utf-8'>";
//	echo "<meta charset='utf-8'>";

//	echo "<meta http-equiv='content-type' content='application/json'>";
//	echo "<meta charset='utf-8'>";

	header("Content-Type:application/json");


?>

<?php

//	echo "<br> \"이것은 Ajax 호출에 따른 정상적인 반환 결과 입니다 !!! (전달된 id = $id)\"";

	$id = $_REQUEST['id'];

//	$query = "select id,chap,title,substring_index(getpriority(id),'|',1) as pre_ord from knowhow_1 where parent=$id order by chap";
	$query = "select a.id,a.chap,a.title,a.pre_ord,count(b.id) as sub_cnt from (select id,chap,title,substring_index(getpriority(id),'|',1) as pre_ord from km where parent=$id order by chap) a left join km b on a.id=b.parent group by id order by a.chap";

	$result = mysqli_query($dbi,$query);
		if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}
	if ($result) {
		$set = array();
		$i = 0;
		while ( $matched=mysqli_fetch_assoc($result) ) {
			$set[$i][id] = $matched[id];
			$set[$i][chap] = $matched[chap];
			$set[$i][title] = $matched[title]; 
			
			// 항목 번호
			$temp_arr = explode('.',$matched[pre_ord]); // pre_ord : 001.002, 001.002.004 등 순서화
			$temp_var = '';
			foreach ($temp_arr as $key => $value) $temp_var = $temp_var.ltrim($value,'0').".";
			$set[$i][prefix] = $temp_var;				// prefix : 1.2, 1.2.4 등 항목 번호

			// 하부 자식 갯수
			$set[$i][sub_cnt] = $matched[sub_cnt];

			$i = $i + 1;
		} 
	} else {
		$set = array( 0 => 'empty' );
	}
	echo my_json_encode($set);

?>

<?php

// 출처 : http://php.net/manual/kr/function.json-encode.php
function my_json_encode($arr)
{

	if (version_compare(phpversion(), '5.4.0', '<')) {
        //convmap since 0x80 char codes so it takes all multibyte codes (above ASCII 127). So such characters are being "hidden" from normal json_encoding
        array_walk_recursive($arr, 
			function (&$item, $key) {
				if (is_string($item)) 
					$item = mb_encode_numericentity($item, array (0x80, 0xffff, 0, 0xffff), 'UTF-8'); 
			}
		);
        return mb_decode_numericentity(json_encode($arr), array (0x80, 0xffff, 0, 0xffff), 'UTF-8');

	} else {
		// Encode multibyte Unicode characters literally (default is to escape as \uXXXX)
		// Available since PHP 5.4.0.
		return json_encode($arr,JSON_UNESCAPED_UNICODE);
	}

}

?>