简体   繁体   English

在while循环的最后一次迭代中保留变量

[英]Leave variable on last iteration of while loop

I am stuck at a situation in my code. 我在代码中陷入困境。 A variable $end_date is iterating in a while loop for showing end date of five chapters. 变量$end_date在while循环中进行迭代,以显示五章的结束日期。 Now on the basis of this variable I have to show an alert box. 现在,基于此变量,我必须显示一个警报框。 My problem is when the last chapter's end date is found the alert box pops up which is not valid because for other four chapters the end date is extended. 我的问题是,当找到上一章的结束日期时,会弹出警告框,这是无效的,因为对于其他四个章节,结束日期已延长。 How can I leave end date for the last chapter only and alert box condition should not be matched. 我如何只保留最后一章的结束日期,并且警报框的条件不匹配。 I have tried alot on this but can't make a logic for this. 我对此进行了很多尝试,但无法为此做出逻辑。 Help or advice will be appreciated. 帮助或建议,将不胜感激。

  if((strtotime($today_date) >= strtotime($end_date) || ( $pass_count == 0 && $status_request != 2 && $status_request != 0 ) ) )
  {


 echo ("<SCRIPT LANGUAGE='JavaScript'>
       mess1='Your exam validity date has been expired. Click OK to request for extending your Quiz validity or you can ignore by clicking Cancel.'  

       x = confirm(mess1);
       if (x == true)  
   {
       window.location = 'validity_request_mail.php?userid=$userid ';    

   } 

       </SCRIPT>");

 }

Solution

We may introduce a new variable named $last_date which is for capturing the "greatest" end date of all chapters. 我们可能会引入一个名为$last_date的新变量,用于捕获所有章节的“最大”结束日期。

It seems that the problem is that after a while() loop, the $end_date is set, and it should be the last date. 看来问题是在while()循环之后,设置了$end_date ,它应该是最后一个日期。 However the last (5th iteration) has an $end_date before other $end_date (s) of other chapters, so showing the confirm() box is not correct (since there are some chapters that are not expired yet). 但是最后(第5次迭代)具有$end_date之前其他$end_date其他章节(S),所以显示confirm()对话框是不正确的(因为有一些章节那些尚未到期)。

So this should fix it: 所以这应该解决它:

while (...) { 
    ... 
} 
... 
// snippet in OP's post

To: 至:

$last_date = '';
while (...) { 
    ... 
    if ( strtotime($end_date) > strtotime($last_date) ) { 
        $last_date = $end_date; 
    } 
} 
... 
$end_date = $last_date; // continue with snippet in OP's post

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM