2026, 새로운 도약을 시작합니다.

json_encode() 배열 값 출력 질문 채택완료

ajax.php

Copy


        $address    = array( '전체','서울','경기','인천','대전','대구','부산','광주','울산','세종','강원','충북','충남','전북','전남','경북','경남','제주' );

        $address_2  = array( '','서울시','경기도','인천시','대전시','대구시','부산시','광주시','울산시','세종시','강원도','충청북도','충청남도','전라북도','전라남도','경상북도','경상남도','제주도' );

        $address_3  = array( '','서울특별시','','인천광역시','대전광역시','대구광역시','부산광역시','광주광역시','울산광역시','세종특별자치시','강원특별자치도','','','','','','','제주특별자치도');//도시찾기 버튼으로 검색시

        if($find_sido && $find_sigugun){ //select로 검색시

            $sql    = "select *

            from sw_machine_holder

           where machine_address like '$find_sido%' '$find_sigugun%'

           or machine_address2 like '$find_sido%' '$find_sigugun%'

           and mapLatitude > '' and

           mapLongitude > '' and

           petroom = 'Y'

           GROUP BY petroom_name

           ";

    $row = "SELECT count(count) as total

    FROM (SELECT count(petroom_name) as count FROM sw_machine_holder

    WHERE petroom = 'Y' and machine_address like '$find_sido%' '$find_sigugun%' or machine_address2 like '$find_sido%' '$find_sigugun%' GROUP BY petroom_name)

    as count";

       

        }else if( $find_machine ){ //text로 지점명 입력 검색시

            // 우선 시 / 도 에있는 값인지 확인

            if(in_array($find_machine ,$address)){

                $sql    = "select *

                    from sw_machine_holder

                    where machine_address like '$find_machine%'

                    and mapLatitude > '' and

                    mapLongitude > '' and

                    petroom = 'Y'

                    GROUP BY petroom_name

               ";

            } else {

                $sql    = "select *

                    from sw_machine_holder

                    where (machine_address REGEXP '$find_machine' or machine_address2 REGEXP '$find_machine') and petroom = 'Y'

                    GROUP BY petroom_name

               ";

            }

             $row = "SELECT count(count) as total

             FROM (SELECT count(petroom_name) as count FROM sw_machine_holder

             WHERE petroom_name like '%$find_machine%'

                        or machine_address like '$find_machine%'

                        and mapLatitude > '' and

                        mapLongitude > '' and

                        petroom = 'Y'

               GROUP BY petroom_name)

             as count";

        } else if( $find_address ) { //도시찾기 버튼으로 검색시

                if($find_address == "전체"){

                    $sql    = "select *

                    from sw_machine_holder

                    where mapLatitude > '' and

                    mapLongitude > '' and

                    petroom = 'Y'

                    GROUP BY petroom_name

                    ";

                    $row = "SELECT count(count) as total

                    FROM (SELECT count(petroom_name) as count FROM sw_machine_holder

                    WHERE petroom = 'Y'

                   and mapLatitude > '' and

                   mapLongitude > '' GROUP BY petroom_name)

                       as count";

                }else {

                $j  = array_search( $find_address, $address ) === false ? ( array_search( $find_address, $address_2 ) === false ? array_search( $find_address, $address_3 ) : array_search( $find_address, $address_2 ) ) : array_search( $find_address, $address );

   

                if ( $j !== false ) {

                    $fine_address_like  = " and ( ";

   

                    if ( $address[$j] > '' ) {

                        $fine_address_like  .= "machine_address like '" . $address[$j] . "%'";

        "               or machine_address like '" . $address_2[$j] . "%' or machine_address like '" . $address_3[$j] . "%' )";

                    }

                    if ( $address_2[$j] > '' ) {

                        if ( $fine_address_like != "( " ) {

                            $fine_address_like  .= " or ";

                        }

                        $fine_address_like  .= "machine_address like '" . $address_2[$j] . "%'";

                    }

                    if ( $address_3[$j] > '' ) {

                        if ( $fine_address_like != "( " ) {

                            $fine_address_like  .= " or ";

                        }

                        $fine_address_like  .= "machine_address like '" . $address_3[$j] . "%'";

                    }

                    if ( $fine_address_like != "( " ) {

                        $fine_address_like  .= " ) ";

                    }

                }

   

                $sql    = "select *

                             from sw_machine_holder

                            where

                            mapLatitude > '' and

                            mapLongitude > '' and

                            petroom = 'Y'

                            $fine_address_like

                            GROUP BY petroom_name

                            ";

                  $row   = "SELECT count(count) as total

                  FROM (SELECT count(petroom_name) as count FROM sw_machine_holder

                  WHERE petroom = 'Y' and machine_address like '$find_address%' GROUP BY petroom_name)

                  as count";    

               $status = "select * from sw_machine_holder where petroom = 'Y' and admin_status = '이용가능'";  

                                  }

            } else { //위 세가지가 안먹을 시

            $sql    = "select *

                         from sw_machine_holder

                         where mapLatitude > '' and

                         mapLongitude > '' and

                         petroom = 'Y' and

                         petroom_name != ''

                         GROUP BY petroom_name ";

                                     

                         $row = "SELECT count(count) as total

                    FROM (SELECT count(petroom_name) as count FROM sw_machine_holder

                    WHERE petroom = 'Y'

                   and mapLatitude > '' and

                   mapLongitude > '' GROUP BY petroom_name)

                       as count";

    }

    $query  = sql_query( $sql );

    $machine_count = sql_query($row);

    $mapNameArray   = array();

    $statusArray    = array();

    $latArray   = array();

    $lotArray   = array();

    $arr   = array();

    $arr["data"]   = array();

 

    for($i=0; $row=sql_fetch_array($query); $i++){

 

        $find_status_sql    = sql_fetch("select * from sw_machine_data where petroom = 'Y' and data2 = '{$row['machine_uuid']}' order by idx desc limit 1");

        $find_status    = $find_status_sql['data1'];

        $make_status    = explode(' ', $find_status);

 

                   $mapNameArray[$i]    = $row['petroom_name'];

                   $latArray[$i]    = $row['mapLatitude'];

                   $lotArray[$i]    = $row['mapLongitude'];

 

                   $status_check = sql_fetch("select petroom_name from sw_machine_holder where petroom = 'Y' and admin_status != '사용불가능' and petroom_name ='$mapNameArray[$i]'");

 

                 if($status_check['petroom_name'] == $mapNameArray[$i]){

                     $statusArray[$i] = "이용가능";

                     $row['admin_status'] = "이용가능";

                } else {

                     $statusArray[$i] = "이용불가";

                     $row['admin_status'] = "이용불가";

                 }

                }

 array_push( $arr["data"], $mapNameArray);

 array_push( $arr["data"], $latArray);

 array_push( $arr["data"], $lotArray);

 array_push( $arr["data"], $statusArray);

 

 //var_dump( $arr );

