PHP不规则多级标题

时间:2018-11-12 06:11:37

标签: php html arrays loops

我是Web编程的新手,我需要您的帮助来解决我的问题。 我试图搜索Stackoverflow中的每个问题,但没有遇到与我的情况类似的事情。 这是关于使用PHP在html中创建不规则的标头。

我遇到了有关使用PHP创建不规则的多级标题的问题。 我自己尝试了一些技巧,但仍然无法解决。

我的PHP数组:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<?php 
$multi_header = array(
                  array('id'=>0,
						'name'=>'Working',
						'level_1'=>'Economically Active',
						'level_3'=>NULL,
						'group'=>'Employment'
						),
                  array('id'=>1,
						'name'=>'Ever Worked',
						'level_1'=>'Unemployment',
						'level_3'=>'Economically Active',
						'group'=>'Employment'
						),
                  array('id'=>2,
						'name'=>'Never Worked',
						'level_1'=>'Unemployment',
						'level_3'=>'Economically Active',
						'group'=>'Employment'
						),
                  array('id'=>3,
						'name'=>'Total',
						'level_1'=>'Unemployment',
						'level_3'=>'Economically Active',
						'group'=>'Employment'
						),
                  array('id'=>4,
						'name'=>'Total of Economically Active',
						'level_1'=>'Economically Active',
						'level_3'=>NULL,
						'group'=>'Employment'
						),
                  array('id'=>5,
						'name'=>'Percentage of Working to Economically Active',
						'level_1'=>NULL,
						'level_3'=>NULL,
						'group'=>'Employment'
						),
                  array('id'=>6,
						'name'=>'Attending School',
						'level_1'=>'Not Economically Active',
						'level_3'=>NULL,
						'group'=>'Employment'
						),
                  array('id'=>7,
						'name'=>'Housekeeping',
						'level_1'=>'Not Economically Active',
						'level_3'=>NULL,
						'group'=>'Employment'
						),
                  array('id'=>8,
						'name'=>'Others',
						'level_1'=>'Not Economically Active',
						'level_3'=>NULL,
						'group'=>'Employment'
						),
                  array('id'=>9,
						'name'=>'Total of Not Economically Active',
						'level_1'=>'Not Economically Active',
						'level_3'=>NULL,
						'group'=>'Employment'
						),
                  array('id'=>10,
						'name'=>'Total',
						'level_1'=>NULL,
						'level_3'=>NULL,
						'group'=>'Employment'
						),
                  array('id'=>11,
						'name'=>'Percentage of Economically Active to Working Age Population',
						'level_1'=>NULL,
						'level_3'=>NULL,
						'group'=>'Employment'
						)
                );

?>

我需要像这样创建表头 My Target

我希望他们看起来像这样:

<style>
table {
    font-family: arial, sans-serif;
    border-collapse: collapse;
    width: 100%;
}

td, th {
    border: 1px solid #dddddd;
    text-align: left;
    padding: 8px;
}


</style>
<table>
	<thead>
		<tr>
			<td rowspan='3'>Scope/Coverage</td>
			<td colspan='5'>Economically Active</td>
			<td rowspan='3'>Percentage of Working to Economically Active</td>
			<td colspan='4'>Not Economically Active</td>
			<td rowspan='3'>Total</td>
			<td rowspan='3'>Percentage of Economically Active to Working Age Population</td>
		</tr>
		<tr>
			<td rowspan='2'>Working</td>
			<td colspan='3'>Unemployment</td>
			<td rowspan='2'>Total of Economically Active</td>
			<td rowspan='2'>Attending School</td>
			<td rowspan='2'>Housekeeping</td>
			<td rowspan='2'>Total of Not Economically Active</td>
			
		</tr>
		<tr>
			<td>Ever Worked</td>
			<td>Never Worked</td>
			<td>Total</td>
		</tr>
	</thead>
	
</table>

我该怎么办? 我要看起来像那样 预先感谢

1 个答案:

答案 0 :(得分:1)

欢迎。因此,有许多方法可以实现。首先,请记住,最好只将尝试表达为HTML,但是如果必须使用PHP,这是一种可能的方法。

$tableHeader = '[
    {
        "title": "Scope/Coverage",
        "type":  "rowspan",
        "span":  3
    },
    {
        "title": "Economically Active",
        "type":  "colspan",
        "span":  3
    }
]';

foreach (json_decode($tableHeader) as $row) {
  echo "<tr {$row->type}=\"{$row->span}\">{$row->title}</tr>\r\n";
}

您当然需要添加其余的表标题,但是您会得到图片。您可以将表的JSON单独存储在某些配置中,以备日后需要修改时使用。