我有HMVC codeigniter项目,我想在字段上创建自动完成,但不管我试过它都行不通,我正在使用CI 3,php 5.5 +
这是我的控制器(测试):
function getData() {
$name = $this->uri->segment(3);
$query = $this->ex_model->getThisData()->like('name', $name)->get();
foreach ($query->result() as $q) {
$res['query'] = $name;
$res['suggestions'][] = array(
'value'=>$q->name,
'gender'=>$q->gender,
'id'=>$q->id,
);
}
echo json_encode($res);
}
因此,我的模型(ex_model):
function getThisData(){
$this->db->select('name');
$this->db->from('table1');
$this->db->join('table2', 'table1.id= table2.id');
$query = $this->db->get();
return $query->row_array();
}
最后,我的观点+ JavaScript:
<input type="search" id="data1" placeholder="Input Name..." class="form-control border-input">
var site = "<?php echo base_url();?>";
$(function(){
$('#data1').autocomplete({
serviceURL: site+'/test/getData',
onSelect:function(suggestion){
$('#id').val(''+suggestion.id);
$('#gender').val(''+suggestion.gender);
}
});
});
我的网址那一刻:
http://localhost/myproject/admin/test
我不知道HMVC ci上的URI段如何工作,我尝试使用segment(4)和(5)仍然没有显示自动完成。
没有错误,但输入字段上的数据不会显示,我想知道我是否应该修复“$ this-&gt; uri-&gt; segment(3)”或我的javascripts或者其他。
答案 0 :(得分:0)
根据您的网址:
http://localhost/myproject/admin/test/getData
您无法将值作为段(4)和(5)获取。
你需要像这样使用:
$this->uri->segment(1); // admin
$this->uri->segment(2); // test
$this->uri->segment(3); // getData
您还可以按照用户手册进行操作:https://www.codeigniter.com/userguide3/libraries/uri.html
根据手册和您的网址:
您的细分应为:
1- admin
2- test
3- getData
答案 1 :(得分:0)
在codeigniter中,段在主域名称后面启动:
例如: http://localhost/myproject/admin/test/getData 此处 http://localhost/myproject 是主域名,因此细分受众群将从 admin 开始。 这里管理员是第1段测试是第二段, getData 是第三段。
答案 2 :(得分:0)
Without checking JS code, your controller and model don't fit:
Your model is already returning array (or FALSE) so there is no need for
$query = $this->ex_model->getThisData()->like('name', $name)->get();
It should be:
$query = $this->ex_model->getThisData();
You are trying to filter query by parameter so you need to pass that to model:
$name = $this->uri->segment(4) ? $this->uri->segment(4) : null;
$query = $this->ex_model->getThisData($name);
Now your model's method code should looks like:
public function getThisData($name)
{
if ($name != null) {
$this->db->select('name');//should this be '*'?
$this->db->from('table1');
$this->db->where('name', $name)
$this->db->join('table2', 'table1.id= table2.id');
$query = $this->db->get();
return $query->result_array();//remember if you return array or object and you are probably looking for result, but not for single row here?
}
//case when $name value from controller is null
return false;
}