当内部do-while循环中的条件满足php时,如何进入外部do-while循环?

时间:2018-03-25 07:15:59

标签: php

do // first do-while loop
{
    $sqlSeltime = "SELECT * FROM tbltime WHERE subtype = '$subtype' AND units = '$subunits' AND classtype = 'Lec' AND (time_value = '60' OR time_value = '90') ORDER BY RAND() LIMIT 1";
    $sqlStoreTime = mysqli_query($connect, $sqlSeltime);
    $rowTime = mysqli_fetch_array($sqlStoreTime);
    $time_day = $rowTime["time_day"];
    $day_avail = $rowTime["day_avail"];
    $starting = $rowTime["start_time"];
    $ending = $rowTime["end_time"];

    $sqlSelRoom = "SELECT * FROM tblroom WHERE room_type = 'Lecture' and college = '$college' ORDER BY RAND() LIMIT 1 ";
    $sqlStoreRoom = mysqli_query($connect, $sqlSelRoom);
    $rowRoom = mysqli_fetch_array($sqlStoreRoom);
    $room = $rowRoom["room"];


    if ($day_avail == 'M/F' || $day_avail == 'M/W/F' || $day_avail == 'W')
    {
        do // second do while loop
        {
            $selectinsection = "SELECT * FROM tblschedule WHERE year_level = '$year' AND section = '$section' AND college = '$college' AND ((day_lec = 'M/F' OR day_lab = 'M/F') OR (day_lec = 'M/W/F' OR day_lab = 'M/W/F') OR (day_lec = 'W' OR day_lab = 'W')) ";
            $resultinsection = mysqli_query($connect, $selectinsection);
            while($rowinsection = mysqli_fetch_assoc($resultinsection))
            {
                $startminlecinsection [] = $rowinsection["start_timeminlec"];
                $endminlecinsection [] = $rowinsection["end_timeminlec"];
                $startmajlecinsection [] = $rowinsection["start_timemajlec"];
                $endmajlecinsection [] = $rowinsection["end_timemajlec"];
                $startmajlabinsection [] = $rowinsection["start_timemajlab"];
                $endmajlabinsection [] = $rowinsection["end_timemajlab"];
            }

            $startminlecsize = count($startminlecinsection);
            $endminlecsize = count($endminlecinsection);
            $startmajlabsize = count($startmajlecinsection);
            $endmajlabsize = count($endmajlecinsection);
            $startmajlecsize = count($startmajlabinsection);
            $endmajlecsize = count($endmajlabinsection);

            $startminlecinsection = array_values($startminlecinsection);
            $endminlecinsection = array_values($endminlecinsection);
            $startmajlecinsection = array_values($startmajlecinsection);
            $endmajlecinsection = array_values($endmajlecinsection);
            $startmajlabinsection = array_values($startmajlabinsection);
            $endmajlabinsection = array_values($endmajlabinsection);

            for($ctr = 0; $ctr <= ($startminlecsize - 1); $ctr++)
            {
                if (isBetween($startminlecinsection[$ctr], $endminlecinsection[$ctr], $starting, $ending) == 'FALSE' )
                {
                    $chk = 0;
                    break 1;
                }
                else
                {
                    $chk = 1;
                }
            }

            for($ctr = 0; $ctr <= ($startmajlecsize - 1); $ctr++)
            {
                if (isBetween($startmajlecinsection[$ctr], $endmajlecinsection[$ctr], $starting, $ending) == 'FALSE' )
                {
                    $chk1 = 0;
                    break 1;
                }
                else
                {
                    $chk1 = 1;
                }
            }

            for($ctr = 0; $ctr <= ($startmajlabsize -1); $ctr++)
            {
                if (isBetween($startmajlabinsection[$ctr], $endmajlabinsection[$ctr], $starting, $ending) == 'FALSE' )
                {
                    $chk2 = 0;
                    break 1;
                }
                else
                {
                    $chk2 = 1;
                }
            }

            if($chk == 0 || $chk1 == 0 || $chk2 == 0)break; // if this is true the execution of the program should continue to the first do-while loop
        }
        while($chk == 0 || $chk1 == 0 || $chk2 == 0);
    }
    else
    {
    }
while(condition);

这是我当前项目自动化类调度程序的代码。我有一个函数(); named isBetween - 用于检查计划冲突。我有三个循环检查3个时间表,即次要科目,主要讲座和主要实验室的时间表。当其中一人返回&#39; 0&#39;该过程应该从第一个循环开始,因为第一个while循环之后的代码块是负责生成房间,类和日时间的代码块。现在当发生冲突时,程序应该生成另一个时间,日期和房间。它就在第一个do-while循环之后。

2 个答案:

答案 0 :(得分:0)

您可以使用break;表示退出当前循环。

do // this is the first do-while loop
{
    if(condition)
    {
        do // this is the second do-while loop
        {
            for(condition)
            {
                //do something
            }
            // if the process inside the for loop returns true the execution
            //   of the program should continue to the first do-while loop
            if ( condition == true ) {
                break;
            }
        }
        while(condition);
    }
    else
    {
    }
}
while(condition);

如果您想在满足条件的情况下立即退出for()循环,则可以使用退出2级循环的break 2;

do // this is the first do-while loop
{
    if(condition)
    {
        do // this is the second do-while loop
        {
            for(condition)
            {
                //do something
                if ( condition == true ) {
                    break 2; // Exit to outer loop
                }
            }
        }
        while(condition);
    }
    else
    {
    }
}
while(condition);

答案 1 :(得分:0)

请注意,breakcontinue有不同的陈述。

break用于中断循环并继续脚本,而continue在循环中跳过一轮,但不会中断它。

继续:

for(i = 1; i < 6; i++)
{
 if(i == 3)
 {
  //if i is 3, just do nothing but go to i is 4.
  continue;
 }
 else
 {
  echo $i." Hello World.<br>";
 }
}

//1 Hello World
//2 Hello World
//4 Hello World
//5 Hello World

符:

for(i = 1; i < 6; i++)
{
 if(i == 3)
 {
  //if i is 3, exit the loop
  break;
 }
 else
 {
  echo $i." Hello World.<br>";
 }
}

//1 Hello World
//2 Hello World