확장필드 10개로 부족할때 json_encode로 처리방법
제가 가끔 쓰는 방법이긴 한데 검색이 필요치 않는 정보 필드로 테이블을 특별히 늘리지 않는 방향에서
디비에 저장할때 json_encode방법으로 $_POST데이터를 object형식으로 만들어서 content에 넣습니다
$wr_content = json_encode($_POST);
그리고 write 페이지나 뷰페이지에서는 그걸다시 풀어서 뷰해주죠
$data = json_decode($write['wr_content']);
형식으로요.
최근 호스팅에는 왠만하면 저 함수가 다 존제 하는데 오래된 호스팅의 php구버전에서는 함수가 없는 경우가 있습니다.
그럴때 사용자 정의 함수로 처리해야 하는데 매번 검색해서 php.net에서 찾다가
여기에도 올립니다
[code]
if (!function_exists('json_encode')) {
function json_encode($data) {
switch ($type = gettype($data)) {
case 'NULL':
return 'null';
case 'boolean':
return ($data ? 'true' : 'false');
case 'integer':
case 'double':
case 'float':
return $data;
case 'string':
return '"' . addslashes($data) . '"';
case 'object':
$data = get_object_vars($data);
case 'array':
$output_index_count = 0;
$output_indexed = array();
$output_associative = array();
foreach ($data as $key => $value) {
$output_indexed[] = json_encode($value);
$output_associative[] = json_encode($key) . ':' . json_encode($value);
if ($output_index_count !== NULL && $output_index_count++ !== $key) {
$output_index_count = NULL;
}
}
if ($output_index_count !== NULL) {
return '[' . implode(',', $output_indexed) . ']';
} else {
return '{' . implode(',', $output_associative) . '}';
}
default:
return ''; // Not supported
}
}
}
[code]
그조화된 데이터는
http://json.parser.online.fr/
이곳에서 확인해보실수있습니다.
디비에 저장할때 json_encode방법으로 $_POST데이터를 object형식으로 만들어서 content에 넣습니다
$wr_content = json_encode($_POST);
그리고 write 페이지나 뷰페이지에서는 그걸다시 풀어서 뷰해주죠
$data = json_decode($write['wr_content']);
형식으로요.
최근 호스팅에는 왠만하면 저 함수가 다 존제 하는데 오래된 호스팅의 php구버전에서는 함수가 없는 경우가 있습니다.
그럴때 사용자 정의 함수로 처리해야 하는데 매번 검색해서 php.net에서 찾다가
여기에도 올립니다
[code]
if (!function_exists('json_encode')) {
function json_encode($data) {
switch ($type = gettype($data)) {
case 'NULL':
return 'null';
case 'boolean':
return ($data ? 'true' : 'false');
case 'integer':
case 'double':
case 'float':
return $data;
case 'string':
return '"' . addslashes($data) . '"';
case 'object':
$data = get_object_vars($data);
case 'array':
$output_index_count = 0;
$output_indexed = array();
$output_associative = array();
foreach ($data as $key => $value) {
$output_indexed[] = json_encode($value);
$output_associative[] = json_encode($key) . ':' . json_encode($value);
if ($output_index_count !== NULL && $output_index_count++ !== $key) {
$output_index_count = NULL;
}
}
if ($output_index_count !== NULL) {
return '[' . implode(',', $output_indexed) . ']';
} else {
return '{' . implode(',', $output_associative) . '}';
}
default:
return ''; // Not supported
}
}
}
[code]
그조화된 데이터는
http://json.parser.online.fr/
이곳에서 확인해보실수있습니다.
댓글 6개
13년 전
너무 좋은 걸 올려주셨는데요...참고할만한 샘플 파일이라도 볼수 없을까요?
초보자인 저로서는 어떻게 대입해서 써야할지...어려운 부분이 있네요
초보자인 저로서는 어떻게 대입해서 써야할지...어려운 부분이 있네요
13년 전
네 요즘 많은 곳에서 이렇게 하죠 테이블 필드수를 줄이기 위해서요..
13년 전
일부 post 값들만 json-encode, decode 할 수 없을까요?
자바스크립트 종류인줄 알았는데 php 이지요?
자바스크립트 종류인줄 알았는데 php 이지요?
13년 전
일부값들만 그렇게 할수있죠. php입니다
근데 귀찮아서 일일이 지정 안할뿐 데이터가 많다 싶으면 일일이 저정해주어야 합니다.
근데 귀찮아서 일일이 지정 안할뿐 데이터가 많다 싶으면 일일이 저정해주어야 합니다.
13년 전
http://kr1.php.net/manual/en/function.serialize.php
http://kr1.php.net/manual/en/function.unserialize.php
저는 serialize랑 unserialize도 괜찮더라구요.
http://kr1.php.net/manual/en/function.unserialize.php
저는 serialize랑 unserialize도 괜찮더라구요.
11년 전
검색이 필요없는 데이터라고 하셨는데 원본내용이 바뀌는건가요?
게시판 목록
그누4 팁자료실
그누보드4와 관련된 팁을 여러분들과 함께 공유하세요.
나누면 즐거움이 커집니다.
나누면 즐거움이 커집니다.
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 2849 | 13년 전 | 2897 | ||
| 2848 |
|
13년 전 | 10112 | |
| 2847 | 13년 전 | 5335 | ||
| 2846 | 13년 전 | 3663 | ||
| 2845 |
|
13년 전 | 8169 | |
| 2844 |
smokingbird
|
13년 전 | 6627 | |
| 2843 | 13년 전 | 8154 | ||
| 2842 |
하하369
|
13년 전 | 5588 | |
| 2841 |
정박사닷컴
|
13년 전 | 4968 | |
| 2840 | 13년 전 | 4034 | ||
| 2839 | 13년 전 | 6607 | ||
| 2838 |
|
13년 전 | 2971 | |
| 2837 |
하하369
|
13년 전 | 6023 | |
| 2836 |
EoDee
|
13년 전 | 4404 | |
| 2835 | 13년 전 | 6066 | ||
| 2834 | 13년 전 | 3814 | ||
| 2833 | 13년 전 | 4037 | ||
| 2832 | 13년 전 | 6995 | ||
| 2831 | 13년 전 | 15578 | ||
| 2830 |
|
13년 전 | 4037 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기