sabahan
sabahan

Reputation: 41

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

Desired result

current output and desired results

Upvotes: 0

Views: 7737

Answers (3)

ranojan
ranojan

Reputation: 837

I used this logic and it worked perfectly. You can try this.

$result = get_pending_sales_order_details(ST_SALESORDER, $_POST['customer_id'],null);

$order_count=0;
$array_ord=array();


while ($myrow = mysql_fetch_array($result)) 
{

 $result23 = get_pending_sales_order_details(ST_SALESORDER, $_POST['customer_id'],$myrow["order_no"]);

        $row_span = mysql_num_rows($result23);

        start_row();
        if($currentorg != $myrow["order_no"])

        {
        label_cell($myrow["order_no"], "rowspan='".$row_span."' align=center");
        }


    label_cell($myrow["stk_code"], "align=center");
    label_cell($myrow["description"], "align=left");
    label_cell($myrow["pending_quantity"], "align=center");

    end_row();
    $currentorg = $myrow["order_no"];
}

Upvotes: 0

sabahan
sabahan

Reputation: 41

$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']; 

 global $cmsDB;
$result2 = $cmsDB->query("SELECT * FROM ".$cmsDB->prefix("staff")." WHERE deptid=$deptid");


$num_rows = $cmsDB->getRowsNum($result2);


$rowSpan=$num_rows;

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

while($row = $cmsDB->fetchArray($result2))
{

$name=$row['name']; 
echo "<td>$name</td></tr>";
}

}


echo "</table>";

Upvotes: 0

Vasan Ramani
Vasan Ramani

Reputation: 302

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.

Upvotes: 1

Related Questions