Problems in implode when bringing database results

Asked

Viewed 41 times

3

I have a table where I store access permissions to a system. It’s like this:

inserir a descrição da imagem aqui

So far so good, but by listing the permissions of each user ( only has 02 registered users ) in the view of administrators, is bringing duplicity of data. The code I’m using is:

while($jmVisualizar = mysqli_fetch_object($sqlAdm)){

    $sqlPermissao = mysqli_query($this->conexao,"SELECT * FROM permissao_adm 
    WHERE IDAdministrador = '".$jmVisualizar->IdAdmin."';"); 

    $jmPermissao = mysqli_fetch_object($sqlPermissao);

    if($jmPermissao->Cadastrar == '1'){
        $visualiza[] = "Cadastrar";
    }if($jmPermissao->Visualizar == '1'){
        $visualiza[] = "Visualizar";
    }if($jmPermissao->Editar == '1'){
        $visualiza[] = "Editar";
    }if($jmPermissao->Excluir == '1'){
        $visualiza[] = "Excluir";
    }

    // Testar
    echo implode(",", $visualiza)."<br>";
}

He’s calling me back:

Cadastrar,Visualizar,Editar,Excluir
Cadastrar,Visualizar,Editar,Excluir,Visualizar,Editar

When in fact it is to return me:

Cadastrar,Visualizar,Editar,Excluir
Visualizar,Editar

1 answer

4


You need reset to array $visualiza each while to achieve this objective.

Create a $visualiza = array() within the while, so each time will go reset the values previously obtained.

while($jmVisualizar = mysqli_fetch_object($sqlAdm)){

    // Cria uma nova array:
    $visualiza = [];

    $sqlPermissao = mysqli_query($this->conexao,"SELECT * FROM permissao_adm 
    WHERE IDAdministrador = '".$jmVisualizar->IdAdmin."';"); 

    $jmPermissao = mysqli_fetch_object($sqlPermissao);

    if($jmPermissao->Cadastrar == '1'){
        $visualiza[] = "Cadastrar";
    }if($jmPermissao->Visualizar == '1'){
        $visualiza[] = "Visualizar";
    }if($jmPermissao->Editar == '1'){
        $visualiza[] = "Editar";
    }if($jmPermissao->Excluir == '1'){
        $visualiza[] = "Excluir";
    }

    // Testar
    echo implode(",", $visualiza)."<br>";
}

  • 1

    Thanks Inkeliz, it worked!

Browser other questions tagged

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