Hi,
I'm having trouble scaling a script I wrote a while back...
background:
The script is like a search-able address book. There are 8 access levels and the user can have permission access to a combination of any/all levels. The results returned are based on the access level combination as a sql query is created based on that access level combination i.e. a user can only see other users at their level.
There used to be only 3 levels and so I had a block of conditional statements to generate the required sql query. However with 8 levels there would be too many statements to maintain and errors are very likely.
I can only think that the best way to do this is to use a multi-dimensional array (?) but I have no idea how to set it up and then traverse it to get the relevant values out to use (I don't have much experience with multi-dimensional arrays).
This is what the current code is:
//Create the sql conditions for each access level
if ($access1 == 'yes')
{
$sql1 = "usertype= 'Principal' or usertype = 'Principal Assistant' or usertype = 'Named' or usertype = 'E-mail' or usertype = 'Web' ";
}
if ($access2 == 'yes')
{
$sql2 = "usertype = 'Principal' or usertype = 'Deputy Principal' or usertype = 'Contact'";
}
if ($access3 == 'yes')
{
$sql3 = "usertype = 'Principal' or usertype = 'Contact' ";
}
//join them
if (($access1 == 'yes') and ($access2 == 'yes') and ($access3 == 'yes')) //all 3
{ $sqlconditions = $sql1." or ".$sql2." or ".$sql3; }
elseif (($access1 == 'yes') and ($access2 == 'yes') and ($access3 == 'no'))
{ $sqlconditions = $sql1." or ".$sql2; }
elseif (($access1 == 'yes') and ($access2 == 'no') and ($access3 == 'no'))
{ $sqlconditions = $sql1; }
elseif (($access1 == 'no') and ($access2 == 'yes') and ($access3 == 'yes'))
{ $sqlconditions = $sql2." or ".$sql3; }
elseif (($access1 == 'no') and ($access2 == 'yes') and ($access3 == 'no'))
{ $sqlconditions = $sql2; }
elseif (($access1 == 'no') and ($access2 == 'no') and ($access3 == 'yes'))
{ $sqlconditions = $sql3; }
elseif (($access1 == 'yes') and ($access2 == 'no') and ($access3 == 'yes'))
{ $sqlconditions = $sql1." or ".$sql3; }
$sqlconditions is passed into the main sql query as a condition. For each access level we have different user types i.e. 'Principal', 'Contact' etc. (btw, none of user access values or types are held in a local db - they are written into the session when a user logs in after the db of a third party crm is interrogated )
Any help or direction is greatly appreciated.
Thanks,
C.