Code Igniter - remove single quotes from where_in

I have 2 questions:

$genres = $this->db->select('Group_Concat(intGenreId) strDJGenres') ->from('tblDJGenres') ->where('intDJId', $this->session->userdata('non_admin_userid')) ->get() ->row(); $results = $this->db->select('tblTracks.*, tblGenres.strName as strGenreName') ->from('tblTracks') ->join('tblGenres', 'tblTracks.intGenreId = tblGenres.intGenreId', 'left') ->where_in('tblTracks.intGenreId', $genres->strDJGenres) ->get() ->result(); 

The first query returns a string such as

'1,2,3,4,8,6,5,7,45,66'

which I use in my where_in clause in the second query. The problem is that with this line he writes SQL as:

 SELECT `tblTracks`.*, `tblGenres`.`strName` as strGenreName FROM (`tblTracks`) LEFT JOIN `tblGenres` ON `tblTracks`.`intGenreId` = `tblGenres`.`intGenreId` WHERE `tblTracks`.`intGenreId` IN ('1,2,3,4,8,6,5,7,45,66') 

When quoting around it, it is considered as one value. How can I get a second request to execute as I want? those.

 .... where `tblTracks`.`intGenreId` IN (1,2,3,4,8,6,5,7,45,66) 
+4
source share
2 answers

Multiple values ​​can be passed to the where_in as an array .


The quotes from the beginning and end of the line can be removed using trim() :

 $dj_genres = trim($genres->strDJGenres, "'"); 

You can then convert this string into an array of strings to jump to the where_in second query.

 $dj_genres_array = explode(",", $dj_genres); 

Or if you need an array of integers :

 $dj_genres_int_array = array_map('intval', $dj_genres_array); 

You can simply add the resulting array to the second query:

 // ... ->where_in('tblTracks.intGenreId', $dj_genres_array) // ... 

Or:

 // ... ->where_in('tblTracks.intGenreId', $dj_genres_int_array) // ... 
+9
source

Answer given by JLeft:

You can remove quotes from the beginning and end of a line using the following function:

 $dj_genres = trim($genres->strDJGenres, "'"); 

You can then convert this string into an array of strings to jump to the where_in clause of the second query.

 $dj_genres_array = explode(",", $dj_genres); 

If you need an array of integers, it can be generated like this:

 $dj_genres_int_array = array_map('intval', $dj_genres_array); 

worked absolutely fine ... Thanks JLeft

+1
source

All Articles