0

Below are lines of my code. I have a foreach loop to print values into a table. It results in an error saying

"Parse error: syntax error, unexpected 'endforeach' (T_ENDFOREACH)"

I can't seem to figure out where I have gone wrong.

<?php foreach (getStaffDetails() as $staffDetails): 
if($staffDetails['admin_confirm'] == 1) 
  {?>
  <tr>                             
  <td><?php echo $staffDetails['name']; ?></td>                               
  <td> <?php echo $staffDetails['staff_id']; ?></td>
  <td> <?php $dept= $staffDetails['dept_id']; 
            $dept_name= getDeptName($dept);
            echo $dept_name;?> </td> 
  <td> <?php echo $staffDetails['email_id']; ?></td>
  <td> <?php echo $staffDetails['ph_no']; ?></td>
  <td> <?php echo $staffDetails['gender']; ?></td>
  <td> <?php echo $staffDetails['doj']; ?></td> 
<?php endforeach; ?>
UditS
  • 1,936
  • 17
  • 37
user100000001
  • 35
  • 1
  • 2
  • 8

2 Answers2

1

You've opened a curly bracket at but did not close it.

if($staffDetails['admin_confirm'] == 1) 
    {?>

You'll need to close it here:

<td> <?php echo $staffDetails['doj']; ?></td> 
<?php } endforeach; ?>
Panda
  • 6,955
  • 6
  • 40
  • 55
0

This is because the if statement is not closed in your code. Therefore, the PHP parser encounters the endforeach keyword before the closing of the if statement

<?php 
  foreach (getStaffDetails() as $staffDetails): 
      if($staffDetails['admin_confirm'] == 1):
?>
<tr>                             
    <td> <?php echo $staffDetails['name']; ?> </td>                               
    <td> <?php echo $staffDetails['staff_id']; ?> </td>
    <td> <?php $dept= $staffDetails['dept_id']; 
               $dept_name= getDeptName($dept);
               echo $dept_name;?> </td> 
    <td> <?php echo $staffDetails['email_id']; ?> </td>
    <td> <?php echo $staffDetails['ph_no']; ?> </td>
    <td> <?php echo $staffDetails['gender']; ?> </td>
    <td> <?php echo $staffDetails['doj']; ?> </td> 
</tr>
<?php 
      endif;
  endforeach; ?>
Birendra Gurung
  • 2,168
  • 2
  • 16
  • 29