Make files available according to client group using php

Asked

Viewed 63 times

0

I need to make available on an Opencart information page 2 price table files, where one is for retail and the other for wholesale. The page that will show the link is only accessed if the customer logs in, now I would like that if it is registered as retail have access only to the link of table V, but if it is registered as attacked it just see the link to the table A. The client registration table is called Custumer and the column indicating the group id is group_id, the id for Retail is 1 and for Wholesale is 2. I would like the experts to help me because I’m starting with PHP now and it’s very difficult to do that. Thank you.

Since the page where I want to enter the code is already within 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>"; }
    ?>
  • just do an if. show what you’ve tried to do

  • Since the page where I want to enter the code is already inside a session I was going to skip this check, so I thought: <? php $consulta_group = "SELECT * FROM Customer WHERE customer_group_id < 2";

  • You thought right, but I don’t see any sense in the underage sign <, would be much simpler just = 1 or = 2. That’s all your doubt was?

  • my full comment Since 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 // Check that the group is less than 2 $consulta_group = "SELECT * FROM Customer WHERE group_id < 2"; // Pórem do not know how to use if ( Empty($consulta_group)) { $linkwholesale = "tableelawholesale.pdf"; } Else { $linkvarejo = "tableretail.pdf": ?>

  • I thought to use < 2 to indicate that the group_id is retail and that should show the retail link and if it was bigger the wholesale one

  • put the code in the question of what you have already tried to do. Show the queries to the database that pull the value of the user group

  • I already added to the question what I tried to do

Show 2 more comments

1 answer

0

Sql searches to return faster data should be as objective as possible. It is better to use = 1 than < 2. In this case assuming that 0, -1 or any other value happens to arrive, the retail table will always be displayed and this may not be your wish. The best is when the user logs in you save their group to session PHP. 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 don’t want to save the group in the session but save the ID, in that case you would have to do 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>"; }
  • Israel think that’s quite what you posted, but I came across another problem the opencart it does not allow me to put command lines in php. I don’t know now what to do.

  • which error it presents?

  • There is no error when saving the article with PHP code it simply deletes the code because I do not know if it is the opencart system that changes the php opening tag in comment, example: Original code: <? php $usuario_i... ? > It changes to: <!-? php ....>

Browser other questions tagged

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