PHP跳过循环并继续

时间:2012-11-26 18:52:57

标签: php for-loop foreach

enter image description here

如何使用for / foreach循环实现上述结果[图2]?我用谷歌搜索,但找不到我想要的答案......

$name = array('D1','D2','D3' );

foreach ( $name as $k=> $v ) {
  $openTime3 = strtotime('10:00');
  $closeTime3 =  strtotime('15:00');

  echo '<div class="col"> <span class="header">'.$v.'</span>';

  while ( $openTime3 < $closeTime3 ) {
     if ( ( date('Hi', $openTime3) > '1030'  && date('Hi', $openTime3) < '1130' ) && $k == 1) {
        echo '<span class="body "><a href="#" class="unconfirmed">B</a></span>';
        break; /* ???? */
        // try to use continue or break, and it didnt work i above result.
     } else {
        echo '<span class="body"><a href="#" class="available">AV</a></span>';
     }

     $openTime3 = strtotime('+15 minutes', $openTime3);
  }

  echo '</div>';
}

css代码:

.col { text-align: center; width: auto; float: left; }
.col > span { display: block; }
.col > span > a {height: 26px;line-height: 26px;display: block; padding: 0 10px; }
.col > span > .available { background: #D6F2F4; }

以上代码总是返回结果[图1]:

enter image description here

2 个答案:

答案 0 :(得分:1)

如果您使用休息时间,它将在10:45停止处理,并且不会在11 - 11:30打印任何内容。

D Time Result
0 14:45 AV
1 10:00 AV
1 10:15 AV
1 10:30 AV
1 10:45 B  <= Breaks Here
2 10:00 AV

使用continue而不是break你需要确保在发出continue之前调整时间,否则你将得到一个永远不会结束循环的致命错误。

while ( $openTime3 < $closeTime3 ) {
 if ( ( date('Hi', $openTime3) > '1030'  && date('Hi', $openTime3) < '1130' ) && $k == 1) {
    echo '<span class="body "><a href="#" class="unconfirmed">B</a></span>';
    $openTime3 = strtotime('+15 minutes', $openTime3);
    continue;
 } else {
    echo '<span class="body"><a href="#" class="available">AV</a></span>';
 }

   $openTime3 = strtotime('+15 minutes', $openTime3);
}

但是这不会产生你想要的结果,因为它将是3个单独的B块而不是高度增加的1个B块;

1 1000 AV
1 1015 AV
1 1030 AV
1 1045 B
1 1100 B
1 1115 B
1 1130 AV
1 1145 AV
etc..

如果您确实需要b块的单个span元素,则需要计算所需的数量,并根据所需的数量在css中相应地调整高度

答案 1 :(得分:1)

我为你的css添加了未经证实的样式,边框颜色与背景相同。使用标记切换 B

<style>
.col { text-align: center; width: auto; float: left; }
.col > span { display: block; }
.col > span > a {height: 26px;line-height: 26px;display: block; padding: 0 10px; }
.col > span > .available { background: #D6F2F4;border-style:solid;
border-width:1px;}
.col > span > .unconfirmed { background: #ffff99;border-style:solid;
border-width:1px;border-color: #ffff99;}

</style>
<?php
$name = array('D1','D2','D3' );
$flag=0;//Set flag off
foreach ( $name as $k=> $v ) {
  $openTime3 = strtotime('10:00');
  $closeTime3 =  strtotime('15:00');

  echo '<div class="col"> <span class="header">'.$v.'</span>';

  while ( $openTime3 < $closeTime3 ) {

     if ( ( date('Hi', $openTime3) > '1030'  && date('Hi', $openTime3) < '1130' ) && $k == 1) {

        if($flag==0){
        echo '<span class="body "><a href="#" class="unconfirmed"></a></span>';//Hide B
        $flag=1;Toggle flag on
        }else{
        echo '<span class="body "><a href="#" class="unconfirmed">B</a></span>';Show B
        $flag=0;Toggle flag off
        }

     } else {
        echo '<span class="body"><a href="#" class="available">AV</a></span>';
     }
     //$flag=1;
     $openTime3 = strtotime('+15 minutes', $openTime3);
  }
  echo '</div>';
}
?>

enter image description here