I am having a hard time getting a foreach loop to skip over values if it doesnt contain any values. Here is my code. Right now its looping through values from 2016-01-04 in 14 day intervals and returning the days the employee worked along with pay and hours in that pay period. However, if the employee didnt work that pay period I would like it the user to not be able to see the date range of the period. I've tried using continue; and break; but it just stops at the first results.
Here's what I'm looking at. Notice on-
Jan 18th - Jan 31st, 2016
it displays nothing of use. How can I go about getting rid of that in PHP? Thanks.
<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>
//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 loops--
//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>";
}
heres what my query is getting:
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