header.php//站点头部,包括数据与导航,数据较多,此处仅列出一行
<?php $staffs = [ [ 'id'=>1, 'name'=>'张三', 'department'=>1, 'image'=>'1.jpg', 'detail'=>'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' ], 。。。(略) $departments = [ ['dep_id'=>1,'name'=>'hr','alias'=>'人力资源部'], ['dep_id'=>2,'name'=>'fin','alias'=>'财务部'], ['dep_id'=>3,'name'=>'market','alias'=>'***部'], ]; ?> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <link rel="stylesheet" href="public/static/css/style.css"> <title>员工信息</title> </head> <body> <!-- 头部导航 --> <div class="header">' <ul class="nav"> <li><a href="index.php">全部信息 </a></li> <?php foreach($departments as $department) : ?> <li><a href="list.php?dep_id=<?php echo $department['dep_id']; ?>"><?php echo $department['alias']; ?></a></li> <?php endforeach; ?> </ul> </div>
index.php//首页(全员信息列表)
<?php include __DIR__.'/public/include/header.php'; foreach($departments as $department){ echo "<h2>{$department['alias']}</h2>"; echo '<ol>'; foreach ($staffs as $staff) { if($department['dep_id']==$staff['department']){ echo "<li><a href='detail.php?id=".$staff['id']."'>{$staff['name']}</a></li>"; } }; echo '</ol>'; }; include __DIR__.'/public/include/footer.php'; ?>
list.php//部门员工信息列表模板
<?php include __DIR__.'/public/include/header.php'; $dep_id = $_GET['dep_id']; foreach($departments as $department){ if($department['dep_id']==$dep_id){ echo "<h2>{$department['alias']}</h2>"; echo '<ol>'; foreach ($staffs as $staff) { if($department['dep_id']==$staff['department']){ echo "<li><a href='detail.php?id=".$staff['id']."'>{$staff['name']}</a></li>"; } } echo '</ol>'; }; } include __DIR__.'/public/include/footer.php'; ?>
detail.php//员工详情页模板
<?php include __DIR__.'/public/include/header.php'; $id = $_GET['id']; foreach ($staffs as $staff) { if($staff['id']==$id){ echo "<h3>{$staff['name']}</h3>"; echo '<img src="public/static/images/'.$staff['image'].'" width="300px">'; echo "<p>{$staff['detail']}</p>"; } } include __DIR__.'/public/include/footer.php'; ?>
--------------
效果图
---------------
首页(全部信息)
部门员工列表页
员工信息详情页
思路:
1、主要是要在a链接中设置好跳转地址和参数,跳转到模板页面时,以$_GET获取传入的参数, 再运用判断语句+循环,匹配满足条件的数据。
2、要做好重复代码部分的分离,实现代码复用