繁体   English   中英

如果没有结果,则PHP foreach循环跳过

[英]PHP foreach loop skip if no results

我很难让一个foreach循环跳过不包含任何值的值。 这是我的代码。 现在,它以14天为间隔循环遍历2016年1月4日的值,并返回该员工在该工资期内的工作天数以及工资和工时。 但是,如果员工没有在该工资期工作,那么我希望用户看不到该时间段的日期范围。 我试过使用continue; break; 但是它只是停留在最初的结果上。

这就是我在看的东西。 通知-

2016年1月18日至1月31日

它什么也没显示。 如何在PHP中摆脱这种情况? 谢谢。

 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div class="container-fluid bone"> <h4>Pay Periods <i class="fa fa-chevron-down"></i></h4> </pre><div class="panel panel-info"><div class="panel-heading ">Feb 1st - Feb 14th, 2016 </div> <div class="panel-body" style="padding:0px;"><ul class="list-group" style="margin-bottom:0px;"><li class="list-group-item"><dt class="pull-right">$200.00</dt><a href="editfishbone.php?view=test5&amp;edit=26 &amp;hours=8.00 &amp;number=5555" id="edit1" data-intro="edit leave button"><dt>Mon, Feb 1, 10:34 AM</dt> </a><small class="text-muted">8 Hours </small><small class="text-muted pull-right">$166.00 net </small></li><ul class="list-group" style="margin-bottom:0px;"><li class="list-group-item"><dt class="pull-right">$200.00</dt><a href="editfishbone.php?view=test5&amp;edit=25 &amp;hours=8.00 &amp;number=222" id="edit1" data-intro="edit leave button"><dt>Sun, Feb 7, 10:29 AM</dt> </a><small class="text-muted">8 Hours </small><small class="text-muted pull-right">$166.00 net </small></li><ul class="list-group" style="margin-bottom:0px;"><li class="list-group-item"><dt class="pull-right">$250.00</dt><a href="editfishbone.php?view=test5&amp;edit=24 &amp;hours=10.00 &amp;number=0655" id="edit1" data-intro="edit leave button"><dt>Mon, Feb 8, 8:15 AM</dt> </a><small class="text-muted">10 Hours </small><small class="text-muted pull-right">$207.50 net </small></li></ul></ul></ul></div> <div class="panel-footer" style="padding-top:0px; padding-bottom:0px; border-bottom: #DDDDDD; border-bottom-width: 10px; border-bottom-style: solid;"><strong class="pull-right"> Total Pay: $539.50 </strong><strong style="margin-bottom:0px;"> Total Hours: 26 </strong></div><div class="panel-heading ">Jan 18th - Jan 31st, 2016 </div> <div class="panel-body" style="padding:0px;"></div> <div class="panel-footer" style="padding-top:0px; padding-bottom:0px; border-bottom: #DDDDDD; border-bottom-width: 10px; border-bottom-style: solid;"><strong class="pull-right"> Total Pay: $0.00 </strong><strong style="margin-bottom:0px;"> Total Hours: 0 </strong></div><div class="panel-heading ">Jan 4th - Jan 17th, 2016 </div> <div class="panel-body" style="padding:0px;"><ul class="list-group" style="margin-bottom:0px;"><li class="list-group-item"><dt class="pull-right">$200.00</dt><a href="editfishbone.php?view=test5&amp;edit=32 &amp;hours=8.00 &amp;number=555" id="edit1" data-intro="edit leave button"><dt>Mon, Jan 11, 9:49 AM</dt> </a><small class="text-muted">8 Hours </small><small class="text-muted pull-right">$166.00 net </small></li></ul></div> <div class="panel-footer" style="padding-top:0px; padding-bottom:0px; border-bottom: #DDDDDD; border-bottom-width: 10px; border-bottom-style: solid;"><strong class="pull-right"> Total Pay: $166.00 </strong><strong style="margin-bottom:0px;"> Total Hours: 8 </strong></div> </div> 

    //php
    $begin = new DateTime('2016-01-04');
    $end = new DateTime();

    $interval = DateInterval::createFromDateString('14 days');
    $period = new DatePeriod($begin, $interval, $end);
    $period = array_reverse(iterator_to_array($period));

    $query = "SELECT hours, number, payRate, start, id FROM fishbone WHERE user='$user' ORDER by start ASC ";
    $result = $con->query($query);
    $num    = $result->num_rows;

