Entering IF with missed criteria

Asked

Viewed 38 times

0

In the code, step to the variable $acesso what would be the value extracted from the user’s session (just for testing). And I have 3 blocks of IF, each checking different conditions for the same variable. But the three conditions are being met when only one is true.

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
        <title>Pagina inicial - Itiban pescados</title>
        <!-- Bootstrap -->
        <link href="css/bootstrap.css" rel="stylesheet">
        <link href="css/custom.css" rel="stylesheet">

        <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
        <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
        <!--[if lt IE 9]>
        <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
        <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->  
    <link rel="icon" href="/img/icon_boat2.png">        
    </head>
    <body>
    <div id="wrap">

    <?php 
    ///var/www/vhosts/pesca.itibanpescados.com.br/httpdocs
    include("/check_1.php");
    include("/header.php");
    include_once("/config.php");
    ?>

    <?php $acesso = 1;?>

    <div class="row" style="margin:5px;"><legend><h1>Viagens em aberto</h1></legend></div>
    <?php echo "Variavel acesso: " . $acesso; ?>
        <br>
        <div class="row" style="margin:5px;">
            <table class="table table-striped">
                <thead>
                    <tr>
                        <th>Viagem ID</th>
                        <th>Barco</th>
                        <th>Mestre</th>
                        <?php if (!isset($_SESSION['UsuarioID']) OR ($_SESSION['UsuarioNivel'] < 2)):else:?><th>Adiantamento</th><?php endif; ?>
                        <th>Data de saída</th>
                        <th>Data estimada de Entrada</th>
                        <th>Ocorrencia</th>
                    </tr>
                </thead>



                    <?php //Exibe as viagens conforme o tipo de acesso do usuario

                    if($acesso==1){ ?>


                        <?php echo "ENTROU NO PRIMEIRO IF";?>


                    <?php}
                    if($acesso==2){?>


                        <?php echo "ENTROU NO SEGUNDO IF";?>


                    <?php}
                    if($acesso==3){?>


                        <?php echo "ENTROU NO TERCEIRO IF";?>


                    <?php } ?>

            </table>
        </div>
    </div>

    <?php include("/footer.php");?>



    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="js/bootstrap.min.js"></script>
    <script src="js/append.js"></script>
    <script src="js/append_venda    .js"></script>
    <!--<script src="js/checkform.js"></script>-->

    </body>
</html>

And the result on the screen is this: inserir a descrição da imagem aqui

  • 3

    change == to === (identico) and use If, dps comment if it worked.

  • 1

    The problem is the lack of white space in <?php}. In this reply I explain about PHP tags, including the need to have this blank space.

  • @Diegoandrade, I used multiple Ifs to test merit, the original code is with elseif. But thanks for the help!

1 answer

2


This is due to an error in interpreting the php tag opening.

On the lines where you have <?php} place a gap between opening the php tag and closing the php keys if, thus: <?php }. That should solve.

Another option is to open and close php tags all the time, reduce the number of times you do this, so it makes the code more readable:

<?php
if($acesso == 1){
    echo "ENTROU NO PRIMEIRO IF";
}
if($acesso == 2){
    echo "ENTROU NO SEGUNDO IF";
}
if($acesso == 3){
    echo "ENTROU NO TERCEIRO IF";
} 
?>
  • Really that detail changed everything. Thank you.

Browser other questions tagged

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