2017-01-18 8 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

よかった、ありがとう。私はその記事を念頭に置いておきます。 –

関連する問題