Problems do not implode when bringing results from the database

3

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

Sofarsogood,butlistingthepermissionsofeachuser(onlyhas02usersregistered)intheadministratorsview,isbringingdataduplicity.ThecodeI'musingis:

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 returning me:

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

When it really comes back:

Cadastrar,Visualizar,Editar,Excluir
Visualizar,Editar
    
asked by anonymous 25.03.2017 / 19:12

1 answer

4

You need reset the array $visualiza every while to achieve this goal.

Create a $visualiza = array() within while , so each time will 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>";
}
25.03.2017 / 19:21