如果不包含任何值,我很难获得foreach循环来跳过值。这是我的代码。现在它以14天的间隔循环显示2016-01-04的值,并返回员工在该支付期间的工资和工时的工作日。但是,如果员工没有工作那个支付期,我希望用户无法查看该期间的日期范围。我尝试过使用continue;
和break;
,但它只是在第一个结果处停止。
这就是我所看到的。请注意 -
它没有任何用处。我怎样才能在PHP中摆脱它?谢谢。2016年1月18日至1月31日
<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&edit=26 &hours=8.00 &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&edit=25 &hours=8.00 &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&edit=24 &hours=10.00 &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&edit=32 &hours=8.00 &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>
&#13;
//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
答案 0 :(得分:5)
如果您可以测试要跳过的条件,可以使用'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
}
答案 1 :(得分:1)
通常情况下,我会制作一些可疑的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);