[eluser]Unknown[/eluser]
this my code
1-control
Code:
class test extends Controller {
function test()
{
parent::Controller();
}
public function __construct()
{
parent::__construct();
}
function index(){
}
function autocomplete(){
$this->load->view('autocomplete');
}
function suggestions()
{
$this->load->model('autocomplete_model');
$term = $this->input->post('term',TRUE);
if (strlen($term) < 2) break;
$rows = $this->autocomplete_model->GetAutocomplete(array('keyword' => $term));
$json_array = array();
foreach ($rows as $row)
array_push($json_array, $row->mystring);
echo json_encode($json_array);
}
}
2-this view
Code:
<!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" c charset=utf-8" />
<title>Autocomplete example</title>
<link href="<?php echo base_url();?>css/jquery-ui.css" rel="stylesheet" type="text/css"/>
[removed][removed]
[removed][removed]
[removed]
$(document).ready(function() {
$(function() {
$( "#autocomplete" ).autocomplete({
source: function(request, response) {
$.ajax({ url: "<?php echo site_url('autocomplete/suggestions'); ?>",
data: { term: $("#autocomplete").val()},
dataType: "json",
type: "POST",
success: function(data){
response(data);
}
});
},
minLength: 2
});
});
});
[removed]
</head>
<body>
Text: <input type="text" id="autocomplete" />
</body>
</html>
3- this model
Code:
class autocomplete_Model extends CI_Model
{
function GetAutocomplete($options = array())
{
$this->db->select('mystring');
$this->db->like('mystring', $options['keyword'], 'after');
$query = $this->db->get('mytable');
return $query->result();
}
}