Question

I have this query:

$conditions = array(
            'editore LIKE' => "%$e%",
            'titolo LIKE' => "%$t%"
        );
        if (isset($autore_diviso))
            $conditions[] = array('autori LIKE ? AND ?' => array("%$autore1%","%$autore2%"));                   
        else
            $conditions[]=array('autori LIKE' => "%$a%");
        if (!$anno&&!$anno2) // I HAVE TO CHANGE THIS - if the user does not insert any year
            $conditions=$conditions;
        else {
        if (!$anno)
            $conditions[] = array('anno <=' => "$anno2");          
        if (!$anno2)
            $conditions[] = array('anno >=' => "$anno");
        }
        if ($anno&&$anno2)
            $conditions[] = array('anno BETWEEN ? AND ?' => array($anno,$anno2));

Where anno mean year, editore means publisher and titolo means title.

It's not working the search for exactly 2 authors.
If I debug $conditions, I have for example:

Array (
[editore LIKE] => %%
[titolo LIKE] => %%
[0] => Array
    (
        [autori LIKE ? AND ?] => Array
            (
                [0] => %massi%
                [1] => %palu%
            )

    ) )


But the result is empty.

Was it helpful?

Solution

That's not how you add elements to arrays in PHP. Also, there's no need to use explicit AND, that's the default. It's needed only if there would otherwise be multiple identical array keys.

$conditions = array(
    'autori LIKE ? AND ?' => array("%$autore1%","%$autore2%"), 'editore LIKE' => "%$e%",
    'titolo LIKE' => "%$t%"
);

if (!$anno)
    $conditions[] = array('anno <=' => "$anno2");          

if (!$anno2)
    $conditions[] = array('anno >=' => "$anno");

if ($anno&&$anno2)
    $conditions[] = array('anno BETWEEN ? AND ?' => array($anno,$anno2));

OTHER TIPS

SOLVED with this code:

$conditions = array(
        'editore LIKE' => "%$e%",
        'titolo LIKE' => "%$t%"
    );
    if (isset($autore_diviso))
        $conditions[] = array('autori LIKE ? AND autori LIKE?' => array("%$autore1%","%$autore2%"));                    
    else
        $conditions[]=array('autori LIKE' => "%$a%");
    if (!$anno&&!$anno2)    // Da sistemare.
        $conditions=$conditions;
    else {
    if (!$anno)
        $conditions[] = array('anno <=' => "$anno2");          
    if (!$anno2)
        $conditions[] = array('anno >=' => "$anno");
    }
    if ($anno&&$anno2)
        $conditions[] = array('anno BETWEEN ? AND ?' => array($anno,$anno2));
    debug($conditions);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top