문제

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.

도움이 되었습니까?

해결책

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));

다른 팁

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);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top