I need a little bit of help here. I made ranks in mysql database.
ranks look like this in mysql:
Code:
ALTER TABLE `tbl_users` CHANGE `Rank` `Rank` ENUM('User','Power User','Uploader','Moderator','Administrator',) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL DEFAULT 'User';
So. i want to hide menu name 'upload file' for Ranks User* and **Power user. And show it for Uploader , Moderator , Administrator
Here is my code in navigation menu for upload page:
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Torrenti <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="#">.....</a></li>
<li role="separator" class="divider"></li>
<li><a href="#">.....</a></li>
<li><a href="#">.....</a></li>
<li role="separator" class="divider"></li>
<li class="dropdown-header">.....</li>
<li><a href="upload.php">Upload file</a></li>
<li role="separator" class="divider"></li>
<li><a href="#">.....</a></li>
</ul>
</li>
Here is my PHP code for checking if user is already logged in:
<?php
session_start();
require_once 'class.user.php';
$user_home = new USER();
if(!$user_home->is_logged_in())
{
$user_home->redirect('index.php');
}
$stmt = $user_home->runQuery("SELECT * FROM tbl_users WHERE userID=:uid");
$stmt->execute(array(":uid"=>$_SESSION['userSession']));
$row = $stmt->fetch(PDO::FETCH_ASSOC);
?>
The easiest way would be like so:
<?php if ($row["Rank"] == "Uploader" || $row["Rank"] == "Moderator" || $row["Rank"] == "Administrator") { ?>
<li><a href="upload.php">Upload file</a></li>
<?php } ?>
A more elegant way would be to define an array of ranks with permission and check if the rank is in the array like so:
<?php
$ranks_with_upload_persmission = array("Uploader", "Moderator", "Administrator");
if(in_array($row["Rank"], $ranks_with_upload_permission))
{
?>
<li><a href="upload.php">Upload file</a></li>
<?php
}
?>
This method has the advantage that you don't have to write that many OR operators. Second it's more readable and third it's reusable. If you want to hide more elements on the page you just have to do in_array instead of copying the whole if with many ORs.