CodeIgniter Forums
Stumped on this one.. (SQL stuff) - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Stumped on this one.. (SQL stuff) (/thread-2170.html)



Stumped on this one.. (SQL stuff) - El Forum - 07-19-2007

[eluser]awpti[/eluser]
Unsure of how to complete this..

I have an array (pulled from a form), as such:

SubArea[] = 'Katchewan';
SubArea[] = 'AnotherSpot';
SubArea[] = 'OneMoreSpot';
etc, etc..

Right now, I'm using a rather silly foreach() loop to generate the query (WHERE subarea = 'blah' or subarea = 'bleh'... )

Is there a way I can do this with the ARecord class?

The documentation doesn't really go into a situation like that, and I didn't see anything obvious in the wiki.


Stumped on this one.. (SQL stuff) - El Forum - 07-19-2007

[eluser]Glowball[/eluser]
It looks like you are using MySQL, so try this:
Code:
"SELECT somefields FROM sometable WHERE subarea IN ('" . implode("', '", $SubArea) . "')"



Stumped on this one.. (SQL stuff) - El Forum - 07-19-2007

[eluser]awpti[/eluser]
My concern there is the lack of validation / escaping.

Know if there is any way to accomplish that using query bindings / the Active Record class?


Stumped on this one.. (SQL stuff) - El Forum - 07-20-2007

[eluser]Peter Goodman[/eluser]
Code:
$qs = array_pad(array(), count($_POST['SubArea']), '?');
$sql = "SELECT * FROM some_table WHERE subarea IN (". implode(',', $qs) .")";
$this->db->query($sql, $_POST['SubArea']);

What this does is it creates an array of question marks, implodes and hence comma separates them and puts them into the query, then lets CI substitute the real values in for each question mark.