2017-11-24 2 views
0

동적 종속 드롭 다운 목록을 CI에 표시하려고합니다. 아래 동적 json을 만들지 만 PHP 형식으로 변환하는 방법을 이해하지 못하고 있습니다.PHP에서 동적 json을 만드는 방법은 무엇입니까?

{ 
    "salary": [ 
     { 
      "id": 0, 
      "text": 2017, 
      "data": {}, 
      "children": [ 
       { 
        "id": 1, 
        "text": "May" 
       }, 
       { 
        "id": 2, 
        "text": "July" 
       }, 
       { 
        "id": 3, 
        "text": "August" 
       }, 
       { 
        "id": 4, 
        "text": "September" 
       }, 
       { 
        "id": 5, 
        "text": "October" 
       } 
      ] 
     } 
    ] 
} 

다음은이 코드를 구현하려는 PHP 코드입니다. text = 2017이 동일한 경우 PAY_YEAR IS SAME 인 경우 children part.it에있는 모든 달이 PAY_MONTH 부분에 표시되어야합니다.

좋아요 : json에서는 2017이 어린이의 손에 들어 있고 5 개월 안에 볼 수 있습니다. 따라서 사용자가 드롭 다운에서 2017을 선택하면 pay_month의 다른 드롭 다운에서 모든 달이 오게됩니다.

<form action="#" method="get" class="form-horizontal"> 
    <div class="control-group" style="float: left;"> 
     <label class="control-label">Select Year</label> 
     <div class="controls"> 
      <select> 
       <?php 
       foreach ($salary as $row) {?> 
       <option><?php echo $row->PAY_YEAR;}?></option> 
      </select> 
     </div> 
    </div> 
    <div class="control-group"> 
     <label class="control-label">Select Month</label> 
     <div class="controls"> 
      <select> 
       <?php 
       foreach ($salary as $row) {?> 
       <option><?php echo $row->PAY_MONTH;}?></option> 
      </select> 
     </div> 
    </div> 
    <div class="form-actions"> 
     <button type="submit" class="btn btn-success">Search</button> 
     <button type="submit" class="btn btn-success">Cancel</button> 
    </div> 
</form> 

여러분이 저를 이해하기를 바랍니다.

+0

왜이 용도로 클라이언트 측 스크립트 (javascript/jQuery)를 사용할 수 없습니까? – Sarath

+0

나는이 json을 treeview 구조에서 동적 인 것으로 이미 사용했기 때문에 이것을 사용해야 만한다. –

+0

예 @amit sutar는 JS에 값을 전달하고 드롭 다운을 .. 더 편리하게 할 것이다. – Sarath

답변

0

나는 이렇게했습니다.

먼저 두 드롭 다운에 모두 id를 추가하십시오. 올해 드롭 다운을 위해 급여 드롭 다운를 들어 ID = "year_select" 를 추가 것은 당신이 요구하는 것을 아주 확실하지 않다 ID = "sal_select"

$("#year_select").change(function() { 
 
\t $.getJSON('json_data.txt', function(read_data) { 
 
\t \t var data = read_data.salary[0].children; 
 
\t \t var str_opt = "<option value=''></option>"; 
 
\t \t $.each(data, function(i, item) { 
 
\t \t \t str_opt += "<option value='"+data[i].id+"'>"+data[i].id+"</option>"; 
 
\t \t }); 
 
\t \t $('#sal_select').html(str_opt); 
 
\t }); 
 
});

+0

그것은 오류를 보여줍니다 –

0

을 추가 할 수 있지만 당신이 필요로하는 경우 변환 JSON에서 PHP 변수 (배열로 변환)는 json_decode($json)이고 다른 방향은 json_encode($variable)입니다.

PHP manual reference for encode

편집

PHP manual reference for decode

: 지금 나는 당신이 묻는 게 아니에요을 참조하십시오. 나는 this과 같은 기능이 인 것을 제외하고는 this이 도움이 될 것이라고 생각한다.