CodeIgniter Forums

Full Version: Stumped on this one.. (SQL stuff)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[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.

El Forum

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

El Forum

[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?

El Forum

[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.