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

5

I have two if parallels and I need to get into one OR on another, not both. However, since I was within while with the first if , I could not put just else , having to make another if out of while . So every time it goes into the condition of the first if it ends up also entering the second one.

Is there anything I can put that if I get into the first one it skips 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>';
}
    
asked by anonymous 13.11.2014 / 23:53

1 answer

6

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 could not 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 placed GitHub for future reference .

I had to rearrange the code so I could understand. It can simplify things even more but maybe you do not understand.

    
14.11.2014 / 00:00