0

I want to display dynamic dependent dropdown list in CI. I create below dynamic json but I am not understanding how to convert this to php format.

{
    "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"
                }
            ]
        }
    ]
}

Below is my PHP code where I want to implement this code. If text = 2017 is same means PAY_YEAR IS SAME then in it will display all months that are in its children part.it should be displayed in PAY_MONTH part.

Like: in json we can see 2017 is there and in children part 5 months inside it. So, if a user will select 2017 from dropdown then in another dropdown of pay_month all months should come.

<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>

I hope you guys understand me.

Vaibhav Bhavsar
  • 432
  • 4
  • 12
amit sutar
  • 541
  • 2
  • 11
  • 37
  • Why can't you use client side script (javascript/jQuery) for this..? – Sarath Nov 24 '17 at 11:26
  • i already uused this json for dynamic thing in treeview structure thats why i have to use this – amit sutar Nov 24 '17 at 11:32
  • yes @amit sutar just pass the value to JS and manupulate the Drop-down.. that will be more convinient – Sarath Nov 24 '17 at 11:33
  • sure i will try this but i dont about javascript much. can you give me some hints or code – amit sutar Nov 24 '17 at 11:42
  • 1
    first, fill the year drop down with php.. and while selecting fill initiate AJAX request to get a list of Months and feed it in Months Drop-down – Sarath Nov 24 '17 at 11:48

2 Answers2

0

I have done something like this.

First add id for both the dropdown. For year dropdown add id="year_select" For salary dropdown add id="sal_select"

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

I'm not quite certain what you are asking, but if you need to convert JSON to PHP variable (turns it into an array) you can use json_decode($json) and the other direction json_encode($variable).

PHP manual reference for decode

PHP manual reference for encode

Edit: Now I see that's not what you are asking. I think this will help, except using a XMLHttpRequest in the changecat(value) function like this.