Smudger
Smudger

Reputation: 10791

Codeigniter return and display the array result of model function, called by jquery, on my view

I am using codeigniter in my app. I have an autocomplete drop down that on selection calls a controller function to display further details about the selected dropdown value.

I have used the below syntax successfully to return a single result but now need to return an entire array to the view and display each returned row with multiple columns.

So the syntax for the single result is:

Controller

function get_link_load_options(){

    $this->load->model('Sales_model');
    if (isset($_POST['data'])){
        $q = strtolower($_POST['data']);
        $data = $this->Sales_model->get_link_load_options($q);
        $this->output->set_content_type('application/json')->set_output(json_encode($data));
    }
}

Model

function get_link_load_options($q){
    $this->db->select('TotalCubes,CustomerName,TotalCubes, TreatedCubes');
    $this->db->where('CustomerOrderID', $q);
    $query = $this->db->get('Customer_Order_Summary');
    if($query->num_rows > 0){
        foreach ($query->result_array() as $row){
            $row_set[] = htmlentities(stripslashes($row['TotalCubes'])); 
       return $row_set;
    }
  }

View Jquery

   $.post('get_link_load_options', {data:substr[1]},function(result) { 
   $('input[name^="totalcubes"]').val(result[0]);
   }); 

So how do I adapt/change this to populate my view with the get_link_load_options function results.

Is this the best method or should I be using another method? Can the entire array $query be passed and if so how do I use jquery / javascript to render the array into a table?

When I do the above, I get the response: Message: json_encode(): type is unsupported, encoded as null

Thanks as always.

Upvotes: 0

Views: 2203

Answers (1)

Muhammad Raheel
Muhammad Raheel

Reputation: 19882

You dont need json for this you can simply do it like this. Just create a div which will be empty at first place. Your jquery result will fill it then like this

Controller

function get_link_load_options(){
    $this->load->model('Sales_model');
    if (isset($_POST['data'])){
        $q = strtolower($_POST['data']);
        $data['result'] = $this->Sales_model->get_link_load_options($q);
        $this->load->view('myview',$data);
    }
}

JQuery

$.post('get_link_load_options', {data:substr[1]},function(result) { 
   $('#div_id').html(result);
}); 

Upvotes: 1

Related Questions