Amitav Roy

Blog on web and travel

Codeigniter getting ajax data using views

Posted on 7 Jul 2012 by Amitav Roy

Codeigniter getting ajax data using views

Getting data through Ajax is very cool, and when we can reuse the CodeIgniter views to get the Ajax data, the overall user experience becomes much more enhanced. So, in this tutorial, I would show you how I have used a CodeIgniter view and Ajax to filter data.

This is a tutorial where I am not going to talk about how to get the Ajax data. I am assuming that you already know how to use Ajax and have a sound knowledge of CodeIgniter. If you want, you can check the other tutorials from CodeIgniter where I have to explain right from the basic how to use Ajax with CodeIgniter.

Ok, so here what I am going to do: I will use the Drupal database of my site to first load a list of all the nodes available on the site – which has images, galleries and articles. Then, I will use Ajax calls to filter out the list with content as per the clicked type: simple isn’t it?

First I created an ajax_demo controller which has index and give_more_data function. The give_more_data is the page which I will call through Ajax to get the desired output. Oh by the way, if you see the line number 8 I am using a master template inside which I am calling another view ‘ajax_index’ which is set on line number 6 as $data[‘view’].

This is calling a model nodes_m which gives the query result. Again the model has two functions one for the flat list of all the nodes and the second one has a where clause to filter as per the node type.

All this is done to get the data. And now here is the view ajax_index which is being used to get the data in designed tabular format.

Now, it’s time to write the jQuery code to make the click function work and make the Ajax calls to populate the container div with desired content.

So where is how the final thing looks like:

Codeigniter getting ajax data with views
Codeigniter getting ajax data with views