Q:

Group table rows in a PHP where clause

I have this SQL query:
<?PHP
$Select = SQLSRV_QUERY($conn,"select Unit, WorkDate FROM TABLE GROUP BY WorkDate, Unit");

    ECHO "<table border='1'width='600'>";
    ECHO "<tr>";

        ECHO "<TD><CENTER>";
            ECHO "Unit";
        ECHO "</TD></CENTER>";

        ECHO "<TD><CENTER>";
            ECHO "WorkDate";
        ECHO "</TD></CENTER>";

    ECHO "</tr>";

    WHILE ($Fetch=SQLSRV_FETCH_ARRAY($Select, SQLSRV_FETCH_ASSOC))
    {
    ECHO "<tr>";
        ECHO "<TD><CENTER>";
            ECHO $Fetch['Unit'];
        ECHO "</TD></CENTER>";

        ECHO "<TD><CENTER>";
            ECHO date_format($Fetch['WorkDate'], 'm/d/Y');
        ECHO "</TD></CENTER>";

    ECHO "</tr>";
    }//end WHILE

    ECHO "</table>";
    ?>

This outputs a table as such:

<table border='1'width='600'>
<tr>

 <TD><CENTER>
  Unit
 </TD></CENTER>

 <TD><CENTER>
  WorkDate
 </TD></CENTER>
</tr>

<tr>
 <TD><CENTER>
  Unit1
 </TD></CENTER>

 <TD><CENTER>
  06/01/2016
 </TD></CENTER>
</tr>

<tr>
 <TD><CENTER>
  Unit2
 </TD></CENTER>

 <TD><CENTER>
  06/02/2016
 </TD></CENTER>
</tr>

<tr>
 <TD><CENTER>
  Unit3
 </TD></CENTER>

 <TD><CENTER>
  06/03/2016
 </TD></CENTER>
</tr>

<tr>
 <TD><CENTER>
  Unit4
 </TD></CENTER>

 <TD><CENTER>
  06/03/2016
 </TD></CENTER>
</tr>
</table>

I'm looking to Output the array into separate tables, based on WorkDate so it looks like this:

    <table border='1'width='600'>
    <tr>

     <TD><CENTER>
      Unit
     </TD></CENTER>

     <TD><CENTER>
      WorkDate
     </TD></CENTER>
    </tr>

    <tr>
     <TD><CENTER>
      Unit1
     </TD></CENTER>

     <TD><CENTER>
      06/01/2016
     </TD></CENTER>
    </tr>
</table>
<BR>
<table border='1'width='600'>
  
      <tr>

     <TD><CENTER>
      Unit
     </TD></CENTER>

     <TD><CENTER>
      WorkDate
     </TD></CENTER>
    </tr>
  
    <tr>
     <TD><CENTER>
      Unit2
     </TD></CENTER>

     <TD><CENTER>
      06/02/2016
     </TD></CENTER>
    </tr>
</table>
<BR>
<table border='1'width='600'>
  
      <tr>

     <TD><CENTER>
      Unit
     </TD></CENTER>

     <TD><CENTER>
      WorkDate
     </TD></CENTER>
    </tr>
  
    <tr>
     <TD><CENTER>
      Unit3
     </TD></CENTER>

     <TD><CENTER>
      06/03/2016
     </TD></CENTER>
    </tr>

    <tr>
     <TD><CENTER>
      Unit4
     </TD></CENTER>

     <TD><CENTER>
      06/03/2016
     </TD></CENTER>
    </tr>
    </table>

I know I need to do a grouping, but I don't know how to put that in a while statement

A:

Try this:
if ($Fetch['WorkDate']!=$lastdate){
    ECHO "</tr>";
    ECHO "</table>";
    ECHO "<BR/>";
    ECHO "<table border='1'width='600'>";
    ECHO "<tr>";
}

$lastdate = $Fetch['WorkDate'];

