codeigniter autocomplete from database

Today, We want to share with you codeigniter autocomplete from database.In this post we will show you ajax search in codeigniter demo, hear for jquery ui autocomplete in codeigniter with database we will give you demo and example for implement.In this post, we will learn about CodeIgniter Autocomplete Multiple Input Textbox With Database with an example.

PHP Codeigniter 3 – Jquery Ajax Autocomplete Search using Typeahead

Step 1: Create Table

tags table

CREATE TABLE IF NOT EXISTS `tags` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `slug` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `meta_keyword` text COLLATE utf8_unicode_ci NOT NULL,
  `meta_description` text COLLATE utf8_unicode_ci NOT NULL,
  `created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=533 ;

Step 2: Make database configuration

application/config/database.php

 '',
	'hostname' => 'localhost',
	'username' => 'main8754_988',
	'password' => '@#hjdfjhf$$%^&&**%^^',
	'database' => 'pakainfo',
	'dbdriver' => 'mysqli',
	'dbprefix' => '',
	'pconnect' => FALSE,
	'db_debug' => (ENVIRONMENT !== 'production'),
	'cache_on' => FALSE,
	'cachedir' => '',
	'char_set' => 'utf8',
	'dbcollat' => 'utf8_general_ci',
	'swap_pre' => '',
	'encrypt' => FALSE,
	'compress' => FALSE,
	'stricton' => FALSE,
	'failover' => array(),
	'save_queries' => TRUE
);

Step 3: Add Route

application/config/routes.php

$route['default_controller'] = 'welcome';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;


$route['ajax-view'] = 'welcome/index';
$route['ajaxpro'] = 'welcome/dashBoard';

Step 4: Create Controller

application/controllers/Welcome.php

load->database();
    }


    /**
    * Create from display view
    *
    * @return Response
   */
    public function index()
    {
        $this->load->view('profiles');
    }


    /**
    * response of ajax json
    *
    * @return Response
   */
    public function dashBoard()
    {
        $query = $this->input->get('query');
        $this->db->like('name', $query);


        $data = $this->db->get("tags")->result();


        echo json_encode( $data);
    }
}

Step 5: Create View File

application/views/profiles.php



    Codeigniter 3 - jquery ajax autocomplete search using typeahead example- www.pakainfo.com  
    
    
      




Codeigniter 3 - jquery ajax autocomplete search using typeahead example- www.pakainfo.com

Codeigniter Autocomplete Search From Database

Implementing an autocomplete search feature in CodeIgniter involves the following steps:

Create a search form that includes an input field for the search term and a hidden field for the base URL of the application.

Create a controller method that handles the search request and returns the results as a JSON-encoded array.

public function search()
{
    $this->load->model('my_model');
    $search_term = $this->input->post('search');
    $results = $this->my_model->search($search_term);
    echo json_encode($results);
}

In this example, we are loading a model called my_model that contains a method called search that performs the database query and returns the results as an array. We retrieve the search term from the POST data and pass it to the search method. We then echo the results as a JSON-encoded array.

Create a JavaScript function that sends an AJAX request to the controller method and populates the autocomplete results.

$(document).ready(function() {
    $('#search').keyup(function() {
        var search_term = $(this).val();
        var base_url = $('#base_url').val();
        $.ajax({
            url: base_url + 'search',
            method: 'post',
            dataType: 'json',
            data: {search: search_term},
            success: function(data) {
                var html = '';
                $.each(data, function(index, item) {
                    html += '
  • ' + item.title + '
  • '; }); $('#results').html(html); } }); }); });

    In this example, we are binding a keyup event handler to the search input field. When the user types a search term, we retrieve the term and the base URL from the form fields. We then send an AJAX request to the search controller method with the search term as a parameter. When the AJAX request is successful, we iterate through the results and build an HTML string containing the search results. We then insert the HTML string into a results element on the page.

    Keep in mind that this is a simplified example and you may need to modify it to fit your specific needs. You will also need to create a database table and a model that performs the database query based on the search term.

    I hope you get an idea about Autocomplete in Codeigniter using Typeahead.
    I would like to have feedback on my infinityknow.com blog.
    Your valuable feedback, question, or comments about this article are always welcome.
    If you enjoyed and liked this post, don’t forget to share.

    Leave a Comment