Multiple else if

The following code should get a query (addressed to amazon ads) to get a link with author only if author is not AA.VV. (in Italian → autori vari = several [various] authors). Otherwise it should search only for the title (in Amazon “AA.VV.” doesn’t work)

   if(($trad_titolo != '') and ($autore != 'AA.VV.')) {echo ", tr.it. <i>$row[trad_titolo]</i>, $row[trad_edizione], $row[trad_luogo] $row[trad_data] (<a rel=\"external\" href=\"$myamzcode1=$amztitolotrad$myamzcode2";}
   else {echo ", tr.it. <i>$row[trad_titolo]</i>, $row[trad_edizione], $row[trad_luogo] $row[trad_data] (<a rel=\"external\" $myamzcode1=$amztitolotradAAVV$myamzcode2";}
   //originale in italiano
   if(($trad_titolo = '') and ($autore != 'AA.VV.')) {echo "(<a rel=\"external\" href=\"$myamzcode1=$amztitolo$myamzcode2";}
   else {echo "(<a rel=\"external\" href=\"$myamzcode1=$amztitoloAAVV$myamzcode2";}

However I get a webpage with

  • two (identical) links to amazon
  • both with the title ($trad_titolo) and both without author ($autore)

You can see the output php source code of webpage

`

  • AA.VV., Martin Lutero, Milano 1985, tr.it. , , ((.
  • Roland Herbert Bainton, Here I Stand: A Life of Martin Luther, Nashville 1950, tr.it. Lutero, Einaudi, Torino 1960 ((.
  • `

    Where I’m wrong?

    EDIT

    Why the following code doesn’t work as expected?

       if(($trad_titolo != '') and ($autore != 'AA.VV.')) {echo ", tr.it. <i>$row[trad_titolo]</i>, $row[trad_edizione], $row[trad_luogo] $row[trad_data] (<a rel=\"external\" href=\"$myamzcode1=$amztitolotrad$myamzcode2";}
       else {echo ", tr.it. <i>$row[trad_titolo]</i>, $row[trad_edizione], $row[trad_luogo] $row[trad_data] (<a rel=\"external\" $myamzcode1=$amztitolotradAAVV$myamzcode2";}
       //originale in italiano
       if(($trad_titolo = '') and ($autore != 'AA.VV.')) {echo "(<a rel=\"external\" href=\"$myamzcode1=$amztitolo$myamzcode2";}
       else {echo "(<a rel=\"external\" href=\"$myamzcode1=$amztitoloAAVV$myamzcode2";}
    

    Look verrry carefully here…

    1 Like

    What do you mean? :slightly_smiling_face:

    = (assignment) vs == (comparison).

    1 Like

    Thank you. I tried with: ($trad_titolo = =''), but unsuccessfully …

    Define ‘unsuccessfully’.
    Your code will spit out two links in all codepaths.

    1 Like

    Yes: two links, while I need only one, the Italian one.

    So why have you got two if’s?

    1 Like

    I don’t know :neutral_face:

    Well I cant answer for you why you have two ifs. You put them into the code, why did you put them there if you only want one?

    1 Like

    Ah, sorry. I didn’t understand: you did mean two“if".
    I did several attempts…

    Well, each of those will generate a link. So… keep the one you need to keep?

    1 Like

    Indeed. It seems that this code works

       if(($trad_titolo != '') and ($autore != 'AA.VV.')) {echo ", tr.it. <i>$row[trad_titolo]</i>, $row[trad_edizione], $row[trad_luogo] $row[trad_data] (<a rel=\"external\" href=\"$myamzcode1=$amztitolotrad$myamzcode2";}
       else if (($trad_titolo != '') and ($autore == 'AA.VV.')) {echo ", tr.it. <i>$row[trad_titolo]</i>, $row[trad_edizione], $row[trad_luogo] $row[trad_data] (<a rel=\"external\" $myamzcode1=$amztitolotradAAVV$myamzcode2";}
       //originale in italiano
       else if(($trad_titolo == '') and ($autore != 'AA.VV.')) {echo "(<a rel=\"external\" href=\"$myamzcode1=$amztitolo$myamzcode2";}
       else {echo "(<a rel=\"external\" href=\"$myamzcode1=$amztitoloAAVV$myamzcode2";}
    

    Now I have lo leave, but after I will check again.
    Thank you so much!

    EDIT

    Yes, it works! :+1: :+1:
    Thank you very much!

    I have not done much PHP but I doubt that = = is the same as ==. My guess is that = = is the same as = (2 assignments would be the same as 1). Also if there is no space in ='' (no space between the equal sign and the quote) then that would likely be a problem too.

    The space between the equals gives an “unexpected token” error.
    It needs to be == and not = = for comparison.

    1 Like
    if(($trad_titolo != '') && ($autore != 'AA.VV.')) {
        echo ', tr.it. <i>' . $row['trad_titolo'] . '</i>, ' . $row['trad_edizione'] . ', ' . $row['trad_luogo'] . ' ' . $row['trad_data'] . ' (<a rel="external" href="' . $myamzcode1 . '=' . $amztitolotrad . $myamzcode2;
    } elseif ($autore != 'AA.VV.') {
        echo ', tr.it. <i>' . $row['trad_titolo'] . '</i>, ' . $row['trad_edizione'] . ', ' . $row['trad_luogo'] . ' ' . $row['trad_data'] . ' (<a rel="external" ' . $myamzcode1 . '=' . $amztitolotradAAVV . $myamzcode2;
    }
    
    //originale in italiano
    if(($trad_titolo == '') && ($autore != 'AA.VV.')) {
        echo '(<a rel="external" href="' . $myamzcode1 . '=' . $amztitolo . $myamzcode2;
    } else {
        echo '(<a rel="external" href="' . $myamzcode1 . '=' . $amztitoloAAVV . $myamzcode2;
    }