if only executes Else

Asked

Viewed 96 times

3

Hello guys I have this function ifit collects a variable coming from login.php and does a check to choose between two menus. It just doesn’t matter the result inside the variable $permt he always chooses Isis. Someone can point out to me my mistake made.

<?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;
    }
  • 2

    echo after receiving data in the $permt variable to see what is being received.

  • already made friend I get the expected value 1

  • 1

    There is no way of knowing, the problem lies elsewhere: http://ideone.com/dvrb5W

  • interestingly if I remove a sign of '=' it happens to show the first option, but hangs on it too.

  • try using quote or quote exe: ($permt=='1') or ($permt=="1") if it works is due to the type of information received being string and not integer.

  • 1

    Are you sure your if you’re not using ===? if($permt===1) this condition plays to the else, but what you have in your code does not show error.

  • I did the tests suggested by Wilson same problem persists, only different thing is that I added echo within if and else and only occurs perfectly in if.

Show 2 more comments

1 answer

3


Maybe it’s because you’re comparing a string with an integer, just to be sure, it converts to an integer before comparing and makes a comparison of value and type '==' (three equals signals).

I also amended $permt=$svc['nivel'].""; for $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 exit from echo is different from var_dump... Which way out of var_dump($svc['nivel']); ?

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

  • with these amendments all echo has the value of 0, naturally inducing else

  • What is the output of var_dump($svc['nivel']); ?

  • 1 I made an echo before everything and really this correct.

  • echo output is different from var_dump... does a test with var_dump can be?

  • Made friend he gave me NULL I suspect you found the possible mistake.

  • 1

    If I helped you solve the problem I am happy if you choose my question or give +1.

  • It helped yes now I know I’m having a problem in the base file that collects information.

Show 2 more comments

Browser other questions tagged

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