一、foreach/if替代语法循环遍历
<?php $staffs = [ ['id'=>1,'name'=>'侯亮平','age'=>'30','sex'=>1,'email'=>'hlp@qq.com','password'=>sha1('123456')], ['id'=>2,'name'=>'赵瑞龙','age'=>'40','sex'=>1,'email'=>'zrl@qq.com','password'=>sha1('123456')], ['id'=>3,'name'=>'李达康','age'=>'50','sex'=>1,'email'=>'ldk@qq.com','password'=>sha1('123456')], ['id'=>4,'name'=>'祁同伟','age'=>'45','sex'=>1,'email'=>'qtw@qq.com','password'=>sha1('123456')], ['id'=>5,'name'=>'高小琴','age'=>'30','sex'=>0,'email'=>'gxq@qq.com','password'=>sha1('123456')], ]; $title = '2019年员工信息表'; $total = count($staffs); ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>员工管理系统</title> <style> table,th,td { border: 1px solid #666; padding: 8px; } table { border-collapse: collapse; width: 80%; text-align: center; margin: 30px auto; } thead tr:first-of-type { background-color: lightblue; } tbody tr:hover { background-color: #efefef; } table > caption { font-size: 1.2rem; margin-bottom: 15px; } table + p { text-align: center; } </style> </head> <body> <table> <caption><?php echo $title ?></caption> <thead> <tr> <th>编号</th> <th>姓名</th> <th>年龄</th> <th>性别</th> <th>邮箱</th> <th>密码</th> </tr> </thead> <tbody> <?php $data = ''; foreach($staffs as $staff){ //staff是一个一维数组 $data .= '<tr>'; $data .= '<td>'.$staff['id'].'</td>'; $data .= '<td>'.$staff['name'].'</td>'; $data .= '<td>'.$staff['age'].'</td>'; $data .= '<td>'.$staff['sex'].'</td>'; $data .= '<td>'.$staff['email'].'</td>'; $data .= '<td>'.$staff['password'].'</td>'; $data .= '</tr>'; } //将生成的html代码发送到浏览器中显示 echo $data; ?> <!--foreach替代语法--> <?php foreach ($staffs as $staff): ?> <tr> <td><?=$staff['id']?></td> <td><?=$staff['name']?></td> <td><?=$staff['age']?></td> <td><?php if ($staff['sex'] === 1): ?> 男 <?php else: ?> 女 <?php endif;?> </td> <td><?=$staff['email']?></td> <td><?=$staff['password']?></td> </tr> <?php endforeach; ?> </tbody> </table> <p>总计: <?php echo $total ?>人</p> <!--<p>总计: --><?//=$total?><!--人</p>--> <!--三元运算符 表单式 ? true : false--> <td><?//=$staff['email'] ? '男' : '女' ?></td> </body> </html>
点击 "运行实例" 按钮查看在线实例
总结:
1、foreach是循环遍历数据,实现有规律代码的循环输出。
2、$staff 是一个临时变量。用于临时存储变量。
3、<?php foreach ($staffs as $staff): ?><?php endforeach; ?> 精简代码,更优化。
4、三元运算符 表达式 ? true : false 为true则输出真值,否则输出假值。
二、员工管理系统 后台首页
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>后台管理系统</title> <style> body{ margin:0; padding:0; } .head{ width:100%; height:100px; background-color:lightblue; } .head h2 { width:500px; float:left; margin-left:50px; } .head p{ width:300px; float:right; height:45px; line-height:45px; } .main { width:100%;} .left{ float:left; width:20%; } .left li{ height:45px; line-height:45px; list-style:none; margin:10px; } .left li a{ height:45px; line-height:45px; background-color:#0e90d2; color:#fff; text-decoration:none; display: block; text-align:center; } .left li a:hover{ background-color:yellow; color:black; } .right{ width:80%; float:right; } iframe{ width:100%; min-height: 650px; } </style> </head> <body> <div class="head"><h2>网站后台管理</h2> <p>管理员:admin <a href="">退出</a></p> </div> <div class="main"> <div class="left"><ul> <li><a href="staff_list.php" target="workspace">员工管理</a></li> <li><a href="system.php" target="workspace">系统设置</a></li> <li><a href="user_list.php" target="workspace">用户设置</a></li> </ul> </div> <div class="right"> <iframe src="staff_list.php" frameborder="0" name="workspace"></iframe> </div> </div> </div> </body> </html>
点击 "运行实例" 按钮查看在线实例
总结:
target的值必须要与iframe中name一致。