基本上,我试图限制登录用户所能看到的内容,这取决于他们的排名。内容有几行,都有不同的等级要求。如果用户没有所需的级别,他将无法查看该行。然而,在这里的问题是,如果一行比它下面的任何行都有更高的等级要求,并且用户没有该级别,那么下面的所有行也都是不可见的。
public function Categories() {
global $Template, $CatArray;
$CatArray = array();
$PermissionTable = array();
$QueryCat = $this->mysqli->query("SELECT * FROM categories ORDER BY id ASC");
while($FetchCat = $QueryCat->fetch_array()) {
$PermissionTable["category"] = array("id" => $FetchCat["id"]); // store category ID as an id sub-array
$data = explode(",", $FetchCat["ranks"]); // the ranks row in the database contains all ranks that can see this category, so here we split them up.
foreach($data as $number) {
$PermissionTable["category"] += array(
"rank" => $data // apply rank requirements in a sub-array again
);
}
if(in_array($Template["user"]["user_group"], $PermissionTable["category"]["rank"])) { // here, if the users rank is in the rank sub-array, they will be able to see it
$CatArray[] = $FetchCat;
} else { // otherwise display nothing
$CatArray[] = null;
}
}
$Template["CatArray"] = $CatArray;
return $CatArray;
}更新:这就是我的意思

发布于 2017-01-18 16:30:38
我做了一些重构,但本质上,您将使用不同的函数来查看用户是否可以看到某个类别:
public function Categories() {
global $Template, $CatArray;
$CatArray = array();
$PermissionTable = array();
$QueryCat = $this->mysqli->query("SELECT * FROM categories ORDER BY id ASC");
while($FetchCat = $QueryCat->fetch_array()) {
$categoryRanks = explode(",", $FetchCat["ranks"]); // the ranks row in the database contains all ranks that can see this category, so here we split them up.
$userCategoriesPermitted = in_array($Template["user"]["user_group"], $categoryRanks); //here we check if user rank is inside category rank
if($userCategoriesPermitted) {
$CatArray[] = $FetchCat; //add to return array
}
}
$Template["CatArray"] = $CatArray;
return $CatArray;
}但这只是反映了一个不遵循第一范式的糟糕的数据库设计
https://stackoverflow.com/questions/41723403
复制相似问题