php array json 방법 문의 채택완료
검은냥냥이
6년 전
조회 3,744
안녕하세요.
어떤 게시판에 추천을 누른 회원을 뽑아올때에 PHP에서 요청을 받아서 JSON으로 뿌려줄대 반복문을 통해서 json을 길게 보내주고 싶은데, 도와주시면 감사하겠습니다.
</p>
<p>$sql = sql_query("select * from {$g5['board_good_table']} where bo_table = '{$bo_table}' and wr_id = '{$wr_id}' order by bg_datetime asc");
foreach ($sql as $row) {
$row2 = sql_fetch("select mb_id, mb_nick, mb_profile from {$g5['member_table']} where mb_id = '{$row['mb_id']}'");</p>
<p> </p>
<p> if ($row2['mb_id'] != $member['mb_id']) {
$array = json_encode(array('confirmation' => 'success', 'completion' => number_format($end - $start, 4), 'before' => $before, 'after' => $after, 'id' => $row2['mb_id'], 'nick' => $row2['mb_nick'], 'value' => number_format($count['wr_good'])), true);
}
}</p>
<p>
위 처럼 반복문을 통해서 데이터를 쌓아 한번에 어떻게 보내야 하나요?
아래의 json 유형 처럼 보내지길 원해요. 아래는 예제이니 데이터는 무시...
</p>
<p> [
{
"firstName": "길동",
"lastName": "홍",
"email": "이메일",
"mobile": "번호"
},
{
"firstName": "민아",
"lastName": "김",
"email": "이메일",
"mobile": "번호"
},
{
"firstName": "진주",
"lastName": "마",
"email": "이메일",
"mobile": "번호"
},
{
"firstName": "서영",
"lastName": "이",
"email": "이메일",
"mobile": "번호"
}
]</p>
<p>
이렇게 가져오면 저는 jquery each를 통해서 반복문으로 출력할려고 합니다.
댓글을 작성하려면 로그인이 필요합니다.
답변 2개
채택된 답변
+20 포인트
6년 전
모든 결과를 다 배열에 넣고 출력 부분에서 json_encode() 를 사용하시면 됩니다.
</p>
<p>$results = array();
$sql = sql_query("select * from {$g5['board_good_table']} where bo_table = '{$bo_table}' and wr_id = '{$wr_id}' order by bg_datetime asc");
foreach ($sql as $row) {
$row2 = sql_fetch("select mb_id, mb_nick, mb_profile from {$g5['member_table']} where mb_id = '{$row['mb_id']}'");
if ($row2['mb_id'] != $member['mb_id']) {
$results[] = array('confirmation' => 'success', 'completion' => number_format($end - $start, 4), 'before' => $before, 'after' => $after, 'id' => $row2['mb_id'], 'nick' => $row2['mb_nick'], 'value' => number_format($count['wr_good']));
}
}
die(json_encode($results)); // json string 출력후 종료</p>
<p>
로그인 후 평가할 수 있습니다
답변에 대한 댓글 1개
�
검은냥냥이
6년 전
댓글을 작성하려면 로그인이 필요합니다.
답변을 작성하려면 로그인이 필요합니다.
로그인
제가 찾은 솔루션과 동일한 방법입니다.
감사합니다.