CodeIgniter Forums
Return array to AJAX - 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: Return array to AJAX (/showthread.php?tid=7382)



Return array to AJAX - El Forum - 04-06-2008

[eluser]iamdesign[/eluser]
Hey there,

I was wondering how I could return an arry to an resulthandler of AJAX.
I'm using jQuery and this is my code

Code:
function getGroupsBySubtypeId($id){
        $query = $this->db->query('SELECT * FROM photo_group WHERE subtype_id="'.$id.'"');
        
        $result = $query->result_array();
        
    }

this returns the following data

Code:
Array

(

    [0] => Array

        (

            [group_id] => 10

            [subtype_id] => 3

            [name] => Köln

        )

    [1] => Array

        (

            [group_id] => 11

            [subtype_id] => 3

            [name] => Paris

        )

    [2] => Array

        (

            [group_id] => 12

            [subtype_id] => 3

            [name] => Kortrijk

        )

)

Now i need this to fill up a select-box, but i can't find a way of doing it Undecided

somebody who knows how to deal with this problem Smile?

thnx in advance,
cheers


Return array to AJAX - El Forum - 04-06-2008

[eluser]Derek Allard[/eluser]
yeah, CI returns a multi-dimensional array, and that's not super helpful. There are 2 ways to approach this, but I'd try this one first. Set your model up as
Code:
function getGroupsBySubtypeId($id)
{
        // the actual query
        $this->db->where('subtype_id', $id)
        $query = $this->db->get('photo_group');

        // a blank array, this will hold our constructed results
        $photos = array();
        
        if ($query->num_rows() >= 1)
        {
            foreach ($query->result() as $photo)
            {
                // build the array
                $photos[$photo->group_id] = $photo->name;            
            }
            
        }
        else
        {
            return FALSE; // you may want to get more clever here...
        }
        
        return $photos;
}

now in your controller, it'd look like
Code:
function somepage()
{
    $this->load->model('photos_model');
    $this->load->helper('form');

    $id = $this->input->get_post('whatever_your_field_is_called');
    
    $data['photos_dropdown'] = form_dropdown('photos', $this->photos_model->getGroupsBySubtypeId($id);
    $this->load->view('the_view', $data);
}

You may need to adapt some of it for your specific application, but I think the general idea is here.

One thing I'm not really hitting is how to return this data using "AJAX". The reason is because there are so many things this might look like.


Return array to AJAX - El Forum - 04-06-2008

[eluser]richthegeek[/eluser]
well AJAX implies XML - therefore changing that structure into XML using this function:
Code:
function array_to_xml( $input, $level) {
    $pre = "\n".str_repeat("    ",($level));
    foreach( $input as $key=>$child ) {
        if( is_array( $child ) ) {
            $output .= $pre."<".$key.">".array_to_xml($child, $level+1).$pre."</".$key.">";
        } else {
            $output .= $pre."<".$key.">".$child."</".$key.">";
        }
    }
    return $output;
}

Then with jQuery you can do (remember to return as Content-type:text/xml):
Code:
function( data ) {
$(data).children().each( function() {
   var group_id = $(this).find("group_id").html();
   var subtype_id = $(this).find("subtype_id").html();
   var xname = $(this).find("name").html();

   // do any append() etc here

})
}
Note, I used "xname" because "name" is semi-restricted and can cause cock-ups in various browsers.


Return array to AJAX - El Forum - 04-07-2008

[eluser]gon[/eluser]
You should convert the array to JSON, and send it to the browser.
Then just evaluate it and you get the data as a javascript object,
so you can populate the select.

And you can change the array structure before converting it to JSON,
to make the JSON data easier to use with javascript.

Cheers.


Return array to AJAX - El Forum - 04-07-2008

[eluser]iamdesign[/eluser]
ow thanks alot! great info

i just got into ajax through jquery so i didn't know all the possibilities :-)

tnx for the help!