//  echo json_decode( $arr );

 //$data = json_decode( $arr );

 echo json_encode( $arr );

?>  

form안에 있는 value값들을 ajax로 받아서 for문을 돌려 나온 값들을 json 배열에 담아 보냈습니다.

Copy


 $.ajax({

            type: "POST",

            url: ajax_url,

            data: frmData,

            dataType: "JSON",

            success: function(response) {

               

                console.log(response);

            },

이렇게 console을 찍어보니 

1889015246_1691507604.3079.png

이렇게 찍히는데 문제는 나온 배열 값들을

Copy

이렇게 출력할수는 없나요?? 위에 코드도 생략되었지만 ajax 페이지와 같이 for문을 돌렸을때 원하는 값을 찾기 위한 코드입니다.

아니면 ajax처리에서 배열로 보내면 안되는것이었나요..

아니면 console로 찍힌 저 배열들을 <script>인데 어떻게 출력해야할지 아예 감이 안잡힙니다ㅠㅠ 

답변 4개

채택된 답변
+20 포인트

$.each(dada.function(i,$i) {

   $.each($i,functin(j,$j) {

        $j 가 대구, 좌표값, 좌표값, 이용가능 식으로 차례대로 나옵니다.

    }

}

위 반복문으로 html을 만들어서 원하는 위치에 넣으시면 됩니다.

모바일이라 자세히 적지는 못하겠네요

로그인 후 평가할 수 있습니다

댓글을 작성하려면 로그인이 필요합니다.

form으로 하는건 값만 체크하시라고 답변드린거에요..
값 체크후에는 ajax로 처리 하세요.

로그인 후 평가할 수 있습니다

댓글을 작성하려면 로그인이 필요합니다.

ajax 로 처리 하는 부분을 주석처리하시고 일반 form으로 submit 해서 값을 확인하시고 ajax로 처리하는게 간편합니다.

로그인 후 평가할 수 있습니다

답변에 대한 댓글 1개

form으로 하면 reload가 되는걸 원치않아서 ajax로 처리하려는데 방법이나 참고 사이트를 알수 있을까요??

댓글을 작성하려면 로그인이 필요합니다.

로그인 후 평가할 수 있습니다

댓글을 작성하려면 로그인이 필요합니다.

답변을 작성하려면 로그인이 필요합니다.

로그인
🐛 버그신고