DB연동 결과물 출력

DB연동 결과물 출력

QA

DB연동 결과물 출력

답변 2

본문

서브 셀렉트 박스(select_zero -> select_first -> select_second)를 누른 후에 g당 단가 테이블에 sql에 맞는 조건의 결과물을 출력하고 싶은데 select_zero와 select_first의 결과값이 나옵니다....

 

 

2039588259_1697378403.9237.png

 

 

 

2039588259_1697378441.8998.png

 

3번째 select 까지 선택하게 되면 아래와 같이 g당 단가에 결과 같이 나옵니다.

2039588259_1697378500.4392.png

 

db에 저장된 값을 넣고 싶습니다. 어떻게 해야하나요?? 

 

 

index.php 

 

<script>

$(document).ready(function(){

      $('#select_zero').on('change', function(){

          var select_zero = $(this).val();

          if(select_zero){

              $.ajax({

                  type:'POST',

                  url:'./ajax/information_one.php',

                  data:'select_zero='+select_zero,

                 

                  success:function(html){

                      $('#select_first').html(html);

                      $('#select_second').html('<option value="">선택</option>');

                  }

              });

          }else{

              $('#select_first').html('<option value="">선택</option>');

              $('#select_second').html('<option value="">선택</option>');

          }

      });

     

      $('#select_first').on('change', function(){

          var select_first = $(this).val();

          if(select_first){

              $.ajax({

                  type:'POST',

                  url:'./ajax/information_one.php',

                  data:'select_first='+select_first,

                  success:function(html){

                      $('#select_second').html(html);

                      $('#price_per_gram').html('0');

                  }

              });

          }else{

              $('#select_second').html('<option value="">선택</option>');

              $('#price_per_gram').html('0');

          }

      });

 

      $('#select_second').on('click', function(){

        var select_second = $(this).val();

        if(select_second){

          var select_first = $('#select_first').val();

          var select_zero = $('#select_zero').val();

 

          $.ajax({

                  type:'POST',

                  url:'./ajax/information_one.php',

                  data: {

                      select_second: select_second,

                      select_first: select_first,

                      select_zero: select_zero

                  },

                  success:function(html){

                      $('#price_per_gram').html(html);

                  }

              });

          }else{

              $('#price_per_gram').html('0');

          }

    });

});

</script>

 

 

------------------------------------------

information_one.php

 

 

<?php

include "../db_connect.php";

 

$select_zero = $_POST['select_zero'];

$select_first = $_POST['select_first'];

$select_second = $_POST['select_second'];

 

if (!empty($select_zero)) {

   

    $sql = "SELECT distinct 사종 FROM information WHERE 염색 = '$select_zero' AND yesno = 1";

    $result = mysqli_query($con, $sql);

    echo '<option value ="">선택 </option>';

 

    if($result ->num_rows>0){

    while ($row = mysqli_fetch_assoc($result)) {

        echo '<option value="'.$row['사종'].'">'.$row['사종'].'</option>';

    }

   

    }else {

        echo '<option value ="">선택 </option>';

    }

}

elseif (!empty($select_first)) {

   

    $sql = "SELECT 품명 FROM information WHERE yesno = 1 and 사종 = '$select_first'";

    $result = mysqli_query($con, $sql);

    echo '<option value ="">선택 </option>';

 

    if($result ->num_rows>0){

    while ($row = mysqli_fetch_assoc($result)) {

       

        echo '<option value="'.$row['품명'].'">'.$row['품명'].'</option>';

    }

    }else {

        echo '<option value ="">선택 </option>';

    }

}

 

elseif (!empty($select_second)) {

   

    $sql = "SELECT G당_단가 FROM information WHERE yesno = 1 and 사종 = '$select_first' and 품명 = '$select_second'";

    $result = mysqli_query($con, $sql);

    echo '0';

 

    if($result ->num_rows>0){

        $row = mysqli_fetch_assoc($result);

        $price_per_gram = $row['G당_단가'];

        echo $price_per_gram;

    } else {

        echo '0';

    }

}

?>

 

 

 

이 질문에 댓글 쓰기 :

답변 2


elseif (!empty($select_second)) {
    $sql = "SELECT G당_단가 FROM information WHERE yesno = 1 and 사종 = '$select_first' and 품명 = '$select_second'";
    $result = mysqli_query($con, $sql);
    echo $sql.'<br>'; // sql 요청이 어떻게 되는지 확인해 보세요
    if($result){
        $row = mysqli_fetch_assoc($result);
        $price_per_gram = $row['G당_단가'];
        echo $price_per_gram;
    } else {
        echo '0';
    }
}

echo $sql.'<br>';  이 줄을 추가하여 어떻게 요청이 되었는지 확인해 보세요

다음과 같은 방법으로 시도해 볼 수 있을 것 같습니다.

참고하셔서 원하시는 형식으로 구현하시면 될 것 같습니다.

 

index.php의 JavaScript 코드를 수정.

select_second의 값이 변경될 때 "g당 단가"를 가져오도록 설정.

 


$('#select_second').on('change', function(){
    var select_second = $(this).val();
    if (select_second) {
        var select_first = $('#select_first').val();
        var select_zero = $('#select_zero').val();
        $.ajax({
            type: 'POST',
            url: './ajax/information_one.php',
            data: {
                select_second: select_second,
                select_first: select_first,
                select_zero: select_zero
            },
            success: function(html) {
                $('#price_per_gram').html(html);
            }
        });
    } else {
        $('#price_per_gram').html('0');
    }
});

 

information_one.php를 수정.

g당 단가를 가져오는 SQL 쿼리를 조건에 맞게 수정.

 


elseif (!empty($select_second)) {
    $sql = "SELECT G당_단가 FROM information WHERE 염색 = '$select_zero' AND 사종 = '$select_first' AND 품명 = '$select_second' AND yesno = 1";
    $result = mysqli_query($con, $sql);
    if ($result->num_rows > 0) {
        $row = mysqli_fetch_assoc($result);
        $price_per_gram = $row['G당_단가'];
        echo $price_per_gram;
    } else {
        echo '0';
    }
}
답변을 작성하시기 전에 로그인 해주세요.
전체 4
© SIRSOFT
현재 페이지 제일 처음으로