求这套html嵌套表格代码
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了求这套html嵌套表格代码相关的知识,希望对你有一定的参考价值。
我不能好好完成,做晕了都没弄好····
求你们今天解决吧···大神拜托
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>table</title>
<style>
.c1background-color:pink;
.c2background-color:orange;
.tabborder:solid 1px red; width:650;
</style>
</head>
<body>
<table class="tab">
<tr>
<td class="c1">现代商务应用技术系</td>
<td rowspan="3"><img src="../images/happy.png"/></td>
</tr>
<tr>
<td class="c2">在校班级</td>
</tr>
<tr>
<td>ZAI XIAO BAN JIE</td>
</tr>
<tr>
<td colspan="2">
<table width="100%">
<tr class="c2">
<td>首页</td><td>专业介绍 </td><td>学科建设 </td>
<td>师资设备</td><td>教育教学</td><td>毕业生</td>
<td>学生工作</td><td>留言簿</td>
</tr>
</table>
</td>
</tr>
<tr>
<td colspan="2">
<hr align="center" width="400" color="orange">
</td>
</tr>
<tr>
<td colspan="2" style="fontcolor:orange">全体师生欢迎您!</td>
</tr>
<tr>
<td colspan="2">
<table border="0" width="100%">
<tr>
<td width="20"> </td>
<td class="c2" width="30"> </td>
<td class="c1">
<table width="460" border="0">
<tr><td align="center"><strong>一班简介</strong></td></tr>
<tr><td><textarea class="c1" cols="52" rows="16"></textarea></td></tr>
</table>
</td>
<td class="c2" width="30"> </td>
<td width="20"> </td>
</tr>
</table>
</td>
</tr>
<tr>
<td align="center" colspan="2"><a href="#">返回</a></td>
</tr>
<tr>
<td colspan="2">
<hr align="center" width="400" color="orange">
</td>
</tr>
<tr>
<td align="center" colspan="2">
请使用800X600分辨率和IE5以上浏览本站<br/>
@四川商务职业学校现代商务应用技术系版权所有
</td>
</tr>
</table>
</body>
</html> 参考技术A 用div+css 更简单。追问
能把HTML代码写下来么?
在 HTML 表格中显示嵌套数组
【中文标题】在 HTML 表格中显示嵌套数组【英文标题】:Displaying a nested array in an HTML table 【发布时间】:2015-07-12 12:56:24 【问题描述】:这是一个简单的问题,我已经尝试解决了几个小时。我有一个包含几个学生的信息和他们在测试中得分的数组。一共8个科目,每个科目有5个参数。
数组如下所示:
Array
(
[Ron] => Array
(
[subject1] => Array
(
[test1] => 47
[test2] => 86
[total] => 133
[percentage] => 88.67
[status] => Pass
)
[pass_count] => 8
[fail_count] => 0
[gross_total] => 963
[gross_percentage] => 80.25
[...]
[subject8] => Array
(
[test1] => 48
[test2] => 86
[total] => 134
[percentage] => 89.33
[status] => Pass
)
[pass_count] => 8
[fail_count] => 0
[gross_total] => 900
[gross_percentage] => 75.50
)
[John] => Array
(
[subject1] => Array
(
[test1] => 39
[test2] => 72
[total] => 111
[percentage] => 74
[status] => Pass
)
[pass_count] => 8
[fail_count] => 0
[gross_total] => 963
[gross_percentage] => 80.25
[...]
[subject8] => Array
(
[test1] => 39
[test2] => 75
[total] => 114
[percentage] => 76
[status] => Pass
)
[pass_count] => 8
[fail_count] => 0
[gross_total] => 846
[gross_percentage] => 70.5
)
)
我需要在格式良好的 HTML 表格中显示以下内容(我打算使用 Bootstrap),但我一生都无法弄清楚如何正确嵌套表格使用 PHP强>.
这是我目前拥有的 HTML 标记:http://jsfiddle.net/9y910uvp/
这给出了以下结果:
没关系。
实际问题
我正在使用 PHP 来显示数组中的内容。我很困惑如何显示嵌套的 <tr>
和 <td>
部分的值。
如何循环遍历数组并正确显示内容? (到目前为止,我会发布我的尝试,但它们都很愚蠢并且不起作用,所以我没有发布)。
非常感谢一个例子,因为我花了无数个小时试图让它工作,但失败了。
【问题讨论】:
...您确实应该发布最有效的内容。它的帮助是不可估量的。如果它们根本没有起作用,请说明这一点,因为即使这也是有用的信息 @QPaysTaxes:它们都不起作用。正如我在问题中所说。如果这看起来像是一个家庭作业问题,我很抱歉,但老实说,我自己尝试了这么多……但失败了。 @AlliterativeAlice:这真的是嵌套表吗?我不确定了。 @JosephJohn 就算是作业也没关系。你已经非常清楚地展示了至少一些努力,而不是说,'听我的代码它不起作用请修复 [code dump]'(这是我曾经看过的实际帖子 sob)。我将深入研究我掌握的一些 PHP 知识,看看我是否无法完成这项工作。 @JosephJohn 发布您尝试过的内容,否则,我们不会帮助您,而是为您完成工作 【参考方案1】:输出不完全相同,但这是一种递归方法,可以处理任何深度的嵌套数组...
<?php
$data = Array (
"Ron" => Array (
"subject1" => Array (
"tests" => Array (
"test1" => 47,
"test2" => 86,
"total" => 133,
"percentage" => 88.67,
"status" => "Pass",
),
"pass_count" => 8,
"fail_count" => 0,
"gross_total" => 963,
"gross_percentage" => 80.25,
),
"subject8" => Array (
"tests" => Array (
"test1" => 48,
"test2" => 86,
"total" => 134,
"percentage" => 89.33,
"status" => "Pass",
),
"pass_count" => 8,
"fail_count" => 0,
"gross_total" => 900,
"gross_percentage" => 75.50,
),
),
"John" => Array (
"subject1" => Array (
"tests" => Array (
"test1" => 39,
"test2" => 72,
"total" => 111,
"percentage" => 74,
"status" => "Pass",
),
"pass_count" => 8,
"fail_count" => 0,
"gross_total" => 963,
"gross_percentage" => 80.25,
),
"subject8" => Array (
"tests" => Array (
"test1" => 39,
"test2" => 75,
"total" => 114,
"percentage" => 76,
"status" => "Pass",
),
"pass_count" => 8,
"fail_count" => 0,
"gross_total" => 846,
"gross_percentage" => 70.5,
),
),
);
print_r($data);
$table = table_cell($data);
echo $table;
function table_cell($data)
$return = "<table border='1'>";
foreach ($data as $key => $value)
$return .= "<tr><td>$key</td><td>";
if (is_array($value))
$return .= table_cell($value);
else
$return .= $value;
$return .= "</td><tr>";
$return .= "</tr></table>";
return($return);
而且这张桌子看起来一点也不像要求的,但是...这是一个有趣的 excersize...
【讨论】:
对于提出这个答案的人,谢谢!我不记得我用这段代码回答了哪个问题,我需要它:-)【参考方案2】:试试这个
<table border="1">
<thead>
<thead>
<tr>
<th>Name</th>
<th>Subject</th>
<th>Test1 Marks</th>
<th>Test2 Marks</th>
<th>Total Marks</th>
<th>Status</th>
<th>Percentage</th>
<th>Pass Count</th>
<th>Total Percentage</th>
</tr>
</thead>
<tbody>
</tr>
<?php
$numberOfSubjects = 3; //I used 3 subjects. You should change this to 8 for your data.
foreach ($data as $student => $info)
echo "<tr><td rowspan=$numberOfSubjects />$student</td>";
//flag to let the inner loop the tr has been drawn for the first row
$firstRow = true;
foreach ($info as $key => $value)
//we only want subject info
if (strpos($key, "subject") === 0)
if (!$firstRow)
echo "<tr>";
//else we already drew it
//its a subject so
echo "<td>$key</td>";
echo "<td>$value['test1']</td>";
echo "<td>$value['test2']</td>";
echo "<td>$value['total']</td>";
echo "<td>$value['status']</td>";
echo "<td>$value['percentage']</td>";
//only draw total for the first row
if ($firstRow)
echo "<td rowspan=$numberOfSubjects>$info['pass_count']</td>";
echo "<td rowspan=$numberOfSubjects>$info['gross_percentage']</td>";
//close the row
echo "</tr>";
$firstRow = false;
?>
</tbody>
</table>
这是输出:
它基于我根据您的描述构建的示例数据集,为了完整起见,包含在下面:
<?php
$data = array(
"Ron" => Array
(
"subject1" => Array
(
"test1" => 47
, "test2" => 86
, "total" => 133
, "percentage" => 88.67
, "status" => Pass
)
, "pass_count" => 8
, "fail_count" => 0
, "gross_total" => 963
, "gross_percentage" => 80.25,
"subject2" => Array
(
"test1" => 47
, "test2" => 86
, "total" => 133
, "percentage" => 88.67
, "status" => Pass
)
, "subject3" => Array
(
"test1" => 48
, "test2" => 86
, "total" => 134
, "percentage" => 89.33
, "status" => Pass
)
, "pass_count" => 8
, "fail_count" => 0
, "gross_total" => 900
, "gross_percentage" => 75.50
),
"John" => Array
(
"subject1" => Array
(
"test1" => 47
, "test2" => 86
, "total" => 133
, "percentage" => 88.67
, "status" => Pass
)
, "pass_count" => 8
, "fail_count" => 0
, "gross_total" => 963
, "gross_percentage" => 80.25,
"subject2" => Array
(
"test1" => 47
, "test2" => 86
, "total" => 133
, "percentage" => 88.67
, "status" => Pass
)
, "subject3" => Array
(
"test1" => 48
, "test2" => 86
, "total" => 134
, "percentage" => 89.33
, "status" => Pass
)
, "pass_count" => 8
, "fail_count" => 0
, "gross_total" => 963
, "gross_percentage" => 80.25
)
);
【讨论】:
【参考方案3】:从你的数组来看,这可能是你正在寻找的东西:
<table border="1">
<thead>
<tr>
<th>Name</th>
<th>Subject</th>
<th>Test1 Marks</th>
<th>Test2 Marks</th>
<th>Total Marks</th>
<th>Status</th>
<th>Percentage</th>
<th>Pass Count</th>
<th>Total Percentage</th>
</tr>
</thead>
<tbody>
<?php foreach($arr as $name => $subjects): ?>
<?php $i = 0; ?>
<?php foreach($subjects as $subjectName => $subject): ?>
<?php if (is_array($subject)): ?>
<tr>
<?php if ($i === 0): ?>
<td rowspan="8"><?php echo $name; ?></td>
<?php endif; ?>
<td><?php echo $subjectName; ?></td>
<td><?php echo $subject['test1']; ?></td>
<td><?php echo $subject['test2']; ?></td>
<td><?php echo $subject['total']; ?></td>
<td><?php echo $subject['status']; ?></td>
<td><?php echo $subject['percentage']; ?></td>
<?php if ($i === 0): ?>
<td rowspan="8"><?php echo $subjects['pass_count']; ?></td>
<td rowspan="8"><?php echo $subjects['gross_percentage']; ?></td>
<?php endif; ?>
</tr>
<?php endif; ?>
<?php $i++; ?>
<?php endforeach; ?>
<?php endforeach; ?>
</tbody>
</table>
【讨论】:
以上是关于求这套html嵌套表格代码的主要内容,如果未能解决你的问题,请参考以下文章