Welcome Guest, Not a member yet? Register   Sign In
search text box which will give me related data from database.
#1

[eluser]adiprogrammer[/eluser]
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.




#2

[eluser]Bart v B[/eluser]
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:
Code:
$.ajax({ url: "<?php echo site_url('welcome/suggestions'); ?>",

The controller:
Code:
<?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:
Code:
&lt;?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:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
&lt;title&gt;Autocomplete example&lt;/title&gt;
&lt;link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/&gt;
<*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: "&lt;?php echo site_url('welcome/suggestions'); ?&gt;",
    data: { term: $("#autocomplete").val()},
    dataType: "json",
    type: "POST",
    success: function(data){
     response(data);
    }
   });
  },
  minLength: 2
  });
});
});
</*script*>
&lt;/head&gt;
&lt;body&gt;
Text: &lt;input type="text" id="autocomplete" /&gt;
&lt;/body&gt;
&lt;/html&gt;

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

[eluser]adiprogrammer[/eluser]
First of all Thanks a Ton Bart you are the only hope for me to do this code..:wow:

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

I created submit button like
Code:
Submit :&lt;input type="submit" name="mybtn"&gt;&lt;/input>
Also I created link to db as
Code:
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.

#4

[eluser]Bart v B[/eluser]
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:
Code:
$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
Code:
$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:
Code:
$foo = array('foo' =>'bar', 'some' =>'thing');

The javascript does a call to the controller in my example here:
Code:
$.ajax({ url: "&lt;?php echo site_url('welcome/suggestions'); ?&gt;",
When there are sugestions, then you get the result back.
Mind, you need to whipe out this line:
Code:
$query = $this->db->get('autocomplete');

The database query looks like this:
Code:
$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?
#5

[eluser]adiprogrammer[/eluser]
woooooowwwwwwwww........>>> i am in the sky now :bug:

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
Code:
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..
#6

[eluser]skhan[/eluser]
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
#7

[eluser]adiprogrammer[/eluser]
skhan you dont require button just follow code and settings given by given by Bart.
#8

[eluser]saima[/eluser]
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
#9

[eluser]swgj19[/eluser]
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/
#10

[eluser]Unknown[/eluser]
@Bart v B
Thanks! All is working fine!




Theme © iAndrew 2016 - Forum software by © MyBB