Show content based on permission

Berk

berkibap#4233
Developer
Oct 17, 2015
863
190
Sup yo
I'm doing an admin panel (not habbo related fuck)
and i need to show some content based on user's permission level, like 1 2 3 or 4 or whatever. (btw im usin xampp rn)
My Php code below:
PHP:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "lumino";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT permission FROM users WHERE username = 'Berkay'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        
    }
}
$permission = $row["permission"];
?><?php if($permission >= 1)  { echo'<li class="parent"><a href="usermanagement.php"><i class="fa fa-users" aria-hidden="true"></i> User Management</a></li> ';} ?>
 

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,638
2,393
It'll come out as a string, so you need to convert it to an integer.

If it's wrong, bear in mind that I've not coded PHP in about 8 months now.

PHP:
$result = $conn->query("SELECT `permission` FROM `users` WHERE `username` = 'Berkay'");
if ($result->num_rows > 0) {
    $user_info = $result->fetch_array();
} else {
    echo 'Error finding user';
}

if (isset($user_info['permission']) && ((int) $user_info['permission']) >= 1) {
    echo '<li class="parent"><a href="usermanagement.php"><i class="fa fa-users" aria-hidden="true"></i> User Management</a></li>';
}
 

Berk

berkibap#4233
Developer
Oct 17, 2015
863
190
It'll come out as a string, so you need to convert it to an integer.

If it's wrong, bear in mind that I've not coded PHP in about 8 months now.

PHP:
$result = $conn->query("SELECT `permission` FROM `users` WHERE `username` = 'Berkay'");
if ($result->num_rows > 0) {
    $user_info = $result->fetch_array();
} else {
    echo 'Error finding user';
}

if (isset($user_info['permission']) && ((int) $user_info['permission']) >= 1) {
    echo '<li class="parent"><a href="usermanagement.php"><i class="fa fa-users" aria-hidden="true"></i> User Management</a></li>';
}
thanks, fixed! <3_<3<3_<3<3_<3<3_<3wowsoBLESSED
@JMG close the thread, ty
 

Zaka

Programmer
Feb 9, 2012
471
121
I would highly suggest you create a method for checking permissions instead which takes a parameter (the username) and returns a bool response or 0 and 1. Then you can easily just create a if statement with the conditions in a single phrase, and inside of that statement put all the data for that permission.

Think like this, every function you would need more than once, needs a own definition, it needs to be a method.
 

Users who are viewing this thread

Top