2017-01-18 40 views
0

基本上我試圖限制什麼登錄用戶可以看到取決於他們的排名是什麼。內容是幾行,都有不同的等級要求。如果用戶沒有所需的排名,他將無法查看該行。 但是,我的問題在於,如果一行的排名要求高於其下的任何行,並且用戶沒有該排名,那麼下面的所有行也都將不可見。限制內容的用戶排名

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:這是我的意思 enter image description here

+0

你不能只是將用戶當前的等級添加到查詢中,如'WHERE rowRank <= userRank' – RiggsFolly

+0

不,因爲等級表看起來像1,2,3,4,5等,所以我不能使用<= opreator(它不是基於最小值需要排名,但是它在表格中寫下了什麼排名) –

+0

請向我們展示您的數據庫結構,以及您需要做什麼的真實示例。例如,用戶X有1,4,8個等級,他需要看到類別a,b和c。 –

回答

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

+0

即使我沒有要求的等級,這似乎也會顯示類別。 –

+0

我的錯。用新的「if(count)」替換該行。 –

+0

太好了,謝謝。我一定會記住這篇文章。 –