How to run only one if without entering the other in PHP

Asked

Viewed 175 times

5

I have two if parallel and I need to enter one OR the other, not both. But because I am inside one while with the first if, I couldn’t put just one else, having to do another if outside the while. So every time he enters the condition of the first if he ends up also entering the second.

There’s something I can put that if I enter the first one it jumps the second?

Here is the code:

while ($arrayBancas = mysql_fetch_array($oBanca->retorno())){
   if (date('Y-m-d', strtotime($arrayBancas['dataHora'])) == $data->format('Y-m-d')) {
       echo '<div class="panel panel-default">';
          echo '<div class="panel-heading">';
             echo '<a href=""><center><strong>--:--</strong></center></a>';
          echo '</div>';    
          echo '<div class="panel-body">';    
             echo '<h6>';
               echo '<strong>'.$arrayBancas['trabalho'].'</strong><br>';
               echo '<strong>Orientador:</strong>'.$arrayBancas['orientador'].'<br>';   
               echo '<strong>Banca:</strong><br>';
               echo '<strong>Sala:</strong>'.$arrayBancas['sala'].'<br>';
             echo '</h6>';
          echo '</div>';
       echo '</div>';
     }
}
    if (date('Y-m-d', strtotime($arrayBancas['dataHora'])) != $data->format('Y-m-d')) {
       echo '<div class="panel panel-default">';
           echo '<div class="panel-heading">';
               echo '<a href=""><center><strong>--:--</strong></center></a>';
           echo '</div>';    
           echo '<div class="panel-body">';    
               echo '<h6>';
                   echo '<strong>----------</strong><br>';
                   echo '<strong>Orientador:</strong><br>'; 
                   echo '<strong>Banca:</strong><br>';
                   echo '<strong>Sala:</strong><br>';
               echo '</h6>';
           echo '</div>';
       echo '</div>';
}
  • 2

    See what you want because I understood one thing, Sergio understood another. The two answers are radically different, one of the two does not answer what you want. In fact the question is confused. Try to explain better. Sergio erased his, but I don’t know if it’s not hers that’s right.

  • Yeah, I didn’t see it, now that I walked in here and it was only your answer but that’s right, on the fly!! Thanks!

1 answer

7


The easiest way is to use a flag:

$entrou = false;
while ($arrayBancas = mysql_fetch_array($oBanca->retorno())){
   if (date('Y-m-d', strtotime($arrayBancas['dataHora'])) == $data->format('Y-m-d')) {
       echo '<div class="panel panel-default">';
          echo '<div class="panel-heading">';
             echo '<a href=""><center><strong>--:--</strong></center></a>';
          echo '</div>';    
          echo '<div class="panel-body">';    
             echo '<h6>';
               echo '<strong>'.$arrayBancas['trabalho'].'</strong><br>';
               echo '<strong>Orientador:</strong>'.$arrayBancas['orientador'].'<br>';   
               echo '<strong>Banca:</strong><br>';
               echo '<strong>Sala:</strong>'.$arrayBancas['sala'].'<br>';
             echo '</h6>';
          echo '</div>';
       echo '</div>';
       $entrou = true;
    }
}
if (!$entrou) {
   echo '<div class="panel panel-default">';
       echo '<div class="panel-heading">';
           echo '<a href=""><center><strong>--:--</strong></center></a>';
       echo '</div>';    
       echo '<div class="panel-body">';    
           echo '<h6>';
               echo '<strong>----------</strong><br>';
               echo '<strong>Orientador:</strong><br>'; 
               echo '<strong>Banca:</strong><br>';
               echo '<strong>Sala:</strong><br>';
           echo '</h6>';
       echo '</div>';
   echo '</div>';
}

On the other hand maybe you want something else (which I couldn’t imagine because it was too easy):

while ($arrayBancas = mysql_fetch_array($oBanca->retorno())){
    echo '<div class="panel panel-default">';
    echo '  <div class="panel-heading">';
    echo '    <a href=""><center><strong>--:--</strong></center></a>';
    echo '  </div>';    
    echo '  <div class="panel-body">';    
    echo '    <h6>';
    if (date('Y-m-d', strtotime($arrayBancas['dataHora'])) == $data->format('Y-m-d')) {
        echo '      <strong>'.$arrayBancas['trabalho'].'</strong><br>';
        echo '      <strong>Orientador:</strong>'.$arrayBancas['orientador'].'<br>';   
        echo '      <strong>Banca:</strong><br>';
        echo '      <strong>Sala:</strong>'.$arrayBancas['sala'].'<br>';
    } else {
        echo '       <strong>----------</strong><br>';
        echo '       <strong>Orientador:</strong><br>'; 
        echo '       <strong>Banca:</strong><br>';
        echo '       <strong>Sala:</strong><br>';
    }
    echo '    </h6>';
    echo '  </div>';
    echo '</div>';
}

I put in the Github for future reference.

I had to rearrange the code in order to understand. I can simplify it even more but maybe you don’t understand.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.