• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to interpret code igniter record results with JSON

#1
[eluser]heaversm[/eluser]
Hi - I currently have a form setup that, depending on the user's dropdown selection, finds a specific table row, and returns that data back to a view as a JSON object. But how do I access that JSON Object's properties (columns of the table row)? Here's my code:

VIEW:
Code:
[removed]
             $(document).ready(function() {
                $('#f_treeindex').change(function(){
                    var tree_id = $('#f_treeindex').val();
                    if (tree_id != ""){
                        var post_url = "/index.php/control_form/get_tree/" + tree_id;
                        $.ajax({
                            type: "POST",
                             url: post_url,
                             success: function(tree)
                              {
                                alert (tree)
                               } //end success
                         }); //end AJAX
                    }//end if
                }); //end change
             }); //end docready
        [removed]

CONTROLLER:

Code:
function get_tree($tree){
        $this->load->model('Model_form','', TRUE);
        header('Content-Type: application/x-json; charset=utf-8');
        echo(json_encode($this->Model_form->get_tree_by_id($tree)));
    }


MODEL:
Code:
function get_tree_by_id($tree = null){
        $this->db->select('id, tree_name, tree_desc');
        
        if($tree != NULL){
            $this->db->where('id', $tree); //conditions
        }
        $query = $this->db->get('trees'); //db name
        
        if($query->result()){
            $tree_result = $query->row();
            return $tree_result;
        }
    }

#2
[eluser]toopay[/eluser]
In the first place, you should returning an array instead an object. In your model
Code:
//...

/ * Turn off this
if($query->result()){
            $tree_result = $query->row();
            return $tree_result;
        }
*/

return $query->result() ? $query->result_array() : array();

//...


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.