vendredi 18 novembre 2016

multiple file_exists function in one if statement overwrite each other

I am creating a word to image convertor and for multy image words, I need to write a bit more complex if statements.

My first if statement works well for 2 image word(word = image + image):

$words = array("car", "bike",...)

foreach ($words as $x => $word) {

    $jpg = "Pictures/".$words[$x] . ".jpg";
    $jpg1 = "Pictures/".$word."/".$word. "1.jpg";
    $jpg2 = "Pictures/".$word."/".$word. "2.jpg";
    $jpg3 = "Pictures/".$word."/".$word. "3.jpg";

    if (file_exists($jpg1) && file_exists($jpg2)) {
        print '<div class="result1"><div id="result1" style="background-image:url('.$jpg1.')"></div>
        <div id="result2" style="background-image:url('.$jpg2.')"></div>
        <a id="word'. $x .'">'. $words[$x] .'</a></div>';
    }

This if spatement says that if the file in directory jpg1 and jpg2 exists, print this...So if it locates just the two files in those directories it will work.

However, when I try adding a 3rd file_exists function to an else if statement like this:

else if (file_exists($jpg1) && file_exists($jpg2) && file_exists($jpg3)) {
    print '<div class="result2"><div id="result3" style="background-image:url('.$jpg1.')"></div>
    <div id="result4" style="background-image:url('.$jpg2.')"></div>
    <div id="result5" style="background-image:url('.$jpg3.')"></div>
    <a id="word'. $x .'">'. $words[$x] .'</a></div>';
}

The first if statement overrides the second one, eventho the second one is correct. and the first one isnt.

I wanted to ask if everything is correct with my syntax ? Or if I can even use multiple file_exists functions like this.

Or any other reason why this might not work.

Thanks in advence! J

Aucun commentaire:

Enregistrer un commentaire