do while循环PHP具有多个条件

huangapple go评论52阅读模式
英文:

do while loop php with multiple conditions

问题

以下是您提供的代码的中文翻译部分:

<?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;

/*获取所有活动总数*/
$total_campaigns = get_total_campaigns($conn);

/*检查是否有可用于发送数据的活动*/
if ($total_campaigns > 0) {

    do {
        //从所有可用活动中获取一个活动ID。
        $c_id = get_campaign($total_campaigns, $total_checked_campaigns, $checked_campaigns_ids, $current_campaign_id, $conn);

        /*此while循环检查所选活动是否已达到每日限制,如果上述活动ID达到了限制,它应该获取另一个可能的活动ID,如果没有可用的活动ID,
        get_campaign函数将返回0,然后我们将停止循环*/
    } 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; //活动状态为1(表示活动处于进行中状态)
    $c_lead_status = 0; //潜在客户状态为0(表示尚未处理)

    $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'];
        // 在下面两行更新了总检查的活动计数以及更新了已检查的活动ID的逗号分隔列表
        $total_checked_campaigns = $total_checked_campaigns + 1;
        $checked_campaigns_ids = $checked_campaigns_ids . "," . $c_id;
        //返回活动ID以处理数据
        return $c_id;

    } else {
        // 返回0,因为没有可用于处理数据的活动
        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;
    }
}
?>

希望这有助于理解您的代码。如果您有任何问题或需要进一步的帮助,请随时告诉我。

英文:

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

&lt;?php
require &#39;../includes/env.php&#39;;
require &#39;../includes/db.inc.php&#39;;
$total_campaigns = 0;
$total_checked_campaigns = 0;
$checked_campaigns_ids =&quot;0&quot;;
$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&gt;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 &lt;= $total_campaigns) &amp;&amp; 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 = &quot;SELECT * FROM tbl_software_campaigns WHERE c_lead_status = ? AND c_status = ? AND c_id &gt; ? AND c_id NOT IN($checked_campaigns_ids) ORDER by c_id ASC LIMIT 1&quot;;
$stmt = $conn-&gt;prepare($selectCampaignQuery); 
$stmt-&gt;bind_param(&quot;iii&quot;, $c_lead_status,$c_status, $current_campaign_id); 
$stmt-&gt;execute();
$selectCampaignResult = $stmt-&gt;get_result();
if ($selectCampaignResult-&gt;num_rows==0) {
if($total_checked_campaigns==0){
$update_campaigns_query = &quot;UPDATE tbl_software_campaigns SET c_lead_status = 0 WHERE c_status = 1&quot;;
$update_campaign_result = mysqli_query($conn,$update_campaigns_query);
$selectCampaignQuery = &quot;SELECT * FROM tbl_software_campaigns WHERE c_lead_status = ? AND c_status = ? AND c_id &gt; ? AND c_id NOT IN($checked_campaigns_ids) ORDER by c_id ASC LIMIT 1&quot;;
$stmt = $conn-&gt;prepare($selectCampaignQuery); 
$stmt-&gt;bind_param(&quot;iii&quot;, $c_lead_status,$c_status, $current_campaign_id);
$stmt-&gt;execute();
$selectCampaignResult = $stmt-&gt;get_result();
}
}
if ($selectCampaignResult-&gt;num_rows&gt;0) {
global $total_checked_campaigns;
global $c_daily_limit;
global $checked_campaigns_ids;
$row = mysqli_fetch_assoc($selectCampaignResult);
$c_id = $row[&#39;c_id&#39;];
$c_daily_limit = $row[&#39;c_daily_limit&#39;];
// 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.&quot;,&quot;.$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-&gt;prepare(&quot;SELECT COUNT(c_id) AS total_campaigns FROM tbl_software_campaigns WHERE c_status = 1&quot;);
$stmt-&gt;execute();
$total_result = $stmt-&gt;get_result();
$row = mysqli_fetch_assoc($total_result);
$stmt-&gt;close();
return $row[&#39;total_campaigns&#39;];
}
function is_duplicate($EMAIL,$conn){
$query = &quot;SELECT lead_id FROM tbl_software_leads WHERE lead_email = ?&quot;;
$stmt = $conn-&gt;prepare($query);
$stmt-&gt;bind_param(&quot;s&quot;, $EMAIL);
$stmt-&gt;execute();
$duplicate_result = $stmt-&gt;get_result();
$stmt-&gt;close();
if ($duplicate_result-&gt;num_rows&gt;0){
return true;
}else{
return false;
}
}
function is_daily_limit_allow($CAMPAIGN,$LIMIT,$conn){
if($LIMIT&gt;0){
$limit_query = &quot;SELECT count(*) as today_total FROM tbl_software_leads WHERE DATE(lead_time)=CURDATE() AND lead_camp_id =? AND lead_status = 1&quot;;
$stmt = $conn-&gt;prepare($limit_query);
$stmt-&gt;bind_param(&#39;i&#39;,$CAMPAIGN);
$stmt-&gt;execute();
$limitrow = $stmt-&gt;get_result()-&gt;fetch_assoc();
$todaysrecord = $limitrow[&#39;today_total&#39;];
if($todaysrecord&lt;$LIMIT){
return true;
}else{
return false;
}
}else{
return true;
}
}
?&gt;

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!

答案1

得分: 0

以下是翻译好的代码部分:

尝试以下代码片段

    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));

在这里,根据您尝试获取的条件,我更改了条件为

    (($c_id == 0 || $total_checked_campaigns <= $total_campaigns) && is_daily_limit_allow($c_id))
英文:

Try below snippet once

do {
echo $c_id;
echo &quot;&lt;br&gt;&quot;;
$c_id = get_campaign($total_campaigns);
} while (($c_id == 0 || $total_checked_campaigns &lt;= $total_campaigns) &amp;&amp; is_daily_limit_allow($c_id));

here I changed condiotions as per your trying to get

(($c_id == 0 || $total_checked_campaigns &lt;= $total_campaigns) &amp;&amp; is_daily_limit_allow($c_id))

答案2

得分: 0

我明白,以下是代码的中文翻译:

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));

或者

while(!(($c_id == 0 || $total_checked_campaigns <= $total_campaigns) && is_daily_limit_allow($c_id)))
英文:

as i understand can you try this code

  do {
echo $c_id;
echo &quot;&lt;br&gt;&quot;;
$c_id = get_campaign($total_campaigns);
} while (($c_id != 0 &amp;&amp; $total_checked_campaigns &lt;= $total_campaigns) || !is_daily_limit_allow($c_id));

or

while(!(($c_id == 0 || $total_checked_campaigns &lt;= $total_campaigns) &amp;&amp; is_daily_limit_allow($c_id)))

答案3

得分: 0

你说:

  • 如果广告系列ID为0,我想要停止循环
  • 如果已检查的广告系列总数等于总广告系列数,我想要停止循环,因为这意味着我们已经检查了所有的广告系列,没有可用于处理数据的广告系列
  • 如果当前广告系列ID允许处理数据且没有达到每日限制,我想要停止循环。

因此,它形成了如下直接的条件:

while($c_id != 0 && $total_checked_campaigns < $total_campaigns && is_daily_limit_allow($c_id, $c_daily_limit, $conn));
英文:

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 &amp;&amp; $total_checked_campaigns &lt; $total_campaigns &amp;&amp; is_daily_limit_allow($c_id,$c_daily_limit,$conn));

huangapple
  • 本文由 发表于 2023年2月8日 11:55:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75381230.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定