0

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.

1
  • i use only echo json_encode($data); for ajax in my service, and probably you can pass $query result directly to the json_encode function. Commented May 20, 2013 at 14:09

1 Answer 1

1

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);
}); 
Sign up to request clarification or add additional context in comments.

6 Comments

Hi Raheel, Thanks for helping me...again! so when I look at the server response that I am getting from your code, it is actually loading an entire page worth of source code? Should 'myview' in your example be a new page that will be loaded inside the div? then I can access the $data content as per normal controller / view functions? or am I way off the mark? Thanks again.
@Smudger you are right you can simply load a view through controller and pass that view to a div through jquery. That is i think the easiest way
Thanks Raheel. Question, how can I pass two data values to the controller? something like: $.post('get_link_load_options', {data:var1,var2},function(result) { ? then in my controller I can use var1 as $q and var2 as $r? is this possible as I'll need to pass multiple variables in order to display the required info in the view? thanks again.
$.post("test.php", { name: "John", time: "2pm" }).done(function(data) { /* other stuff here */ }); recieve name and time in post on test.php
Thanks Raheel. using the syntax you provided now, $.post("get_link_load_options", { varlatitude: latitude, varlongitude: longitude }).done(function(data) { $('#results').html(result); }); this prints the 2 variable latitude and longitude to my div, not the contents of view get_link_load_options any ideas? thanks.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.