skip to Main Content

I am posting full code here for make my situation more clear.

<?php
require '../includes/env.php';
require '../includes/db.inc.php';

$total_campaigns = 0;
$total_checked_campaigns = 0;
$checked_campaigns_ids ="0";
$current_campaign_id = 0;
$c_rull = 1;
$c_daily_limit = 0;

/*get all live campaigns count*/
$total_campaigns = get_total_campaigns($conn);

/*check if any campaign available for send data*/
if($total_campaigns>0){
    
        do {
            //get one campaign id from all available campaigns.
            $c_id =  get_campaign($total_campaigns,$total_checked_campaigns,$checked_campaigns_ids,$current_campaign_id,$conn);
            
            /*this while loop is checking that selected campaign have reached daily limit or not,if above campaign id reached limit, it should get another possible campaign id, if there no any campaign id avaialble
            get_campaign function will return 0 and we will stop the loop*/
        }  while(!(($c_id == 0 || $total_checked_campaigns <= $total_campaigns) && is_daily_limit_allow($c_id,$c_daily_limit,$conn)));
        
        echo $c_id;
}

function get_campaign($total_campaigns,$total_checked_campaigns,$checked_campaigns_ids,$current_campaign_id,$conn){
    $c_status = 1;//live
    $c_lead_status = 0;//not processed
    
    $selectCampaignQuery = "SELECT * FROM tbl_software_campaigns WHERE c_lead_status = ? AND c_status = ? AND c_id > ? AND c_id NOT IN($checked_campaigns_ids) ORDER by c_id ASC LIMIT 1";
    $stmt = $conn->prepare($selectCampaignQuery); 
    $stmt->bind_param("iii", $c_lead_status,$c_status, $current_campaign_id); 
    $stmt->execute();
    $selectCampaignResult = $stmt->get_result();
    if ($selectCampaignResult->num_rows==0) {
        
        if($total_checked_campaigns==0){
            $update_campaigns_query = "UPDATE tbl_software_campaigns SET c_lead_status = 0 WHERE c_status = 1";
            $update_campaign_result = mysqli_query($conn,$update_campaigns_query);
            
            
            $selectCampaignQuery = "SELECT * FROM tbl_software_campaigns WHERE c_lead_status = ? AND c_status = ? AND c_id > ? AND c_id NOT IN($checked_campaigns_ids) ORDER by c_id ASC LIMIT 1";
            $stmt = $conn->prepare($selectCampaignQuery); 
            $stmt->bind_param("iii", $c_lead_status,$c_status, $current_campaign_id);
            $stmt->execute();
            $selectCampaignResult = $stmt->get_result();
        }
        
    }
    
    if ($selectCampaignResult->num_rows>0) {
        global $total_checked_campaigns;
        global $c_daily_limit;
        global $checked_campaigns_ids;
        
        $row = mysqli_fetch_assoc($selectCampaignResult);
        $c_id = $row['c_id'];
        $c_daily_limit = $row['c_daily_limit'];
        // we are updating total checked campaign count here as well updating comma seperated list of checked campaign id here in below two line
        $total_checked_campaigns = $total_checked_campaigns+1;
        $checked_campaigns_ids = $checked_campaigns_ids.",".$c_id;
        //return campaign id for process the data
        return $c_id;
        
    }else{
        //return 0 because there no any campaign available to process data
        return 0;
    }
    
}

function get_total_campaigns($conn){
    $stmt = $conn->prepare("SELECT COUNT(c_id) AS total_campaigns FROM tbl_software_campaigns WHERE c_status = 1");
    $stmt->execute();
    $total_result = $stmt->get_result();
    $row = mysqli_fetch_assoc($total_result);
    $stmt->close();
    return $row['total_campaigns'];
}

function is_duplicate($EMAIL,$conn){
   $query = "SELECT lead_id FROM tbl_software_leads WHERE lead_email = ?";
   $stmt = $conn->prepare($query);
   $stmt->bind_param("s", $EMAIL);
   $stmt->execute();
   $duplicate_result = $stmt->get_result();
   $stmt->close();
   if ($duplicate_result->num_rows>0){
       return true;
   }else{
       return false;
   }
   
}

function is_daily_limit_allow($CAMPAIGN,$LIMIT,$conn){
    if($LIMIT>0){
        $limit_query = "SELECT count(*) as today_total FROM tbl_software_leads WHERE DATE(lead_time)=CURDATE() AND lead_camp_id =? AND lead_status = 1";
        $stmt = $conn->prepare($limit_query);
        $stmt->bind_param('i',$CAMPAIGN);
        $stmt->execute();
        $limitrow = $stmt->get_result()->fetch_assoc();
    
        $todaysrecord = $limitrow['today_total'];
    
        if($todaysrecord<$LIMIT){
            return true;
        }else{
            return false;
        }
    }else{
        return true;
    }
}


?>

Now Let me explain my goal.

What I am looking to do is

  1. on start its checking how many campaigns available there!
  2. if campaign available more than 0, I am getting one campaign id and marking that campaign as checked campaign using get_campaign function
  3. get_campaign function giving me campaign id and if there no any campaign available for process, its give me 0 as id.
  4. if there 0 as id, I do not want do anything since it means there no any campaign available for handle the data
  5. if get_campaign give me any campaign id, I need to check that its reached daily limit or not with is_daily_limit_allow function, if its reached that limit, I need to get new id using get_campaign function and check its daily limit.

I am trying to achieve above thing with while loop but its not working properly and running for infinity time.

Basically when

if campaign id is 0, I want stop the loop
if total checked campaigns equal to total campaigns, I want stop the loop because it means we have checked all campaigns and none are available for process the data
 
if current campaign id allow to process data and have not daily limit, I want stop the loop.

I should also clear that when

$c_daily_limit = 0;

means there no any daily limit in that campaign.

I hope its clear everything now and will help to expert here on stackoverflow.

Let me know if anyone can help me for solve the puzzle.

Thanks!

3

Answers


  1. Try below snippet once

    do {
        echo $c_id;
        echo "<br>";
        $c_id = get_campaign($total_campaigns);
    } while (($c_id == 0 || $total_checked_campaigns <= $total_campaigns) && is_daily_limit_allow($c_id));
    

    here I changed condiotions as per your trying to get

    (($c_id == 0 || $total_checked_campaigns <= $total_campaigns) && is_daily_limit_allow($c_id))
    
    Login or Signup to reply.
  2. as i understand can you try this code

      do {
            echo $c_id;
            echo "<br>";
            $c_id = get_campaign($total_campaigns);
        } while (($c_id != 0 && $total_checked_campaigns <= $total_campaigns) || !is_daily_limit_allow($c_id));
    

    or

    while(!(($c_id == 0 || $total_checked_campaigns <= $total_campaigns) && is_daily_limit_allow($c_id)))
    
    Login or Signup to reply.
  3. You said

    • if campaign id is 0, I want stop the loop
    • if total checked campaigns equal to total campaigns, I want stop the loop because it means we have checked all campaigns and none are
      available for process the data
    • if current campaign id allow to process data and have not daily limit, I want stop the loop.

    So, it makes straightforward conditions as below,

    while($c_id != 0 && $total_checked_campaigns < $total_campaigns && is_daily_limit_allow($c_id,$c_daily_limit,$conn));
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search