简体   繁体   中英

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!

this is my query which is working and it doesn't contain any where clause

 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

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.

$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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM