Hello World, Welcome to codeigniter tutorial series Learn codeigniter from scratch. In the previous tutorial you have learned that how to update and delete data from database with codeigniter. In this tutorial, we will learn that how we can perform searching and sorting with codeigniter. We will apply searching and sorting on out existing todo app. I have divided this tutorial in following parts:
- Concept Overview
- Implement Searching
- Implement Sorting
- Test the application.
Concept Overview
In this tutorial we will implement searching and sorting on our todo app. We will search task by task title and sort the task according to its alphabetic name. I hope you will enjoy this tutorial.
Implement Searching
In this step we will search task by task title.
- Open todo.php file in application/views/todo folder and add the following code after h1 tag.
<!-- Searching and sorting start --> <div class="page-header"> <div class="row"> <div class="col-xs-6"> <form action="" method="get"> <table class="table table-bordered"> <tr> <th><input type="text" name="q" class="form-control" /></th> <th><input type="submit" class="btn btn-danger" /></th> </tr> </table> </form> </div> <div class="col-xs-6"> <form action="" method="post"> <table class="table table-bordered"> <tr> <th><a href="<?php echo current_url(); ?>/?sort=desc">ASC to DESC</a></th> <th><a href="<?php echo current_url(); ?>/?sort=asc">DESC to ASC</a></th> </tr> </table> </form> </div> </div> </div> <!-- Searching and sorting close -->
After adding the above code when you run the application you wee see the following screen on your browser.
- Open Todo_Model.php model and change the count_list and get_all_list method as following:
// Count Total List function count_list(){ if(isset($_GET['q'])){ $search=(string)$_GET['q']; $this->db->like('task_title',$search); } return $this->db->count_all_results('todo_list'); } // Get all list function get_all_list($start){ $response=array(); $this->db->limit('5',$start); // Searching Implement if(isset($_GET['q'])){ $search=(string)$_GET['q']; $this->db->like('task_title',$search); } $query=$this->db->get('todo_list'); if($query->num_rows()>0){ $response['bool']=true; $response['d']=$query->result_array(); }else{ $response['bool']=false; } return $response; }
- Save this code and run the application. Now when you search in text box you will see the relative results.
We have successfully implemented the search on our application. In the next step we will apply sorting.
Implement Sorting
In this step we will apply sorting on our task.
- Open Todo_Model.php model and change the get_all_list method as shown in the following code.
// Get all list function get_all_list($start){ $response=array(); $this->db->limit('5',$start); // Searching Implement if(isset($_GET['q'])){ $search=(string)$_GET['q']; $this->db->like('task_title',$search); } if(isset($_GET['sort'])){ $sort=(string)$_GET['sort']; $this->db->order_by('task_title',$sort); } $query=$this->db->get('todo_list'); if($query->num_rows()>0){ $response['bool']=true; $response['d']=$query->result_array(); }else{ $response['bool']=false; } return $response; }
- Now, When you click on ASC to DESC or DESC to ASC link. You will see the relative results.
Congratulations, We have implemented searching and sorting with codeigniter. 🙂