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 "

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

(image)


(image)


current output and desired results

  
"
answer1
  
  

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 ""  echo "" . $count++ . "";  echo "".$deptname."";  

The second while loop must output


$name  

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.

  
  
answer2
  
  
$result = $cmsDB->query("SELECT * FROM ".$cmsDB->prefix("departments")."");
echo "

          ";
$count =1 ;
while($row = $cmsDB->fetchArray($result))  {  $deptid=$row['deptid'];   $deptname=$row['deptname'];
global $cmsDB;  $result2 = $cmsDB->query("SELECT * FROM ".$cmsDB->prefix("staff")." WHERE deptid=$deptid");
  $num_rows = $cmsDB->getRowsNum($result2);
  $rowSpan=$num_rows;
echo "";  echo "";  echo "";
while($row = $cmsDB->fetchArray($result2))  {
$name=$row['name'];   echo "";  }
}
  echo "
BilDepartmentStaff
" . $count++ . "".$deptname."$name
";