[英]filter results based on user level CASE v. IF
Okay I am having an issue with this.好的,我对此有疑问。
I have 2 different tables that I am trying use to restrict access to records.我有 2 个不同的表,我试图用它们来限制对记录的访问。
Users are based on 4 levels.用户基于 4 个级别。 lvl 1 being the highest and lvl 4 being the lowest.
1级最高,4级最低。
The users table has 7 fields.用户表有 7 个字段。 The other table also has the fields below indicated by {train_...}
另一个表也有下面由 {train_...} 指示的字段
In my php code I have tried IF statements, and In my SQL I have tried CASE statements.在我的 php 代码中,我尝试了 IF 语句,而在我的 SQL 中,我尝试了 CASE 语句。
I cannot get the IF statement to function, as I keep getting a syntax error.我无法使 IF 语句起作用,因为我不断收到语法错误。 Parse Error: unexpected } on line ....
解析错误:意外的} 在线....
The CASE statement works for the SQL, however, it causes issues with dynamic searches once the form is loaded. CASE 语句适用于 SQL,但是,一旦加载表单,它就会导致动态搜索出现问题。
The If Statment If 语句
[] are global variables and {} indicate field names. [] 是全局变量,{} 表示字段名称。
if ('[usr_level]' == 1)
{
'{train_agencyID}' == '[agencyID]'
}
elseif ('[usr_level]' == 2)
{
'{train_componentID}' == '[componentID]'
}
elseif ('[usr_level]' == 3)
{
'{train_UnitMSC}' == '[mscID]'
}
elseif ('[usr_level]' == 4)
{
'{train_Unit_Comp}' == '[submscID]'
};
The CASE Statement案例声明
WHERE '[usr_level]'
CASE WHEN '[usr_level]' = 1 THEN
train_agencyID = '[agencyID]'
END
OR
CASE WHEN '[usr_level]' = 2 THEN
train_componentID = '[componentID]'
END
OR
CASE WHEN '[usr_level]' = 3 THEN
train_UnitMSC = '[mscID]'
END
OR
CASE WHEN '[usr_level]' = 4 THEN
train_Unit_Comp = '[submscID]'
END
I've been racking my brain for 3 days now and I'm missing something simple but for the life of my I cannot figure this out.我已经绞尽脑汁 3 天了,我错过了一些简单的东西,但在我的一生中,我无法弄清楚这一点。
I think this may work for you, here is a basic switch statement with php.我认为这可能对你有用,这是一个带有 php 的基本 switch 语句。
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<form method="post">
<input type="text" name="user_level" />
<input type="submit" value="submit" />
</form>
</body>
</html>
<?php
$user_level = filter_input(INPUT_POST, 'user_level');
switch($user_level) {
case '1':
$train_agencyID = 'agencyID';
break;
case '2':
$train_componentID = 'componentID';
break;
case '3':
$train_UnitMSC = 'mscID';
case '4':
$train_Unit_Comp = 'submscID';
break;
default:
$error = "Invalid selection.";
}
if(isset($train_agencyID)) {
echo $train_agencyID;
}
elseif(isset($train_componentID)) {
echo $train_componentID;
}
elseif(isset($train_UnitMSC)) {
echo $train_UnitMSC;
}
elseif(isset($train_Unit_Comp)) {
echo $train_Unit_Comp;
}
?>
I added more code so you can test this and see the output for yourself.我添加了更多代码,以便您可以对此进行测试并亲自查看输出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.