php array json 방법 문의

php array json 방법 문의

QA

php array json 방법 문의

답변 2

본문

안녕하세요.

 

어떤 게시판에 추천을 누른 회원을 뽑아올때에 PHP에서 요청을 받아서 JSON으로 뿌려줄대 반복문을 통해서 json을 길게 보내주고 싶은데, 도와주시면 감사하겠습니다.

 


$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']) {
        $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);
    }
}

 

위 처럼 반복문을 통해서 데이터를 쌓아 한번에 어떻게 보내야 하나요?

아래의 json 유형 처럼 보내지길 원해요. 아래는 예제이니 데이터는 무시...

 


 [
    {
        "firstName": "길동",
        "lastName": "홍",
        "email": "이메일",
        "mobile": "번호"
    },
    {
        "firstName": "민아",
        "lastName": "김",
        "email": "이메일",
        "mobile": "번호"
    },
    {
        "firstName": "진주",
        "lastName": "마",
        "email": "이메일",
        "mobile": "번호"
    },
    {
        "firstName": "서영",
        "lastName": "이",
        "email": "이메일",
        "mobile": "번호"
    }
]

 

이렇게 가져오면 저는 jquery each를 통해서 반복문으로 출력할려고 합니다.

이 질문에 댓글 쓰기 :

답변 2

 

모든 결과를 다 배열에 넣고 출력 부분에서 json_encode() 를 사용하시면 됩니다.


$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 출력후 종료

 

<?

echo "[";

while( db sql조건){

 

if( $row마지막줄 아닐때){

  echo "{";

  echo $row["name"].",";

  echo $row["name"].",";

  echo $row["name"];

  echo "},";

}else{

  echo "{";

  echo $row["name"].",";

  echo $row["name"].",";

  echo $row["name"];

  echo "}";

}

 

}

 

echo "]";

?>

 

 

답변을 작성하시기 전에 로그인 해주세요.
QA 내용 검색
질문등록
전체 28
© SIRSOFT
현재 페이지 제일 처음으로