[英]passing array in where clause in join
i have a custom query inside CI which is fetching multiple data from two tables i am implementing a search mechanism in which multiple parameters are parsed using get request i want to customize they query to fetch data on the basis of the search parameters! 我在CI内部有一个自定义查询,该自定义查询从两个表中获取多个数据我正在实现一种搜索机制,其中使用get请求来解析多个参数,我想自定义它们的查询以基于搜索参数来获取数据!
this is my query which is working and it doesn't contain any where clause 这是我的查询,正在运行,它不包含任何where子句
return $this->db->query('Select t1.id,t1.course_name,t1.course_duration,t1.course_price,t1.course_category, t2.first_name , t2.last_name , t2.email
from teacher_courses as t1 LEFT JOIN teacher as t2 on t1.teacher_id=t2.id
')->result_array();
now the query will remain exactly the same i just want to enter retrieve data on the basis of search parameter in the where clause 现在查询将保持完全相同,我只想根据where子句中的search参数输入检索数据
this is the format of my url 这是我的网址格式
http://localhost/online-learning/Home/courses?courses=1-3&teacher=1
inside my controller i am doing something like this 在我的控制器内,我正在做这样的事情
if(isset($_GET['courses'])){
(isset($_GET['courses'])) ? $get['courses']=explode('-', $_GET['courses']) :"";
(isset($_GET['teacher'])) ? $get['teacher']=explode('-', $_GET['teacher']) :"";
echo "<pre>";
print_r($get);
}
now the data is retrieved in the form of 现在以以下形式检索数据
Array
(
[courses] => Array
(
[0] => 1
[1] => 3
)
[teacher] => Array
(
[0] => 1
)
)
so how do i pass this array in the where clause of my join query. 因此,如何在我的联接查询的where子句中传递此数组。
$courses = implode(',', $array['courses']);
$teacher = implode(',', $array['teacher']);
return $this->db->query('Select
t1.id,t1.course_name,t1.course_duration,t1.course_price,t1.course_category,
t2.first_name , t2.last_name , t2.email
from teacher_courses as t1
LEFT JOIN teacher as t2 on t1.teacher_id=t2.id WHERE course_id IN ($courses) AND teacher_id IN ($teacher)')->result_array();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.