PHP在循环中跳过一定次数的东西

时间:2015-03-18 13:12:15

标签: php mysql loops html-table

我有大量的for循环用于生成表格,其结果来自我的MySQL数据库。一些更重要的信息是开始和结束日期。我计算除了开始日期之外这个缺席的天数(例如02.03.2015 - 04.03.2015,2天)。我将此结果保存在$difference中,但是我在for循环中得到了这个结果,所以我不能简单地转到for循环并说出$xyz - difference

所以我想到了在循环中滑动<td>'的次数,差异很多次。

在图片中,您会看到<td>已经超出缺席持续的天数。

enter image description here

我如何跳过它们的次数差异,所以表格看起来又是正确的?

我的代码仍然很乱,所以没有评论:

echo '<table class="table table table-bordered table-striped table-hover">';
echo '<thead>';
for ($l = 0; $l < $days; $l++) {
    if ($l == 0) {
        echo '<th>', '<b>Day</b>', '</th>';
    } else {
        $date = "$current-$month-$l";
        $date = date('D', strtotime($date));
        $date = substr($date, 0, -1);
        echo '<th class="center">', $date,'</th>';
    }
    echo "\n";
}

echo '</thead>';
echo '<tbody>';

for ($l = 0; $l < $days; $l++) {
    if ($l == 0) {
        echo '<td>', '<b>Onshore</b>', '</td>';
    } else {
        echo '<th class="center">', $l, '</th>';
    }
    echo "\n";
}

for ($i = 0; $i < $count_user; $i++) {
    echo '<tr>';        
    $result = mysql_query("select start, end, type_FK, employee_FK FROM absences where employee_FK = {$array_user[$i]['employee_ID']} and MONTH(start) = $month and YEAR(start) = $current");
    while ($row = mysql_fetch_assoc($result)) {
        $array_absences[] = $row;
    }

    $count = 0;
    if (!empty($array_absences)) {
        $count = count($array_absences);
    }

    for ($j = 0; $j < $days; $j++) {
        $true = 0;
        if ($j == 0 && $i == $count_on) {
            echo '<td>';
            echo '<b>Offshore</b>';
            echo '</td>';
            for($k = 0; $k < $days -1; $k++){
                echo '<td>';
                echo '</td>';
            }
            echo '</tr>';
        }

        if ($j == 0) {
            echo '<td>';
            echo $array_user[$i]['name'], ' ', $array_user[$i]['surname'];
            echo '</td>';
        }

        for ($k = 0; $k < $count; $k++) {       
            $array_absences[$k]['start'] = substr($array_absences[$k]['start'], -2);
            $array_absences[$k]['end']   = substr($array_absences[$k]['end'], -2);
            $array_absences[$k]['start'] = ereg_replace("^0", "", $array_absences[$k]['start']);
            $array_absences[$k]['end']   = ereg_replace("^0", "", $array_absences[$k]['end']);      
            $difference = $array_absences[$k]['end'] - $array_absences[$k]['start'];    

            if ($j == $array_absences[$k]['start'] && $array_absences[$k]['employee_FK'] == $array_user[$i]['employee_ID']) {
                $true = 1;
                $result = mysql_query("select approved from absences where DAY(start) = $j and MONTH(start) = $month");
                while ($row = mysql_fetch_assoc($result)) {
                    $approved[] = $row;
                    $n++;
                }

                $now = date('Y-m-d');
                $absence = strtotime("$current/$month/$j");
                $absence = date('Y-m-d',$absence);

                for ($q = 0; $q < $difference+1; $q++) {
                    if ($approved[$n]['approved'] == 1) {
                        echo '<td class="center green">';
                    } elseif ($approved[$n]['approved'] == 0 && $now <= $absence) {
                        echo '<td class="center orange">';
                    } elseif ($approved[$n]['approved'] == 0 && $now > $absence) {
                        echo '<td class="center red">';
                    }

                    for ($l = 0; $l < $count_types; $l++) {

                        if ($array_absences[$k]['type_FK'] == $types[$l]['type_ID']) {
                            echo $types[$l]['short'];
                        }
                    }

                    echo '</td>';
                }
            }

        }       

        //Days that are not absences                    

        //Skip this the amounts of $difference
        echo '<td ';

        //If weekend special coloring
        $date = "$current-$month-$j+1";
        $date = new DateTime($date);
        $day =  $date->format("w");             

        if ($day == 6 || $day == 0) {
            echo 'class = "weekend"';   
        }

        echo '>';
        echo '</td>';
        echo "\n";
    }
    echo '</tr>';
}

1 个答案:

答案 0 :(得分:1)

我担心我没有真正了解你的解释,但是,为了在循环中跳过某些内容,你可以使用 continue 关键字。

例如:

for($i = 0; $i++; i<1000){
    if($i < 200 && $i > 100)
        continue;
    // Computations are here....
}