dpntq48842 2017-01-18 15:36
浏览 12
已采纳

按用户排名限制内容

Basically I'm trying to limit what a logged in user can see depending on what their rank is. The content is several rows, all with different rank requirements. If the user doesn't have the required rank, he will not be able to view that row. However, my problem here is that if one row has a higher rank requirement than any rows below it and the user does not have that rank, all rows below will not be visible either.

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

UPDATE: This is what I mean enter image description here

  • 写回答

1条回答 默认 最新

  • dongzhi4470 2017-01-18 16:30
    关注

    I made some refactor, but in essence, you will use different functions to see if a user can see some category:

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

    But this just reflect a poor database design that doesn't follow the First Normal Form

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?