while ($row  = $result->fetch_array(MYSQLI_ASSOC)) {
        $rows[] = $row;
        // echo '<pre>',print_r($row),'</pre>';
    }

foreach循环-

//php
foreach ($period as $dt) {
        $i = new DateInterval('P13D');
        $d2 = clone $dt;
        $d2->add($i);
        echo "<div class='panel-heading '>";
        echo $dt->format("M jS\n");
        echo " - ";
        echo $d2->format("M jS, Y\n");
        echo "</div>";
        echo "  <div class='panel-body' style='padding:0px;'>";
        $sum = 0;
        $sum1 = 0;
        foreach ($rows as $row) {
            $startday = date('Y-m-d', strtotime($row['start']));
            $start = date('D, M j, g:i A', strtotime($row['start']));
            $hours = $row['hours'];
            $number = $row['number'];
            $payRate = $row['payRate'];
            $i = new DateInterval('P13D');
            $d2 = clone $dt;
            $d2->add($i);
            $pay = 0;
            $pay1 = 0;

            if ($startday >= $dt->format('Y-m-d') && $startday <= $d2->format('Y-m-d')) {
                $sum += $hours;
                $sum1 += ($hours*$payRate*-.17)+($hours*$payRate);
                $pay += ($hours*$payRate);
                $pay1 += ($hours*$payRate*-.17)+($hours*$payRate);
                echo "<ul class='list-group' style='margin-bottom:0px;'><li class='list-group-item'>";
                echo "<dt class = 'pull-right'>$" . (number_format($pay, 2, '.', '')) . "</dt>";
                echo "<a href='editfishbone.php?view=$view" . "&edit=" . $row['id'] . " " . "&hours=" . $row['hours'] . " " . "&number=" . $row['number'] . "' id= 'edit1' data-intro='edit leave button'><dt>" . $start  . "</dt>  </a>";
                echo "<small class = 'text-muted'>" . (number_format($hours, 2, '.', '')-0) . " Hours </small>";
                echo "<small class = 'text-muted pull-right'>$" . (number_format($pay1, 2, '.', '')) . " net </small>";
                echo "</li>";
            }
        }

        echo "</div>";
        echo "  <div class='panel-footer' style='padding-top:0px; padding-bottom:0px; border-bottom: #DDDDDD; border-bottom-width: 10px; border-bottom-style: solid;'>";
        echo "<strong class='pull-right'> Total Pay: $" . (number_format($sum1, 2, '.', '')) . " </strong>";
        echo "<strong style='margin-bottom:0px;'> Total Hours: " . (number_format($sum, 2, '.', '')-0) . " </strong>";
        echo "</div>";
    }

这是我的查询得到的:

   Array
(
    [hours] => 8.00
    [number] => 555
    [payRate] => 25.00
    [start] => 2016-01-11 09:49:01
    [id] => 32
)
1
Array
(
    [hours] => 8.00
    [number] => 5555
    [payRate] => 25.00
    [start] => 2016-02-01 10:34:07
    [id] => 26
)
1
Array
(
    [hours] => 8.00
    [number] => 222
    [payRate] => 25.00
    [start] => 2016-02-07 10:29:01
    [id] => 25
)
1
Array
(
    [hours] => 10.00
    [number] => 0655
    [payRate] => 25.00
    [start] => 2016-02-08 08:15:02
    [id] => 24
)
1

如果可以测试要跳过的条件,则可以使用'continue'关键字转到for(each)循环的下一个迭代。

例:

foreach($dates as $date) {
    if($date > $date_range) {
        continue; // 'skips', Goes back to the top of the foreach loop
    }

    // ... Do other things for values you didn't skip
}

通常,我会制作一些可怕的SQL查询,对这些行进行分组并返回所需的行,并在其末尾加上句点总和。

不过,作为更简单的解决方法,您应该可以使用下面的代码跳过没有小时的时段。 我用一个简单的数组对其进行了测试,但可能需要(也可能不需要)进行一些调整才能与您的数组一起使用。

$hoursForPeriod = array_reduce($rows, function($carry, $elem) use ($dt, $d2) {
    $startday = date('Y-m-d', strtotime($elem['start']));

    if ($startday >= $dt->format('Y-m-d') && $startday <= $d2->format('Y-m-d')) {
        $carry += $elem['hours'];
    }

    return $carry;
}, 0);

if($hoursForPeriod == 0)
    continue;

尝试将该代码放在脚本中这些行的下方:

$d2 = clone $dt;
$d2->add($i);

暂无
暂无

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

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