CodeIgniter Forums

Full Version: Search data in table using Codeigniter
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Model
[b]<?php
class Searchmodel extends CI_Model
{

   public function __construct() {
       parent::__construct();

   }
   function search($keyword)
   {
       $this->db->like('first_name',$keyword);
       $query  =   $this->db->get('user');
       return $query->result_array();
   }
}
?>
[/b]
Controller
[b][b]<?php
Class Search extends CI_Controller
{

   public function __construct() {
       parent::__construct();
       $this->load->model('searchmodel');

   }


   function search_keyword()
   {
       $keyword=$this->input->post('submit');
       $data['users']=$this->searchmodel->search($keyword);

       $this->load->view('user/view', $data);

   }
}
?>
[/b]
[/b]
View

<[b]link
href='http://fonts.googleapis.com/css?family=Marcellus' rel='stylesheet' type='text/css'/>
<link rel="stylesheet" type="text/css" href="<?php echo base_url();?>css/bootstrap.min.css" >
<link rel="stylesheet" type="text/css" href="<?php echo base_url();?>css/bootstrap.css" >
<div class="container">

<h1 style="text-align: center">This is view of User</h1>
<!--site_url: Returns base_url + index_page + uri_string-->
   <form class="form-inline" role="form" action="<?php echo site_url().'/search/search_keyword';?>" method="post">
       <div class="form-group">
           <input type="text" class="form-control" name="search" placeholder="Search by firstname">
       </div>
<button type="submit" class="btn btn-info" name="submit" >Search</button>
   </form>
<table class="table-bordered">
   <tr>
       <th>User ID:</th>
       <th>First Name:</th>
       <th>Last Name:</th>
       <th>Email:</th>
       <th>Password:</th>
       <th>Created Date:</th>
       <th>Updated Date:</th>
       <th>Delete Flag:</th>
       <th>NEW DATA:</th>
       <th>EDIT:</th>
       <th>DELETE:</th>


   </tr>
   <?php foreach($users as $user){ ?>

           <tr style="text-align: center">
           <td><?=$user['user_id']?></td>
           <td><?=$user['first_name']?></td>
           <td><?=$user['last_name']?></td>
           <td><?=$user['email']?></td>
           <td><?=$user ['password']?></td>
           <td><?=$user['created_at']?></td>
           <td><?=$user['updated_at']?></td>
           <td><?=$user['is_deleted']?></td>

               <td><a class="btn btn-primary" href="<?php echo site_url('cnt/add_cnt/'.$user['user_id']);?>">ADD</a></td>
               <td><a class="btn btn-success" href="<?php echo site_url('cnt/update_cnt/'.$user['user_id']);?>">EDIT</a></td>

               <td><a class="btn btn-danger" href="<?php echo site_url('cnt/delete_row/'.$user['user_id']); ?>">Delete</a></td>
           </tr>

   <?php } ?>

</table>
</div>
[/b]
Please use code tags when placing code in the forums.

If you use the forums Reply to this topic button you will get a full editor
to place code into etc;
I want to display table and apply search at same page and display search result at same page, give me the suggestions.
@Sourabh Sen,

You could also use DataTables ( https://datatables.net/ ) examples ( https://www.google.com/search?ei=q0T8W_a...E8ZLAq3W_0 )
Use the form_open() function from the form helper.
Use the anchor() function from the url helper instead of <a href=

Use the alternative php style in views:
PHP Code:
<?php foreach($users as $user) : ?>
     //table rows here
<?php endforeach; ?>