Welcome Guest, Not a member yet? Register   Sign In
Stumped on this one.. (SQL stuff)
#1

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

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

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

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




Theme © iAndrew 2016 - Forum software by © MyBB