CodeIgniter Forums
how to interpret code igniter record results with JSON - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: how to interpret code igniter record results with JSON (/showthread.php?tid=42270)



how to interpret code igniter record results with JSON - El Forum - 06-01-2011

[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;
        }
    }



how to interpret code igniter record results with JSON - El Forum - 06-01-2011

[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();

//...