Search Data From Database In Codeigniter With Example
If you are looking for a web tutorial on how to display data from database in PHP using Codeigniter. we have discussed a topic like a search query in Codeigniter and display data from database in Codeigniter Framework.
This tutorial is search data from database in Codeigniter based on CodeIgniter and Bootstrap design and other designs I share with you how to display data from database in PHP using Codeigniter. When we want to retrieve a record filtered by a keyword, we have to use the search function. we have to search for all records to get the needed one record. So, here I will explain to you how to display data from database in Codeigniter simple search button.
This tutorial uses like query in Codeigniter form search data from database in Codeigniter. many projects need a search by product Then we use search query in Codeigniter. Then display data from database in Codeigniter in a project. use Fetch data from database in codeigniter and show list in one by one product.
Contents
- Html Search Form
- Create Controller
- Create Model
- Create View
- Conclusion
This step add a form tag method="get"
and use action in send controller and controller to function call. see below code
<form method="get" action="<?= base_url()?>searchcontroller/searchfunction"> <input type="text" name="search" class="input-search" placeholder="Search here..."> <button type="submit"><i class='bx bx-search-alt'></i></button> </form>
First of all, Create a controller Searchcontroller.php. update the following code controller form into searchcontroller.php file:
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class Searchcontroller extends CI_Controller { public function searchfunction() { $data['searchdata']=$this->Search_Model->GetSearchdata(); $this->load->view('search',$data); } }
Create a model Search_Model.php. update the following code model from the Search_Model.php file to fetch data from database in Codeigniter.
Use like query in codeigniter so You will know from whom you want to match like keyword, title, metatag, etc.
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class Search_Model extends CI_Model { public function GetSearchdata() { $this->db->select("*"); $this->db->like('tutorial_name',$this->input->get('search')); $query = $this->db->get("tutorial_name"); return $query->result(); } }
This step create a view search.php and copy to below code to show the list in user search.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>search data from database in codeigniter - phpcodingstuff.com</title> <!-- Fonts --> <link href="https://fonts.googleapis.com/css?family=Nunito:200,600" rel="stylesheet"> <!-- Styles --> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" > <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <style> html, body { background-color: #fff; color: #636b6f; font-family: 'Nunito', sans-serif; font-weight: 200; height: 100vh; margin: 0; } </style> </head> <body> <div class="container"> <div class="row"> <h1 class="text-center">search data from database in codeigniter</h1> <?php foreach ($searchdata as $key=> $value): ?> <div class="col-lg-12 col-md-12"> <div class="single-courses-list-box mb-30"> <div class="box-item"> <div class="courses-image"> <div class="image" style="background-image: url('<?= base_url();?>uploads/tutorial_images/<?= $value->image;?>');"> <img src="<?= base_url();?>uploads/tutorial_images/<?= $value->image;?>" alt="<?= $value->tutorial_name?>" title="<?= $value->tutorial_name;?>"> <a target="_blank" href="<?= base_url();?>blog/<?= $value->slug;?>" class="link-btn"></a> </div> </div> <div class="courses-desc"> <div class="courses-content"> <h2 class="courses_content_h2"><a href="<?= base_url();?>blog/<?= $value->slug;?>" class="d-inline-block" title="<?= $value->meta_tag_title; ?>"><?= ucfirst($value->tutorial_name);?></a></h2> <?php $tutorialdesc = $this->Front_Model->getdescriptiontutorial($value->id); ?> <p><?php $shorttuto = @$tutorialdesc->tutorial_data; echo substr($shorttuto, '0','200'); ?> ...</p> </div> <div class="courses-box-footer"> <ul> <li class="students-number" class="list_bottom_text_me"> <i class='bx bx-user'></i> By Ajay kumar </li> <li class="courses-lesson" class="list_bottom_text_me"> <i class='bx bx-calendar'></i> <?= $value->create_at;?> </li> </ul> </div> </div> </div> </div> </div> <?php endforeach ?> </div> </div> </body> </html>
You will learn to query in Codeigniter form search data from database in Codeigniter use this code and enjoy search query in codeigniter.
I hope it can help you...
I could not understand the query of the search function for a long time, which is very easy to say in this tutorial thank you.