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에서 해주시기 바랍니다.
| 번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|---|
| 4796 | jQuery | 8년 전 | 1534 | ||
| 4795 | jQuery | 8년 전 | 1438 | ||
| 4794 | jQuery | 8년 전 | 1563 | ||
| 4793 | jQuery | 8년 전 | 1620 | ||
| 4792 | jQuery | 8년 전 | 1625 | ||
| 4791 | jQuery | 8년 전 | 1794 | ||
| 4790 | jQuery | 8년 전 | 1735 | ||
| 4789 | jQuery | 8년 전 | 1598 | ||
| 4788 | jQuery | 8년 전 | 1660 | ||
| 4787 | jQuery | 8년 전 | 1475 | ||
| 4786 | jQuery | 8년 전 | 1434 | ||
| 4785 | jQuery | 8년 전 | 1645 | ||
| 4784 | jQuery | 8년 전 | 1523 | ||
| 4783 | jQuery | 8년 전 | 1232 | ||
| 4782 | jQuery | 8년 전 | 1599 | ||
| 4781 | jQuery | 8년 전 | 1338 | ||
| 4780 | jQuery | 8년 전 | 1575 | ||
| 4779 | jQuery | 8년 전 | 1983 | ||
| 4778 | jQuery | 8년 전 | 1957 | ||
| 4777 | jQuery | 8년 전 | 1528 | ||
| 4776 | jQuery | 8년 전 | 1582 | ||
| 4775 | jQuery | 8년 전 | 1971 | ||
| 4774 | jQuery | 8년 전 | 1485 | ||
| 4773 | jQuery | 8년 전 | 1410 | ||
| 4772 | jQuery | 8년 전 | 2203 | ||
| 4771 | jQuery | 8년 전 | 3610 | ||
| 4770 | PHP | 8년 전 | 5579 | ||
| 4769 | 기타 | 8년 전 | 1963 | ||
| 4768 | 기타 | 8년 전 | 1781 | ||
| 4767 | 기타 | 8년 전 | 2067 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기