PHP Hide Menu By Role MySQL

0

I have a session that is based on the IdRol of a database, depending on the Role it shows the menu, but it only lets me put 1 Role per Menu and it does not leave me for 2. (I do not want to use a nested if I have 8 user types and would make a huge mega code) Example that works with only 1 IdRol:

                <? if ($_SESSION[ 'idRol' ] == 1) { ?>
                <li class="active">
                    <a href="index.php#" title="Subtipo" data-toggle="collapse-next" class="has-submenu">
                        <span class="item-text">Subtipo</span>
                    </a>
                    <ul class="nav collapse ">
                        <li>
                            <a href="Subtipo.php" title="Subtipo Nuevo" data-toggle="" class="no-submenu">
                                <span class="item-text">Subtipo Nuevo</span>
                            </a>                                                     
                        </li>
                    </ul>
                </li>
                <? } ?> 

When I try it with two it does not leave me, example:

                <? if ($_SESSION[ 'idRol' ] == 1 && $_SESSION[ 'idRol' ] == 2) { ?>
                <li class="active">
                    <a href="index.php#" title="Subtipo" data-toggle="collapse-next" class="has-submenu">
                        <span class="item-text">Subtipo</span>
                    </a>
                    <ul class="nav collapse ">
                        <li>
                            <a href="Subtipo.php" title="Subtipo Nuevo" data-toggle="" class="no-submenu">
                                <span class="item-text">Subtipo Nuevo</span>
                            </a>                                                     
                        </li>
                    </ul>
                </li>
                <? } ?> 
    
asked by Tony Muñoz 20.06.2017 в 15:41
source

1 answer

1

Your error is in the IF you are saying that $_SESSION['idRol'] == 1 and that at the same time is equal to 2 which is not possible I think you should change it for an OR.

if ($_SESSION[ 'idRol' ] == 1 || $_SESSION[ 'idRol' ] == 2)

In this way you will enter the IF whenever the variable has the value of 1 or 2

UPDATE

To validate several ID's you can create an array containing all your valid Roles Id and ask if the Session Id is inside it.

$id_validos = array(1,2,3,4);
if (in_array($_SESSION['idRol'],$id_validos))

Ó

You can also ask if the Id is greater than or equal to your lower ID limit and if it is less than or equal to the upper limit of your ID range

if($_SESSION['idRol'] >= 1 && $_SESSION['idRol'] <= 4)
    
answered by 20.06.2017 / 15:47
source