总结: 这个作业和作业1有些区别,区别在于数据直接在php内循环,循环后直接以标签形式在html输出,这种输出方式优势在于做网站更便捷,不用过多考虑html内的标签。
<?php
$title = '员工管理系统';
$arr = array(
array(
'id' => 1,
'name' => '韦小宝',
'age' => 20,
'sex' => 0,
'address' => '扬州'
),
array(
'id' => 2,
'name' => '鳌拜',
'age' => 60,
'sex' => 0,
'address' => '北京'
),
array(
'id' => 3,
'name' => '吴三桂',
'age' => 70,
'sex' => 0,
'address' => '云南'
),
array(
'id' => 4,
'name' => '耿精忠',
'age' => 61,
'sex' => 0,
'address' => '广西'
),
array(
'id' => 5,
'name' => '阿珂',
'age' => 18,
'sex' => 1,
'address' => '扬州'
),
);
// 空数组一定要在外层,否则循环后直接变空,只显示最后一条数据。
function table($arr){
$ret = '';
foreach ($arr as $value) {
$ret .= '<tr><td>'.$value['id'].'</td>';
$ret .= '<td>'.$value['name'].'</td>';
$ret .= '<td>'.$value['age'].'</td>';
$ret .= '<td>' . ($value['sex'] ? '女' : '男') . '</td>';
$ret .= '<td>'.$value['address'].'</td></tr>';
}
return $ret;
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title><?php echo $title ?></title>
<style type="text/css">
*{
padding: 0;
margin: 0;
}
table,td,th{
border: 1px solid #999;
}
caption{
border: 1px solid #999;
}
table{
margin:50px auto;
width: 800px;
text-align: center;
height: 32px;
line-height: 32px;
border-collapse: collapse;
}
table + p {
text-align: center;
}
</style>
</head>
<body>
<table>
<caption><?php echo $title ?></caption>
<tr>
<th>ID</th>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
<th>籍贯</th>
</tr>
<!-- php输出的函数需要引入参数 -->
<?php echo table($arr) ?>
</table>
<p>员工总人数:<?php echo count($arr) ?>人</p>
</body>
</html>点击 "运行实例" 按钮查看在线实例
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号