I have a function to check wether a function/module is disabled by an admin or not.
The function looks like this (in functions.php):
function function_enabled($function_module) {
include("dbconnect.php");
if ($function_module == "changelogs" OR $function_module == "news" OR $function_module == "calendar" OR $function_module == "groups" OR $function_module == "settings" OR $function_module == "eat" OR $function_module == "weather" OR $function_module == "usertext" OR $function_module == "login") {
$sql = $db->query("SELECT enabled FROM functions WHERE function_name = '$function_module'");
$row = $sql->fetch_object();
$return = $row->enabled;
return $return;
}
}
And this is how I use the function (in any other php file):
include("functions.php");
if(function_enabled("weather") == 1) {
//do the weather stuff here
} else {
echo"Function disabled";
}
Now my question: How is the performance of this function? Can this cause relative high performance hits? Is there a better/faster way to check wether a function is enabled or not or is this script okay?
UPDATE 1
Now, I combined these multiple queries into one so that all values are in an array.
function compare_id($a, $b) {
if ($a['id'] == $b['id']) return 0;
return ($a['id'] < $b['id']) ? -1 : 1;
}
$sql = $db->query("SELECT function_name, enabled, id FROM functions");
$function_array = array();
while ($row = mysqli_fetch_assoc($sql)) {
$function_array[] = $row;
}
usort($function_array, compare_id);
if($function_array[0]['enabled'] == 1) {
echo"changelogs enabled<br>";
} else {
echo"changelogs disabled<br>";
}
if($function_array[2]['enabled'] == 1) {
echo"calendar enabled<br>";
} else {
echo"calendar disabled<br>";
}
if($function_array[7]['enabled'] == 1) {
echo"usertext enabled<br>";
} else {
echo"usertext disabled<br>";
}
if($function_array[8]['enabled'] == 1) {
echo"login enabled<br>";
} else {
echo"login disabled<br>";
}
if($function_array[4]['enabled'] == 1) {
echo"settings enabled<br>";
} else {
echo"settings disabled<br>";
}
Is this method faster?