DB 테이블에 저장된 카테고리를 트리형식으로 변수에 담기
head.php 에서 보면 메뉴 카테고리를 $menu_datas 에 담는 코드가 있습니다.
카테고리 깊이가 2단이니까 for 문을 2번 돌려 $menu_datas 의 변수에 담습니다. 직관적입니다.
카테고리 플러그인 작업중에 카테고리 데이타를 변수에 담기위해 너무 많이 sql문을 실행하는 것 같아 다른 방식으로 구현해봤습니다.
테이블 형식은 다르고 깊이를 좀 자유롭게 두고 싶어습니다.
PHP변수에 담았다가 json형식으로 반환하는 코드입니다.
테이블 형식은 다음과 같습니다.
CREATE TABLE `tl_dir_zone` (
`d_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`d_path` varchar(100) DEFAULT NULL,
`d_name` varchar(100) DEFAULT NULL,
`d_type` varchar(10) DEFAULT NULL,
`d_1` varchar(255) NOT NULL DEFAULT '',
`d_2` varchar(255) NOT NULL DEFAULT '',
`d_3` varchar(255) NOT NULL DEFAULT '',
`d_4` varchar(255) NOT NULL DEFAULT '',
`d_5` varchar(255) NOT NULL DEFAULT ''
PRIMARY KEY (`d_id`),
KEY `idx_d_path` (`d_path`)
) DEFAULT CHARSET=utf8;
[code]
<?php
include './_common.php';
if($LT != 1) exit; //표현타입이 2 이면 종료(법정동처럼 너무 큰 데이타는 다른형식으로 dir_list_x.php 참조)
$json_file = G5_DATA_PATH.'/'.$DIR_TABLE.'_all.json';
$is_cache_used = true;
$cache_time = 60*60*1; // 캐쉬(1시간)
if ($is_cache_used === true && file_exists($json_file) && filectime($json_file) > time() - cache_time) {
echo file_get_contents($json_file);
exit;
}
// 필드를 적절히 수정 : json 파일 크기 줄이기 위해
$sql = "SELECT d_path, d_name, d_type, CHAR_LENGTH(d_path) dd FROM {$DIR_TABLE} ORDER BY d_path"; //dd : dir depth
$result = sql_query($sql);
$TMPM = array();
$DIRM = array();
while ($row = sql_fetch_array($result)) {
$depth = $row['dd'] / $DS;
if($depth == 1) {
$DIRM[$row['d_path']] = $row;
} else {
$p_path = substr($row['d_path'], 0 , ($DS * -1));
$TMPM[$p_path][$row['d_path']] = $row;
}
}
function tree_dir(&$p_row) {
global $TMPM;
foreach($p_row as $k=>$v) {
if(isset($TMPM[$v['d_path']])) {
$p_row[$k]['rows'] = $TMPM[$k];
tree_dir($p_row[$k]['rows']);
}
}
}
tree_dir($DIRM);
$json = json_encode($DIRM, JSON_UNESCAPED_UNICODE);
if ($is_cache_used === true) {
file_put_contents($json_file, $json);
}
echo $json;
?>
[/code]
위의 코드를 이용한 다단 selectbox 예입니다.
http://note.tloghost.kr/plugin/tl_dir/ex-niceselect.php
댓글 2개
게시판 목록
개발자팁
질문은 QA에서 해주시기 바랍니다.
| 번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|---|
| 4766 | jQuery | 8년 전 | 1672 | ||
| 4765 | jQuery | 8년 전 | 1348 | ||
| 4764 | jQuery | 8년 전 | 2160 | ||
| 4763 | jQuery | 8년 전 | 1853 | ||
| 4762 | jQuery | 8년 전 | 1625 | ||
| 4761 | jQuery | 8년 전 | 1932 | ||
| 4760 | jQuery | 8년 전 | 1295 | ||
| 4759 | jQuery | 8년 전 | 1596 | ||
| 4758 | jQuery | 8년 전 | 1310 | ||
| 4757 | jQuery | 8년 전 | 1273 | ||
| 4756 | jQuery | 8년 전 | 1495 | ||
| 4755 | jQuery | 8년 전 | 1646 | ||
| 4754 | jQuery | 8년 전 | 1611 | ||
| 4753 | jQuery | 8년 전 | 1766 | ||
| 4752 | jQuery | 8년 전 | 1519 | ||
| 4751 | jQuery | 8년 전 | 1838 | ||
| 4750 | jQuery | 8년 전 | 1981 | ||
| 4749 | jQuery | 8년 전 | 1439 | ||
| 4748 | PHP | 8년 전 | 2809 | ||
| 4747 | jQuery | 8년 전 | 1389 | ||
| 4746 | jQuery | 8년 전 | 1645 | ||
| 4745 | jQuery | 8년 전 | 1651 | ||
| 4744 | jQuery | 8년 전 | 1531 | ||
| 4743 | jQuery | 8년 전 | 1612 | ||
| 4742 | jQuery | 8년 전 | 1702 | ||
| 4741 | jQuery | 8년 전 | 1597 | ||
| 4740 | jQuery | 8년 전 | 1600 | ||
| 4739 | jQuery | 8년 전 | 1796 | ||
| 4738 | jQuery | 8년 전 | 1306 | ||
| 4737 | jQuery | 8년 전 | 2097 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기