考虑这个代码块,其中switch语句从ajax请求中获取参数,然后将其转换为列名,并将列名传递给CI or_like查询。但请注意,如果参数是"地址",我将要与查询的多个参数进行比较,包括街道地址,方向指示符,街道类型(街道,大道,大道等) ,city,zip_code和地址的所有粒子,每个粒子在名为" assets":
的表中都有相应的列名。 $field = array();
switch($params['field']){
case "Owner" :
$field[0] = "owner_name";
break;
case "Owner phone" :
$field[0] = "owner_phone";
break;
case "Listing agent" :
$field[0] = "listing_agent";
break;
case "Listing date" :
$field[0] = "list_date";
break;
case "Address" :
$field[0] = "as_st_number";
$field[1] = "as_st_dir";
$field[2] = "as_st_name";
$field[3] = "as_st_desig";
$field[4] = "as_unit_num";
$field[5] = "as_city";
$field[6] = "as_city";
$field[7] = "as_state";
break;
}
$array = array(
$field => $match
);
$query = $this->db->select('*')->from('assets')->or_like($array)->get();
执行查询会导致此错误:
Error: 200 , "
<div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">
<h4>A PHP Error was encountered</h4>
<p>Severity: Warning</p>
<p>Message: Illegal offset type</p>
<p>Filename: models/Assets_model.php</p>
<p>Line Number: 743</p>
<p>Backtrace:</p>
显然查询语句不像$field
中的$field => $match
是一个数组,但需要它是一个值。
我可以使用以下内容填充每个案例:
case "Address" :
$field[0] = "as_st_number";
$field[1] = "as_st_dir";
$field[2] = "as_st_name";
$field[3] = "as_st_desig";
$field[4] = "as_unit_num";
$field[5] = "as_city";
$field[6] = "as_city";
$field[7] = "as_state";
$array = array(
$field[0] => $match,
$field[1] => $match,
$field[2] => $match,
$field[3] => $match,
$field[4] => $match,
$field[5] => $match,
$field[6] => $match,
$field[7] => $match
);
break;
......但这似乎不够优雅。如果问题需要澄清,请说出来。
答案 0 :(得分:1)
可能最好循环遍历数组以构建查询
$this->db->select('*')->from('assets');
foreach($array as $field => $search){
$this->db->or_like($field,$search);
}
$results = $this->db->get->result();
答案 1 :(得分:1)
试试这个:
switch($params['field'])
{
case "Owner" :
$this->db->like("owner_name", $match);
break;
case "Owner phone" :
$this->db->like("owner_phone", $match);
break;
case "Listing agent" :
$this->db->like("listing_agent", $match);
break;
case "Listing date" :
$this->db->like("list_date", $match);
break;
case "Address" :
// setup the first like
$this->db->like("as_st_number", $match);
// figure out the or_like particles
$field_names = [
"as_st_dir", "as_st_name", "as_st_desig",
"as_unit_num", "as_city", "as_city", "as_state"
];
foreach($field_names as $name)
{
$others[$name] = $match;
}
$this->db->or_like($others);
break;
}
$query = $this->db->get('assets'); //does a select * from assets
或者这可能更简洁一点。
if($params['field'] === "Address)
{
$this->db->like("as_st_number", $match);
// setup the or_like array
$field_names = ["as_st_dir", "as_st_name", "as_st_desig",
"as_unit_num", "as_city", "as_city", "as_state"];
foreach($field_names as $name)
{
$others[$name] = $match;
}
$this->db->or_like($others);
}
else
{
switch($params['field'])
{
case "Owner" :
$field = "owner_name";
break;
case "Owner phone" :
$field = "owner_phone";
break;
case "Listing agent" :
$field = "listing_agent";
break;
case "Listing date" :
$field = "list_date";
break;
}
$this->db->like($field, $match);
}
$query = $this->db->get('assets');