Provide files according to the client group using php

0

I need to make available on an Opencart 2 information page files of price lists, where one is for retail and the other for wholesale. The page that will show the link is only accessed if the client is logged in, now I would like it if it is registered as a retailer only has access to the link in table V, but if it is registered as a wholesale it just see the link to the table A. The customer registration table is called custumer and the column that indicates the group id is group_id, the id for Retail is 1 and for Wholesale it is 2. I would like the experts to help me as I am starting with PHP now and it is very difficult to do this. Thank you.

As the page where I want to insert the code is already inside a session I was going to skip this check, so I thought:

    <?php 
    // Verificar se o grupo é menor que 2
    $consulta_grupo = "SELECT * FROM customer WHERE group_id < 2";

    if ( empty($consulta_grupo)) {
    $linkvarejo  = "<a href="http://localhost/tabelavarejo.pdf">Tabela Varejo</a>";
    } else {
    $linkatacado = "<a href="http://localhost/tabelaatacado.pdf">Tabela Atacado</a>"; }
    ?>
    
asked by anonymous 02.08.2018 / 02:39

1 answer

0

Sql queries to return faster data should be as objective as possible. It is better to use = 1 than < 2. In this case assuming that by chance 0, -1 or any other value arrives, the retail table will always be displayed and this may not be your desire. It is best when the user logs you to save his group in PHP's session . This way it would be possible to do something like this:

$usuario_grupo = $_SESSION['grupoUsuario'];

if (  $usuario_grupo == 1) {
    $linkvarejo  = "<a href="http://localhost/tabelavarejo.pdf">Tabela Varejo</a>";
    } else {
    $linkatacado = "<a href="http://localhost/tabelaatacado.pdf">Tabela Atacado</a>"; }

Assuming you do not want to save the group in session but save the ID, in that case you would have to do the sql to find out which group the client belongs to:

 $usuario_id = $_SESSION['usuarioId'];

 $consulta_grupo_do_usuario = "SELECT grupo_id FROM customer WHERE id == $usuarioId";

if ( $consulta_grupo_do_usuario['grupo_id'] == 1 ) {
$linkvarejo  = "<a href="http://localhost/tabelavarejo.pdf">Tabela Varejo</a>";
} else {
$linkatacado = "<a href="http://localhost/tabelaatacado.pdf">Tabela Atacado</a>"; }
    
02.08.2018 / 04:22