按用户排名限制内容

时间:2017-01-18 15:36:47

标签: php

基本上我试图限制登录用户可以看到的内容,具体取决于他们的排名。内容是几行,所有行都有不同的等级要求。如果用户没有所需的排名,他将无法查看该行。 但是,我的问题是,如果一行的排名要求高于其下方的任何行,并且用户没有该排名,则下面的所有行也将不可见。

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;
}

更新:这就是我的意思 enter image description here

1 个答案:

答案 0 :(得分:1)

我制作了一些重构,但实际上,您将使用不同的函数来查看用户是否可以看到某个类别:

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;
}

但这只是反映了一个不遵循First Normal Form

的糟糕数据库设计