Limit content according to user

1

I have a question about how to limit the options that a user sees in a form in my select / option
I want the user id = 1 to only see the clients that I assign (let's say the client1 and clients5)
I am free to add fields for this purpose in my database users and clients which are: < br>

users

id nombre usuario contraseña tipoUsuario
1  Miguel us1     psw1       1
2  Pedro  us2     psw2       1
3  Lalo   us3     psw3       2
4  Juan   us4     psw4       2
5  Diego  us5     psw5       3

clients

idCliente  cliente
1          cliente1
2          cliente2
3          cliente3
4          cliente4
5          cliente5

My intention is that they can see in my meeting only their assigned clients.

I'm using a Select field where I show all the clients in the clients table:

        <?php
$conn //aqui tengo la conecion a la base de datos

            session_start();
                include "../controler/conn.php";
                if (!isset($_SESSION['user_log'])&& $_SESSION['user_log']==null) {
               header("location: ../action/logout.php");
           }
            $id=$_SESSION['user_log'];
            $query=sqlsrv_query($conn,"SELECT * from usuarios WHERE correo='$id'");
            while ($row=sqlsrv_fetch_array($query)) {
$tt_usuario_id = $row['id']; //el id del usuario 
        ?>

          <select name="cliente" >
            <option value="" >--Select--</option>
              <?php
$sqlCliente = "SELECT * FROM clientes";//aqui es donde agregaria el where 

                  $resultCliente = sqlsrv_query($conn,$sqlCliente);
                        while($rowCliente = sqlsrv_fetch_array($resultCliente) )
                        { 
                        $idClienteSelect= utf8_encode($rowCliente['idCliente']); 
                        $clienteSelect  = utf8_encode($rowCliente['cliente']);
              ?>
            <option value ="<?php echo $idClienteSelect;?>">
              <?php echo $clienteSelect;?>
            </option>
                <?php } ?>        
          </select> 

As I mentioned, I would like to know what would be a good method in my where or what to add in the database to assign clients to the users and be able to show only what was assigned (I have been tried but I think that the ideas were finished: ))

    
asked by claus vargas 06.09.2018 в 17:11
source

3 answers

1

What you have to do is an intermediate table where you associate the id of the user with the id of the clients ie a table from 1 to many 1: N, where a client can be managed by one or several users. Example:

Usuarios

id nombre usuario contraseña tipoUsuario
1  Miguel us1     psw1       1
2  Pedro  us2     psw2       1
3  Lalo   us3     psw3       2
4  Juan   us4     psw4       2
5  Diego  us5     psw5       3

Clientes

idCliente  cliente
1          cliente1
2          cliente2
3          cliente3
4          cliente4
5          cliente5

Usuario-Clientes

idUsuario idCliente
1         1
1         5
2         1
2         3

The query would be something like this:

 SELECT * FROM usuarios u, clientes c usuarioclientes uc
 WHERE u.id = uc.idUsuario
 AND c.idCliente = uc.idCliente

The asterisk is changed by the fields you need. You let me know if he served you.

    
answered by 06.09.2018 / 17:37
source
0

The way I can think of is to create a new table with the assignment of clients per user, that way a client could even be seen by 1 or more users (if it were the case), you enter it in your query with a INNER JOIN and in the WHERE you add the id of the user. Something like this:

SELECT * FROM CLIENTES 
INNER JOIN CLIENTES_USUARIOS ON (CLIENTES.IdCliente=CLIENTES_USUARIOS.IdCliente)
INNER JOIN USUARIOS ON (USUARIOS.Id=CLIENTES_USUARIOS.IdUsuario)
WHERE USUARIOS.Id='$id'
    
answered by 06.09.2018 в 17:19
0

finally solve it!

solve it by assigning repeated

idCliente  cliente
1          cliente1
2          cliente2
2          cliente3
1          cliente4
5          cliente5

This way the user can be assigned a value ( 1 for example) and would show client1 and client4 if you have assigned one client to users

    
answered by 07.09.2018 в 20:22