2017-11-24 2 views
0

Ich möchte dynamische abhängige Dropdown-Liste in CI anzeigen. Ich erstelle unter dynamischen JSON, aber ich verstehe nicht, wie man dies in PHP-Format konvertiert.Wie erstellt man dynamische JSON in PHP?

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

Unten ist mein PHP-Code, wo ich diesen Code implementieren möchte. Wenn text = 2017 ist gleichbedeutend bedeutet PAY_YEAR ist gleich, dann wird es alle Monate, die in seiner children part.it angezeigt werden soll im PAY_MONTH Teil angezeigt werden.

Wie: in Json können wir sehen, 2017 ist da und in Kinder Teil 5 Monate drin. Also, wenn ein Benutzer 2017 von Dropdown wählen wird, dann in einem anderen Dropdown von pay_month alle Monate kommen sollten.

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

Ich hoffe, Sie verstehen mich.

+0

Warum können Sie nicht clientseitige Skript (javascript/jQuery) für dieses ..? – Sarath

+0

Ich habe bereits diese Json für dynamische Sache in treeview Struktur verwendet, deshalb muss ich diese verwenden –

+0

ja @amit sutar übergeben Sie einfach den Wert zu JS und manupuliere das Dropdown .. das wird convinient sein – Sarath

Antwort

0

Ich habe so etwas getan.

Fügen Sie zunächst die ID für das Dropdown-Menü hinzu. Für das Jahr Drop-Down-Add id = "year_select" Für Gehalt Drop-Down-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

es zeigt Fehler –

0

Ich bin nicht ganz sicher, fügen Sie, was Sie fordern, aber wenn Sie konvertieren JSON to PHP-Variable (verwandelt es in ein Array) können Sie json_decode($json) und die andere Richtung json_encode($variable) verwenden.

PHP manual reference for decode

PHP manual reference for encode

Edit: Jetzt sehe ich, das ist nicht das, was Sie fordern. Ich denke, this wird helfen, außer mit einem XMLHttpRequest in der changecat(value) Funktion wie this.

Verwandte Themen