[eluser]defectivereject[/eluser]
Not sure if you fixed it, but this works a treat for me, courtesy of IBM CI tutorials
Controller
Code:
public function skillsuggest() {
//INput string passed by jQuery from user input
$str = addslashes($_POST['str']);
//the following is the database query, checking the above inputted string from the input box
$skills_query = $this->mtest->find_qualheld($str);
//This is the returned data from the above database query, set into a UL appearing directly under the input box.
echo "<ul>";
foreach ($skills_query->result() as $activity) {
echo '<li>qualification).'\',' . $activity->q_id.');">' . $activity->qualification . "</li>";
}
echo "</ul>";
}
Model
Code:
function find_qualheld($str) {
$this->db->select('add your fields here');
$this->db->from('table here');
//join several tables to check against if need be
$this->db->join('qualification', 'q_id = qual_id', 'inner');
$this->db->join('staff', 's_id = staff_id', 'inner');
//search against the relevant fields
$this->db->like('qualification', $str);
$this->db->or_like('lastname', $str);
$this->db->or_like('full_name', $str);
$this->db->or_like('firstname', $str);
//set order by
$this->db->order_by('qualification', 'asc');
//set group_by if your table has several of the same records for different reasons in
$this->db->group_by('job_title','asc');
return $this->db->get();
}
View
Code:
<?php
echo form_open("training/home/search");
?>
<label for="term3">Begin Typing A Skillset</label>
//User input field, converted to $str and passed to the controller and model by jQuery
<input type="text" autocomplete="off" name="term" id="term" size="50">
//This is where the UL generated by the query will go
<div class="suggestskill" id="suggestskill_list"></div>
//Submit suggested text to a further search if need be
<input type="submit" class="button2" value="Search Skills">
<?php
echo form_close();
?>
jQuery function
Code:
$(document).ready(function() {
/* SkillSet SEARCH */
// triggered by input field onkeyup
function suggestskill(str){
// if there's no text to search, hide the list div
if (str.length == 0) {
$('#suggestskill_list').fadeOut(500);
} else {
// first show the loading animation
$('#term').addClass('loading');
// Ajax request to CodeIgniter controller "ajax" method "autosuggest"
// post the str parameter value
$.post('http://web/address/index.php/controller/function',
{ 'str':str },
function(result) {
// if there is a result, fill the list div, fade it in
// then remove the loading animation
if(result) {
$('#suggestskill_list').html(result);
$('#suggestskill_list').fadeIn(500);
$('#term').removeClass('loading');
}
});
}
}
/* AUTOSUGGEST SET ACTIVITY */
// triggered by an onClick from any of the li's in the autosuggest list
// set the class_acitity field, wait and fade the autosuggest list
// then display the activity details
function set_skill(qualification, q_id) {
$('#term').val(qualification);
setTimeout("$('#suggestskill_list').fadeOut(100);", 100);
}
});//End Document Ready
I'm not saying its the most efficient of code, but it works. i have 8 different versions of this working across an intranet i developed.
If you use it more than once for different searches, just change all function names, and field id's e.t.c.
Hope it helps :/