创建表rowspan php

时间:2017-05-25 04:33:33

标签: php tablerow

我需要帮助我的代码..如何我有输出,如下图所示,它将自动插入rowspan ..我当前的代码

$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

当前输出和所需结果

3 个答案:

答案 0 :(得分:1)

请注意:设计执行两个查询只是一个坏主意。还是那样。您需要做的就是在创建块之前执行第二个查询。记录结果数据的计数并将其用作rowSpan。非常简单。

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>";

第二个while循环必须输出

<td>$name</td></tr>

重要的是关闭while循环内的表行,如果要为同一个部门打印第二个名称,也要启动一个新的表行。

答案 1 :(得分:0)

$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>";

答案 2 :(得分:0)

我使用了这种逻辑,并且效果很好。你可以试试看。

#include <stdio.h>
#define x 4
#define y 4

void main(){
    int i=0, k=0;
    int matrix[x][y];

    for (i = 0; i < x; ++i) {
        for (k = 0; k < y; ++k) {
            matrix[i][k] = i ;
        }
    }
     for (i = 0; i < x; ++i) {
        for (k = 0; k < y; ++k) {
            printf("\t%d", matrix[i][k]);
        }
        printf("\n");
    }

}