0

Sorry for asking probably a basic question, but I'm having a blackout.

If I have an array of arrays:

Array
(
    [appsversion] => Array
        (
            [0] => appsversion='6.1.0.33'
            [1] => appsversion='6.1.0.40'
        ),
    [osversion] => Array
        (
            [0] => osversion='6.1.0.53'
            [1] => osversion='6.1.0.42'
        )
)

how do I please construct an SQL condition with OR and AND of it?

I.e. for something like:

    $condition = '';
    foreach ($CONDITIONS as $key => $value) {
        # XXX I'm so lost here XXX
    }

    $sql = sprintf('select %s from mytable where %s', $condition);
    $sth = $pg->prepare($sql);
    $sth->execute();

I need the constructed string

    (appsversion='6.1.0.33' OR appsversion='6.1.0.40') AND
    (osversion='6.1.0.53' OR osversion='6.1.0.42') 

or:

    appsversion in ('6.1.0.33', '6.1.0.40') AND
    osversion in ('6.1.0.53', '6.1.0.42') 

Please give me few hints - to get my brain started again :-)

5 Answers 5

2

There:

$where = array();
foreach ($conditions as $values) {
    $where[] = '('.implode(' OR ', $values).')';
}
$string = implode(' AND ', $where);

Yields:

(appsversion='6.1.0.33' OR appsversion='6.1.0.40') AND (osversion='6.1.0.53' OR osversion='6.1.0.42')
Sign up to request clarification or add additional context in comments.

Comments

1

You mean something like this?

$condition = "(appsversion = '" . implode("' OR appsversion = '", $conditions['appsversion']) . "')";
$condition .= " AND (osversion = '" . implode("' OR osversion = '", $conditions['osversion']) . "')";

Or, cleaner with IN:

$condition = "appsversion IN ('" . implode("', '", $conditions['appsversion']) . "')";
$condition .= " AND osversion IN ('" . implode("', '", $conditions['osversion']) . "')";

3 Comments

Thanks, but what if I don't know the keys? (I.e. don't know there is "appsversion" and "osversion").
That would produce appsversion = ('6.1.0.33' OR '6.1.0.40'). I don't think that would work correctly.
I saw my mistake after posting, updated, thanks. @Alex then you're screwed. Why don't you know the keys?
1

In case your original array does not duplicate the key names and provides values "as-is" (I somehow misread your question, but probably it's useful):

$conditions = array(
    'appsversion' => Array('6.1.0.33', '6.1.0.40'),
    'osversion' => Array('6.1.0.53', '6.1.0.42'),
);

foreach ($conditions as $key => &$values) {
    foreach($values as &$value)
        $value = sprintf("%s='%s'", $key, $value);
    $values = sprintf("(%s)", implode(' OR ', $values));
}
unset($values, $value);
echo implode(" AND \n", $conditions);

Output/Demo:

(appsversion='6.1.0.33' OR appsversion='6.1.0.40') AND (osversion='6.1.0.53' OR osversion='6.1.0.42')

Comments

0
$conditions1 = array();
foreach ($CONDITIONS['appsversion'] as $value) {
    $conditions1[] = sprintf("appversion = '%s'", mysql_real_escape_string($value));
}

$conditions2 = array();
foreach ($CONDITIONS['osversion'] as $value) {
    $conditions1[] = sprintf("osversion = '%s'", mysql_real_escape_string($value));
}

$sql = sprintf('select %s from mytable where (%s) AND (%s)', $cols, implode(' OR ', $conditions1), implode(' OR ', $conditions2));
$sth = $pg->prepare($sql);
$sth->execute();

Comments

0
foreach($C as $key => $value)
{
    $str .= ($str ? ' AND ' : '') . '(' . implode(' OR ',$value) . ')';
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.