-4
foreach( $users as $u ) {
echo '<tr><td>' . $u->nome . '</td><td>' . $u->username . '</td><td>' . $u->email . '</td><td>' . ( $u->acesso == 1 ? '<b>Administrador</b>' : 'Aluno' ) . '</td><td><a href="/website/admineditar.php?id=' . $u->id . '">Alterar</a> <a href="/website/adminremover.php?id=' . $u->id . '" onclick="return confirm(\'Deseja mesmo remover este utilizador?\');">Remover</a></td></tr>';
}

so my problem is on this part

( $u->acesso == 1 ? '<b>Administrador</b>' : 'Aluno' )

i would like it to be doing something like this

( $u->acesso == 1 ? '<b>Administrador</b>' :  $u->acesso == 0 ? 'Aluno' : 'Enc. educação' )

but don't know how to do it. I tried to use if and elseif but gives me an error.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Rui Couto
  • 26
  • 4
  • Does this answer your question? [Using nested ternary operators](https://stackoverflow.com/questions/8735280/using-nested-ternary-operators) – showdev Jan 28 '20 at 10:59

2 Answers2

0

If you are trying to create a multi-role system, you should set the role of the user first (Administrador, Aluno or Enc. educação) either by different role number 1, 2, 3 for example inside the user object and then allow access accordingly.

As for nested ternaries, this is how you would do it :

$u->acesso === 1 ? 'Administrador': ($u->acesso === 2 ? 'Aluno' : 'Enc. educação' );

In this case, Administrator is 1, Aluno is 2 and Enc.educação is everything else. Note the ===, which checks the type of variable as $u->acesso == 1 would evaluate to true if $u->acesso was everything except 0 or false; In php == does not check the variable type (int, string, object etc.)

Francois
  • 3,050
  • 13
  • 21
0

Solved it after all... here is how i solved it after one hour... tnhx to everyone who demoted the question without having the trouble to answer... very smart to downrate something u cant answer...

anyway heres the solution i got...

foreach( $users as $u ) {
echo '<tr><td>' . $u->nome . '</td><td>' . $u->username . '</td><td>' . $u->email . '</td><td>' ;

if ( $u->acesso == 1) {
    echo '<b>Administrador</b>';
}elseif ( $u->acesso == 2) {
    echo 'Enc. Educação';
}else{
    echo 'Aluno';
}

echo  '</td><td><a href="/website/admineditar.php?id=' . $u->id . '">Alterar</a> <a href="/website/adminremover.php?id=' . $u->id . '" onclick="return confirm(\'Deseja mesmo remover este utilizador?\');">Remover</a></td></tr>';
}
Rui Couto
  • 26
  • 4