Log in

View Full Version : foreach() inside while()



crobinson42
01-28-2012, 02:13 AM
Hello everyone1 I'm having a problem and I assume it's because I can't figure out a more clever way to write this segment of code.



//make array of shifts between dates
$query1="SELECT * FROM schedule_shift WHERE post='$post' AND start_date>='$start' AND start_date<='$end'";
$result1=mysqli_query($dbc, $query1);
if(mysqli_num_rows($result1) == '0'){ $shifts[]='1'; }
while($shift=mysqli_fetch_assoc($result1)){ $shifts[]=$shift; }

echo '<tr><td>'.$row['name'].'</td>';

while(date('Y-m-d',$sd) <= date('Y-m-d',$ed)){
$d=date('Y-m-d',$sd);
echo '<td>'.date('Y-m-d',$sd).'<hr>';

foreach($shifts as $shift){
if($shift['start_date'] == $d){
echo $shift['officer_name'].'<br>';
echo $shift['start'].'-'.$shift['end'].'<br>';
}
echo '</td>';
}

$sd= strtotime("+1 Day", $sd);
}

Any help on getting this to work or a more efficient way of writing it?