Q:

creating table rowspan php

i need help with my code..how do i have output like in the image below where it will automatically insert rowspan ..my current code
$result = $cmsDB->query("SELECT * FROM ".$cmsDB->prefix("departments")."");

echo "<br /><br /><table class='table table-bordered table-striped'>
<tr>
<th>Bil</th>
<th>Department</th>
<th>Staff</th>
</tr>";

$count =1 ;

while($row = $cmsDB->fetchArray($result))
{
$deptid=$row['deptid']; 
$deptname=$row['deptname']; 
 
echo "<tr><td>".$count++."</td><td>".$deptname."</td><td>";
global $cmsDB;
  $result2 = $cmsDB->query("SELECT * FROM ".$cmsDB->prefix("staff")." WHERE deptid=$deptid");
  
while($row = $cmsDB->fetchArray($result2))
{

$name=$row['name']; 
echo "".$name." <br />";

}

}

echo "</td></tr></table>";

current output and desired results

A:

Please Note: By design performing two queries for this just a bad idea. Still going with that. All you need to do is to perform the second query ahead of creating the  block. Record the counts of the result Data and use that as rowSpan. Quite Simple. 
global $myDB;
$result2 = $myDB->query("SELECT * FROM ".$myDB->prefix("staff")." WHERE deptid=$deptid");

$rowSpan=$result2-num_rows;

echo "<tr>"
echo "<td rowspan=" . $rowSpan . ">" . $count++ . "</td>";
echo "<td rowspan=" . $rowSpan . ">".$deptname."</td>";

The second while loop must output 
<td>$name</td></tr>

The important thing is to close the table row  inside the while loop also start a new table row in case you are printing a second name for the same dept.

