Home  >  Q&A  >  body text

php uses NOT operator to evaluate conditions

I have 4 sales offices and once all 4 sales offices have received 20 leads, only sales office number 4 should receive the remaining leads for the day. I need some help creating a condition to basically check if sales offices 1-4 have each received 20 leads and then obviously follow the instructions to proceed

First I query the database to check my counter (yes I know this can be more compact and done inside a loop)

$sales_office= 1;
$sql = $conn->prepare("SELECT LeadsReceivedToday FROM Lead_Counter WHERE SalesOffice=?");
$sql->execute([$sales_office]);
$result = $sql->fetch();
$sales_office1_count = $result['LeadsReceivedToday'];

$sales_office= 2;
$sql = $conn->prepare("SELECT LeadsReceivedToday FROM Lead_Counter WHERE SalesOffice=?");
$sql->execute([$sales_office]);
$result = $sql->fetch();
$sales_office2_count = $result['LeadsReceivedToday'];

$sales_office= 3;
$sql = $conn->prepare("SELECT LeadsReceivedToday FROM Lead_Counter WHERE SalesOffice=?");
$sql->execute([$sales_office]);
$result = $sql->fetch();
$sales_office3_count = $result['LeadsReceivedToday'];

$sales_office= 4;
$sql = $conn->prepare("SELECT LeadsReceivedToday FROM Lead_Counter WHERE SalesOffice=?");
$sql->execute([$sales_office]);
$result = $sql->fetch();
$sales_office4_count = $result['LeadsReceivedToday'];

When I execute this logic, it doesn't work as expected? Once sales_office = 1 reaches 20 leads it will be transferred directly to sales office 4?

if ($sales_office1_count != 20 && $sales_office2_count != 20 && $sales_office3_count != 20)
{
    //distribute leads to sales office 1-4 in order of 1-4
    
}
else
{
   //only send leads to sales office no 4
}

P粉618358260P粉618358260223 days ago401

reply all(1)I'll reply

  • P粉792673958

    P粉7926739582024-04-03 09:03:57

    Your current logic check for all office counts, you must check each office individually.

    if ($sales_office1_count < 20 || $sales_office2_count < 20 || $sales_office3_count < 20)
    {
        //distribute leads to sales office 1-4 in order of 1-4
        
    }
    else
    {
       //only send leads to sales office no 4
    }

    Additionally, you can improve the readability of your code by using arrays:

    $officesLeadsCount = [1 => 0, 2 => 0, 3 => 0, 4 => 0];
    
    foreach ($officesLeadsCount as $number => $value) {
      $sql = $conn->prepare("SELECT LeadsReceivedToday FROM Lead_Counter WHERE SalesOffice=?");
      $sql->execute([$number]);
      $result = $sql->fetch();
      $officesLeadsCount[$number] = $result['LeadsReceivedToday'];    
    }
    
    
    if ($officesLeadsCount[1] < 20 || $officesLeadsCount[2] < 20 || $officesLeadsCount[3] < 20)
    {
        //distribute leads to sales office 1-4 in order of 1-4
    
    }
    else
    {
       //only send leads to sales office no 4
    }

    reply
    0
  • Cancelreply