<?php
session_start();
if(!isset($_SESSION['titles']) || !isset($_SESSION['positions'])|| !isset($_SESSION['usernames'])){
header("location:index.php");}

function racss($pr)
{
if(!isset($_SESSION['titles']) || !isset($_SESSION['positions'])|| !isset($_SESSION['usernames'])){
header("location:index.php");}
else
{
	
$conn=mysqli_connect("localhost","primarysales","S5L7lF01EQZvQrGK","primarysales_dianaold");

$sts=0;
$rts=0;
$rws=0;

$flt=" where agent_names is not null ";
$sqlst="select zones,regions,agents,territorys,chanell,executive,rsmname from acs where user_names='".$_SESSION['usernames']."' ";
$qry=mysqli_query($conn,$sqlst);
$fn=mysqli_num_fields($qry);
$rn=mysqli_num_rows($qry);

if($rn==0){
return "access failed";}
else
{
	
$sts=0;
while($rst=mysqli_fetch_row($qry)){
for($cnt=0;$cnt<$fn;$cnt++){
if(strtolower($rst[$cnt])!='all'){
$sts++;}}
}

if($sts>0){	
$flt=$flt."and (";
$qry=mysqli_query($conn,$sqlst);
while($rst=mysqli_fetch_row($qry)){
$sts=0;
$rts=0;
	
for($cnt=0;$cnt<$fn;$cnt++){
if(strtolower($rst[$cnt])!='all'){
$sts++;}
}

if($sts>0){
if($rws==0){
$flt=$flt." (";}	
if($rws>=1){
$flt=$flt." or (";}	

$rws++;
$rts=0;
	
if(strtolower($rst[0])!='all'){
$rts++;	
$flt=$flt." zones='".$rst[0]."' ";}

if(strtolower($rst[1])!='all'){
if($rts>0){	
$flt=$flt."and regions='".$rst[1]."' ";}
else{
$flt=$flt."regions='".$rst[1]."' ";}
$rts++;}

if(strtolower($rst[2])!='all'){
if($rts>0){	
$flt=$flt."and agent_names='".$rst[2]."' ";}
else{
$flt=$flt."agent_names='".$rst[2]."' ";}
$rts++;}

if(strtolower($rst[3])!='all'){
if($rts>0){	
$flt=$flt."and territory_names='".$rst[3]."' ";}
else{
$flt=$flt."territory_names='".$rst[3]."' ";}
$rts++;}

if(strtolower($rst[4])!='all'){
if($rts>0){	
$flt=$flt."and chanell='".$rst[4]."' ";}
else{
$flt=$flt."chanell='".$rst[4]."' ";}
$rts++;}

if(strtolower($rst[5])!='all'){
if($rts>0){	
$flt=$flt."and executive_names='".$rst[5]."' ";}
else{
$flt=$flt."executive_names='".$rst[5]."' ";}
$rts++;}

if(strtolower($rst[6])!='all'){
if($rts>0){	
$flt=$flt."and rsm_names='".$rst[6]."' ";}
else{
$flt=$flt."rsm_names='".$rst[6]."' ";}
$rts++;}

$flt=$flt.")";
}
}

$flt=$flt.")";
}
return $flt;
}
}
}
?>
