EllisLab text mark
Advanced Search
     
search text box which will give me related data from database. 
Posted: 11 July 2012 02:34 AM   [ Ignore ]
Joined: 2012-07-06
4 posts

Please help me i have googled a whole for this day i am new to codeigniter.

i want a search box which can be able to search in database,

example : in college alumni site if there is table Names with
ID fname
1   aditya
2   adi
3.  bob
4   adam

if i type in text box letter ‘a’ it should show me aditya ,adi,adam.


please give me complete view, conntroller ,model
and please tell me flow of code.

 

 

 
Posted: 11 July 2012 03:42 AM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2010-01-28
228 posts

Ok here is an example how it’s done.
I maded on a clean codeigniter installation so it’s done in the welcome controller.
Feel free to change it to you own controller, remember to change in the view this line:

$.ajax({ url"<?php echo site_url('welcome/suggestions'); ?>"

The controller:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class 
Welcome extends CI_Controller 
{
 
function __construct()
 
{
   parent
::__construct();
   
$this->load->model('autocomplete_model');
 
}

 
function index()
 
{
   $this
->load->view('welcome_message');
 
}

 
function suggestions()
 
{
   
   $term 
$this->input->post('term',TRUE);

    if (
strlen($term) < 2) break;

     
$rows $this->autocomplete_model->GetAutocomplete(array('keyword' => $term));

     
$keywords = array();
     foreach (
$rows as $row)

       
array_push($keywords$row->keyword);

     echo 
json_encode($keywords);
 
}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */ 

The model:

<?php
/**
 * Autocomplete_Model
 *
 * @package autocomplete
 */

class Autocomplete_Model extends CI_Model
{
    
function GetAutocomplete($options = array())
    
{
     $this
->db->select('keyword');
     
$this->db->like('keyword'$options['keyword']'after');
     
     
$query $this->db->get('autocomplete');
     return 
$query->result();
    
}

And then we have the view:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<
html>
<
head>
<
meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<
title>Autocomplete example</title>
<
link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
<*
scrip*t type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></*script*>
<*scrip*t type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></*script*>
<*scrip*t type="text/javascript">
$(document).ready(function() {
 $(function() {
  $( "#autocomplete" ).autocomplete({
   source: function(request, response) {
    $.ajax({ url: "<?php echo site_url('welcome/suggestions'); ?>",
    data: { term: $("#autocomplete").val()},
    dataType: "json",
    type: "POST",
    success: function(data){
     response(data);
    }
   });
  },
  minLength: 2
  });
 });
});
</*script*>
</head>
<body>
Text: <input type="text" id="autocomplete" />
</body>
</html> 

NOTE: You need to whipe the * out the script tags from the view.
I can not post script tags here because they get removed.

 
Posted: 11 July 2012 04:51 AM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Joined: 2012-07-06
4 posts

First of all Thanks a Ton Bart you are the only hope for me to do this code..surprised

i am not getting anything after view file after typing http://localhost/ftsearch/index.php it shows only text box not result.
i follow your code completely

I created submit button like

Submit :<input type="submit" name="mybtn"></input

Also I created link to db as

function GetAutocomplete($options = array())
    
{
     $this
->db->select('keyword');
     
$this->db->like('keyword'$options['keyword']'after');

     
$query $this->db->get('autocomplete');
     
$query $this->db->get('regnames');     // this line as my table name is regnames.
     
return $query->result();
    

 

But I am not getting anything .

My questions are :

1.where to provide database name and table name in this code ( in my case DB name is alumni and table name is             regnames )

2.What happens after view exactly ..?


I newbie to json and ajax.
please help me and correct me .
once again thanks for your your effords Bart.

 

 
Posted: 11 July 2012 02:54 PM   [ Ignore ]   [ # 3 ]   [ Rating: 0 ]
Avatar
Joined: 2010-01-28
228 posts

First of all you don’t need a submit button smile
Of you want to do more, but for the autocomplete you don’t need it.

here some explainations..

in application/config/database.php find these lines:

$db['default']['hostname'"localhost";
$db['default']['username'"root"// username
$db['default']['password'""// your password.
$db['default']['database'" alumni"// here your databasename
$db['default']['dbdriver'"mysql";
$db['default']['dbprefix'"";
$db['default']['pconnect'TRUE;
$db['default']['db_debug'FALSE;
$db['default']['cache_on'FALSE;
$db['default']['cachedir'"";
$db['default']['char_set'"utf8";
$db['default']['dbcollat'"utf8_general_ci";
$db['default']['swap_pre'"";
$db['default']['autoinit'TRUE;
$db['default']['stricton'FALSE

then change the line in /application/config/autoload.php

$autoload['libraries'= array('database'); 

Now you have alway’s connection to the database.

So this wil work right now smile

Json and ajax is a little to complicated to explain in English for me.
But what Json do, is crating an array like in php. Something like:

$foo = array('foo' =>'bar''some' =>'thing'); 

The javascript does a call to the controller in my example here:

$.ajax({ url"<?php echo site_url('welcome/suggestions'); ?>"

When there are sugestions, then you get the result back.
Mind, you need to whipe out this line:

$query $this->db->get('autocomplete'); 

The database query looks like this:

$this->db->select('fname');
$this->db->like('fname'$options['keyword']'after');
$query $this->db->get('alumni'); 
//produces somthing like: SELECT fname FROM alumni LIKE fname = '".$_POST[keyword]."%' 

Hope this help you a little?

 
Posted: 12 July 2012 04:41 AM   [ Ignore ]   [ # 4 ]   [ Rating: 0 ]
Joined: 2012-07-06
4 posts

woooooowwwwwwwww….....>>> i am in the sky now   big surprise

Thanks a lot Bart.. you rock the world.

This is my first post in any of the programming forum and i got reply from you.>
Thanks a ton..

now my confidence to code in codeigniter is increased some %

now i want to show this data on the same view

welcome_message.php 

page

i am working on it
do u have any idea about this or any link .

Please guide me in this..


you roxx man..

 
Posted: 12 July 2012 11:30 PM   [ Ignore ]   [ # 5 ]   [ Rating: 0 ]
Joined: 2012-07-12
3 posts

hi Bart v B!
I tried your MVC code for search button,
but it seems javascript is not working even i already removed all asterisk (*) in script,
and it shows the search textbox only and no button

 
Posted: 12 July 2012 11:54 PM   [ Ignore ]   [ # 6 ]   [ Rating: 0 ]
Joined: 2012-07-06
4 posts

skhan you dont require button just follow code and settings given by given by Bart.

 
Posted: 18 September 2012 02:12 AM   [ Ignore ]   [ # 7 ]   [ Rating: 0 ]
Joined: 2012-08-16
3 posts

Hi,
I am new in ajax and CI, I try to use this code but it didnt show data in input field it show only text box and display array on top of the page not in input field, wht should id do plz help me, i include all css and js
thanx

 
Posted: 18 September 2012 02:21 AM   [ Ignore ]   [ # 8 ]   [ Rating: 0 ]
Avatar
Joined: 2011-02-09
99 posts

It’s ok to ask for help with a snippet of code that you provide, but asking for the entire code without lifting a finger is effortless and not real coding.

I sure hope that all members of codeigniter will keep this unwritten rule of conduct when posting and that newcomers learn how to properly ask for help.

Based on that, look at the tutorial http://video.derekallard.com/

 Signature 

What you cry about today, you will rejoice about tomorrow.-Kenneth E. Hagin

 
Posted: 10 November 2012 11:48 AM   [ Ignore ]   [ # 9 ]   [ Rating: 0 ]
Joined: 2012-08-06
1 posts

@Bart v B
Thanks! All is working fine!