Jump to content

IF 2 arrays are true


Boxerman

Recommended Posts

Hey guys,

 

Hopefully a quick one for you...

 

I'm trying to check if two arrays match then output text.

 

what i have is: 

if (in_array($usernamelower, $superusers AND $adminusers, true)) {

full code: 

//Let's see who has what access
$adminsql = "SELECT * FROM permissions WHERE level = 'Admin' ";
$superusersql = "SELECT * FROM permissions WHERE level = 'Super User' ";
$adminresult = mysql_query($adminsql) or die('Query failed: ' . mysql_error()); 
$superuserresult = mysql_query($superusersql) or die('Query failed: ' . mysql_error()); 
//Let's build the adminuser array
while($adminrow = mysql_fetch_assoc($adminresult)){ 
    $adminusers[] =$adminrow['username']; 
}  
//Let's build the superuser array
while($superuserrow = mysql_fetch_assoc($superuserresult)){ 
    $superusers[] =$superuserrow['username']; 
} 

the if(in array) doesnt work :( can anyone help with this one.. i've tried AND, ||,  && 

Link to comment
https://forums.phpfreaks.com/topic/296463-if-2-arrays-are-true/
Share on other sites

Do you mean the table would look like this, where user A is both Admin and Super User?

mysql> SELECT * FROM permissions;
+----+----------+------------+
| id | username | level      |
+----+----------+------------+
|  1 | A        | Admin      | *
|  2 | B        | Moderator  |
|  3 | C        | Admin      |
|  4 | D        | Super User |
|  5 | E        | Moderator  |
|  6 | A        | Super User | *
+----+----------+------------+

then

WHERE level = 'Admin' AND level = 'Super User'

will not find any records as there is no single record that has both values at the same time (impossible)

mysql> SELECT username
    -> FROM permissions
    -> WHERE level = 'Admin' AND level = 'Super User';
Empty set (0.00 sec)

Using OR instead of AND would produce

mysql> SELECT username
    -> FROM permissions
    -> WHERE level = 'Admin' OR level = 'Super User';
+----------+
| username |
+----------+
| A        |
| C        |
| D        |
| A        |
+----------+

However, to find only users with both

mysql> SELECT A.username
    -> FROM permissions A
    -> INNER JOIN
    ->     permissions B
    ->     ON A.username = B.username
    ->     AND A.level = 'Admin'
    ->     AND B.level='Super User';
+----------+
| username |
+----------+
| A        |
+----------+

Back to the original question. From the two arrays, $admins and $superusers, to find who is in both arrays use array_intersect()

$admins = array ('A', 'C');
$superusers = array ('D', 'A');

$both = array_intersect($admins, $superusers);

echo '<pre>',print_r($both, true),'</pre>';

/** RESULT *****

    Array
    (
        [0] => A
    )

****************/

If the levels are a single value do it in one loop and make arrays comparing the levels

$adminusers = array();

$superusers = array();

$sql = "SELECT (username,level) FROM permissions WHERE level = 'Admin' OR level = 'Super User' ";
$result = mysql_query($sql) or die('Query failed: ' . mysql_error());

while($row = mysql_fetch_assoc($result)){
    if($row['level'] == 'Admin'){
        $adminusers[] = $row['username']; 
    }else{
        $superusers[] = $row['username']; 
    }
} 

Or if wanted to get all users privileges

$adminusers = array();
$superusers = array();
$users = array();

$sql = "SELECT (username,level) FROM permissions";
$result = mysql_query($sql) or die('Query failed: ' . mysql_error());

while($row = mysql_fetch_assoc($result)){
    if($row['level'] == 'Admin'){
        $adminusers[] = $row['username'];
    }elseif($row['level'] == 'Super User'){
        $superusers[] = $row['username'];
    }else{
        $users[] = $row['username'];
   }
} 

Now this would be ok if wanted a huge list, I personally like to just check what their level is on login. Set in a session if matches admin or superuser. Otherwise no login or not matching those in session would be set as a normal user in session.

 

On the subject of naming levels it's easier to do a numbering one such as 1-10.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.