if only execute else

3

Hello everyone, I have this function if it collects a variable coming from login.php and does a check to choose between two menus. But it does not matter the result inside the variable $permt it always chooses else. Someone can point out my mistake.

<?php
include"in/login.php";

$hi=$svc['nome']."";
$permt=$svc['nivel']."";

?>

<?php   
if($permt==1) {

    require_once"in/menu.html";
    echo "$permt";  
}
else{
     require_once"in/menu2.html";
     echo "$permt";
    }
?>

To complement this is the content of login.php

<?php
include "conecta.inc";


$log= mysqli_query($conn,"select nome,nivel from usuarios where login='$doc' ");
$svc=mysqli_fetch_array($log);
if($cont){
    header("Location: ../home.php"); exit;
    }
    
asked by anonymous 09.06.2016 / 02:23

1 answer

3

Maybe it's because it's comparing a string with an integer, just to ensure it converts to integer before comparing and makes a value comparison and type '===' (three equal signs).

I also changed $permt=$svc['nivel'].""; to $permt=$svc['nivel'];

$permt=intval($permt,10);
var_dump($permt); // para debugar se continuar dando erro.
if($permt===1) {
    echo 'op 1';
}else{
    echo 'op 2';
} 

Complete code:

<?php
include"in/login.php";
$hi=$svc['nome']."";
$permt=$svc['nivel'];
$permt=intval($permt,10); // poderia converter na linha anterior...
if($permt===1) {
    require_once"in/menu.html";
    echo "$permt";  
}else{
     require_once"in/menu2.html";
     echo "$permt";
}

Remembering that the output of echo is different from var_dump ... What is the output of var_dump($svc['nivel']); ?

I hope to have helped and wish good luck on your project!

    
09.06.2016 / 03:01