Closed. This question needs to be more focused。它目前不接受答案。
想改进这个问题吗?更新问题,使其只关注一个问题editing this post。
四年前关闭。
我有五个输入字段,即技能、位置、公司、经验和薪水。根据用户需求,我将从数据库中获取值。我的问题是,如何动态地使用where条件?考虑到用户在skill和company输入字段中输入值,我想从数据库中获取这些值怎么做?现在我有20个条件来解决这个问题
你甚至可以做得更好,制作一个包含所有输入名字段的数组,以便循环到它上面。
想改进这个问题吗?更新问题,使其只关注一个问题editing this post。
四年前关闭。
我有五个输入字段,即技能、位置、公司、经验和薪水。根据用户需求,我将从数据库中获取值。我的问题是,如何动态地使用where条件?考虑到用户在skill和company输入字段中输入值,我想从数据库中获取这些值怎么做?现在我有20个条件来解决这个问题
public function get_jobonline($skillname,$userid,$location)
{
$company=$this->input->post('searchcompany');
$experience=$this->input->post('experience');
$salary=$this->input->post('salary');
echo $company;
if($location==NULL)
{
$query=$this->db->query("SELECT j.id AS job_id, j.created_date,j.title, l.city AS location, cm.name AS company_name,
cm.logo FROM `jobs` AS j
LEFT JOIN `keyword` AS k ON `k`.`job_id` = `j`.`id`
LEFT JOIN `keyword_master` AS km ON `km`.`id` = `k`.`keyword_id`
LEFT JOIN `location_master` AS l ON `l`.`id` = `j`.`location`
LEFT JOIN `company_master` AS cm ON `cm`.`id` = `j`.`company_id`
WHERE km.name = '$skillname' AND j.uid='$userid'
;");
}
else
{
$query=$this->db->query("SELECT j.id AS job_id, j.created_date,j.title, l.city AS location, cm.name AS company_name,
cm.logo FROM `jobs` AS j
LEFT JOIN `keyword` AS k ON `k`.`job_id` = `j`.`id`
LEFT JOIN `keyword_master` AS km ON `km`.`id` = `k`.`keyword_id`
LEFT JOIN `location_master` AS l ON `l`.`id` = `j`.`location`
LEFT JOIN `company_master` AS cm ON `cm`.`id` = `j`.`company_id`
WHERE km.name = '$skillname' AND j.uid='$userid' AND l.city='$location'
;");
}
// if($company != NULL && )
return $query->result_array();
}
最佳答案
如果我理解正确,您需要根据用户输入动态更改查询。
要做到这一点,你应该把查询本身和查询的函数分开。
伪代码中的Ex:
// Let's suppose it's your base query
// (that meets the minimum field required to do the research).
$sql = "SELECT j.id AS job_id, j.created_date,j.title,... WHERE
km.name = '$skillname'";
// U add the condition skill if the user typed it
if(isset($_POST['skill'])
$sql .= "AND j.skill = '$_POST['skill']'";
... (for every input filled u add condition to the query)
// WHEN all input u needed to add are tested
// Query the DB
$query = $this->db->query($sql);
你甚至可以做得更好,制作一个包含所有输入名字段的数组,以便循环到它上面。
关于php - 如何在php中动态添加多个WHERE子句,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30412857/
10-16 14:53