控制器:Test.php
public function autocompleteData()
{
$returnData = array();
$conditions['searchTerm'] = $this->input->get('term');
$skillData = $this->Main_data->getRows($conditions);
if(!empty($skillData))
{
foreach ($skillData as $row)
{
$data['value'] = $row['courses'];
array_push($returnData, $data);
}
}
echo json_encode($returnData);die;
}
模型:Main_data.php
public function getRows($params = array())
{
$this->db->select("sub_courses as courses");
$this->db->from("second_sub_courses");
$query1 = $this->db->get_compiled_select();
$this->db->select("course_name as courses");
$this->db->distinct();
$this->db->from("all_courses");
if(!empty($params['searchTerm']))
{
$this->db->like('courses', $params['searchTerm']);
}
$this->db->order_by('courses');
$query2 = $this->db->get_compiled_select();
$query = $this->db->query($query1." UNION ".$query2);
$result = ($query->num_rows() > 0)?$query->result_array():FALSE;
return $result;
}
视图:index.php
<script>
$(document).ready(function(){
$("#search_courses").autocomplete({
source: "<?php echo base_url('test/autocompleteData'); ?>",
minLength:1,
select:function(b,c)
{
var a=c.item.value;
a=encodeURIComponent(a);
str = a.replace(/%20/g,"+");
window.location.href="search_result/"+str+"+training+in+noida";
return false
}
});
});
</script>
<input type="text" name="search_courses" id="search_courses" class="textbox" />
在这段代码中,我使用union
创建了一个自动完成文本框,以从单个键属性(即courses
)中的两个表中获取数据。现在,当我运行控制器功能时会发生什么,它向我显示json文件,但无法在文本框内访问。我不知道我在哪里做错了。如何解决此问题?请帮助我。
谢谢