Show only users with the same Administrator id on the page

Asked

Viewed 76 times

1

Good person,

I have a page where shows all registered clerks as shows this image(https://prnt.sc/t972jm).

In the "Unit" column of my bank it shows the unit that the clerk works. I have two units in my bank as shows this picture (https://prnt.sc/t973n8).

In my "users" table I have a column called "id_drive" which is a foreign key with the "id" column of the "drive" table as shows this image (https://prnt.sc/t977rs).

My question is: How do I show on my page of clerks only the clerks who have the same "id_unit" of the logged-in user? in case it is the administrator user who is logged in.

What do I want it for?

The system is a delivery system and that page is the administrator page. It has the function of registering the clerks and I already have all function ready and working to pull in the registration, the unit of the logged-in user and so register it in the same unit of the logged-in user. As it stands, all registered clerks appear on the spreadsheet and I want to appear only the clerks of the same logged-in user unit,


this is the function list I have to capture the data and show in the spreadsheet:

<?php 

require_once("../../conexao.php");
$pagina = 'balconistas';

$txtbuscar = @$_POST['txtbuscar'];


echo '
<div class="table-responsive">
<table class="table table-sm mt-3 tabelas">
    <thead class="thead-light">
        <tr>
            <th scope="col">Nome</th>
            <th scope="col">CPF</th>
            <th scope="col">Telefone</th>
            <th scope="col">Email</th>
            
            <th scope="col">Unidade</th>
            <th scope="col">Ações</th>
        </tr>
    </thead>
    <tbody>';

    
        

        //PEGAR A PÁGINA ATUAL
        $itens_pag = intval(@$_POST['itens_pag']);
        if($itens_pag != ''){
            $itens_por_pagina = $itens_pag;
        }
        $pagina_pag = intval(@$_POST['pag']);

        $limite = $pagina_pag * $itens_por_pagina;

        //CAMINHO DA PAGINAÇÃO
        $caminho_pag = 'index.php?acao='.$pagina.'&';

    if($txtbuscar == ''){
        $res = $pdo->query("SELECT * from usuarios where nivel = 'Balconista' order by nome asc LIMIT $limite, $itens_por_pagina");
    }else{
        $txtbuscar = '%'.@$_POST['txtbuscar'].'%';
        $res = $pdo->query("SELECT * from locais where nivel = 'Balconista' and (nome LIKE '$txtbuscar' or cpf LIKE '$txtbuscar') order by nome asc");

    }
    
    $dados = $res->fetchAll(PDO::FETCH_ASSOC);


        //TOTALIZAR OS REGISTROS PARA PAGINAÇÃO
        $res_todos = $pdo->query("SELECT * from usuarios where nivel = 'Balconista'");
        $dados_total = $res_todos->fetchAll(PDO::FETCH_ASSOC);
        $num_total = count($dados_total);

        //DEFINIR O TOTAL DE PAGINAS
        $num_paginas = ceil($num_total/$itens_por_pagina);


    for ($i=0; $i < count($dados); $i++) { 
            foreach ($dados[$i] as $key => $value) {
            }

            $id = $dados[$i]['id']; 
            $nome = $dados[$i]['nome'];
            $cpf = $dados[$i]['cpf'];
            $telefone = $dados[$i]['telefone'];
            $usuario = $dados[$i]['usuario'];
            $unidade = $dados[$i]['id_unidade'];
            
        

            

echo '
        <tr>

            
            <td>'.$nome.'</td>
            <td>'.$cpf.'</td>
            <td>'.$telefone.'</td>
            <td>'.$usuario.'</td>
            <td>'.$unidade.'</td>
            
            
            <td>
                <a href="index.php?acao='.$pagina.'&funcao=editar&id='.$id.'"><i class="fas fa-edit text-info"></i></a>
                <a href="index.php?acao='.$pagina.'&funcao=excluir&id='.$id.'"><i class="far fa-trash-alt text-danger"></i></a>
            </td>
        </tr>';

    }

echo  '
    </tbody>
</table>
</div> ';


if($txtbuscar == ''){


echo '

<!--ÁREA DA PÁGINAÇÃO -->
<nav class="paginacao" aria-label="Page navigation example">
          <ul class="pagination justify-content-center">
            <li class="page-item">
              <a class="btn btn-outline-dark btn-sm mr-1" href="'.$caminho_pag.'pagina=0&itens='.$itens_por_pagina.'" aria-label="Previous">
                <span aria-hidden="true">&laquo;</span>
                <span class="sr-only">Previous</span>
              </a>
            </li>';
            
            for($i=0;$i<$num_paginas;$i++){
            $estilo = "";
            if($pagina_pag >= ($i - 2) and $pagina_pag <= ($i + 2)){


            if($pagina_pag == $i)
              $estilo = "active";

          echo '
             <li class="page-item"><a class="btn btn-outline-dark btn-sm mr-1 '.$estilo.'" href="'.$caminho_pag.'pagina='.$i.'&itens='.$itens_por_pagina.'">'.($i+1).'</a></li>';
           } }
            
           echo '<li class="page-item">
              <a class="btn btn-outline-dark btn-sm" href="'.$caminho_pag.'pagina='.($num_paginas-1).'&itens='.$itens_por_pagina.'" aria-label="Next">
                <span aria-hidden="true">&raquo;</span>
                <span class="sr-only">Next</span>
              </a>
            </li>
          </ul>
</nav>

<div align="center">';

if(@$itens_pag == $itens_por_pagina_1){
    $classe_ativa_1 = 'classe_ativa_pag';
}
if(@$itens_pag == $itens_por_pagina_2){
    $classe_ativa_2 = 'classe_ativa_pag';
}
if(@$itens_pag == $itens_por_pagina_3){
    $classe_ativa_3 = 'classe_ativa_pag';
}

echo '
<a href="'.$caminho_pag.'itens='.@$itens_por_pagina_1.'" class="'.@$classe_ativa_1.'" title="Itens para mostrar na paginação">'.$itens_por_pagina_1.'</a> - 
<a href="'.$caminho_pag.'itens='.@$itens_por_pagina_2.'" class="'.@$classe_ativa_2.'" title="Itens para mostrar na paginação">'.$itens_por_pagina_2.'</a> -
<a href="'.$caminho_pag.'itens='.@$itens_por_pagina_3.'" class="'.@$classe_ativa_3.'" title="Itens para mostrar na paginação">'.$itens_por_pagina_3.'</a> -
<small>Itens</small>

</div>


';

}


?>

  • Adds in SQL: and id_drive = '$id_unidade_do_adm'.... SELECT * from usuarios Where nivel = 'Balconist' and id_drive = '$id_unidade_do_adm'... logically, only in the situation that is convenient...

  • In Else txtbuscar SQL... you use the local table instead of users....

  • To help you with the "ready" answer... you need more information!

  • The variable '$id_unidade_do_adm' needs to be created right? How do I create it by taking the 'id_drive' of the logged-in user? What information do you need to help me better?

1 answer

0


I managed to solve

I opened a session at the beginning of the page to create the restore variable of the logged-in user id:

<?php 
@session_start();
require_once("../../conexao.php");
  

//TRAZER OS DADOS DO CLIENTE
  $cpf_cliente_u = @$_SESSION['cpf_usuario'];
 
  $res2 = $pdo->query("SELECT * from usuarios where cpf = '$cpf_cliente_u'");
  $dados2 = $res2->fetchAll(PDO::FETCH_ASSOC);
  $id_unidade = @$dados2[0]['id_unidade'];
  
 ?>

<?php 

After that, I used the @Duda Gervásio tip to add the variable in the sql that inserts the value in the list.

if($txtbuscar == ''){
        $res = $pdo->query("SELECT * from usuarios where nivel = 'Balconista' and id_unidade = '$id_unidade' order by nome asc LIMIT $limite, $itens_por_pagina");
    }else{
        $txtbuscar = '%'.@$_POST['txtbuscar'].'%';
        $res = $pdo->query("SELECT * from locais where nivel = 'Balconista' and id_unidade = '$id_unidade' and (nome LIKE '$txtbuscar' or cpf LIKE '$txtbuscar') order by nome asc");

    }


    
    $dados = $res->fetchAll(PDO::FETCH_ASSOC);


        //TOTALIZAR OS REGISTROS PARA PAGINAÇÃO
        $res_todos = $pdo->query("SELECT * from usuarios where nivel = 'Balconista' and id_unidade = '$id_unidade'");

  • That’s it! I would need to know what information you had available, usually in the same session!! To avoid a query at the base just to grab the id_drive, you can already save it in the session also at login time! ;)

Browser other questions tagged

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