实例
运用foreach循环,运用“.=”拼接
<?php $a = [ ['id' => 1,'name' => 'jack','age' =>25,'sex' => 1,'emall' => 'fajf@qq.com','password' => 'dfwe123'], ['id' => 2,'name' => 'rose','age' =>22,'sex' => 0,'emall' => 'rafe@qq.com','password' => '321e123'], ['id' => 3,'name' => 'davee','age' =>30,'sex' => 1,'emall' => 'derm@qq.com','password' => 'df43s23'], ['id' => 4,'name' => 'tosi','age' =>36,'sex' => 1,'emall' => 'htdh@qq.com','password' => 'rsft423'], ['id' => 5,'name' => 'maray','age' =>28,'sex' =>0,'emall' => 'aded@qq.com','password' => '2dgt5e3'] ]; $total = count($a); $title = '员工信息表'; $caption = $title; ?> <!DOCTYPE html> <head> <html lang="en"> <head> <meta charset="UTF-8"> <title><?php echo $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 $caption; ?> </caption> <thead> <tr> <th>编号</th> <th>姓名</th> <th>年龄</th> <th>性别</th> <th>邮箱</th> <th>密码</th> </tr> </thead> <tbody> <?php $stu = ''; foreach ($a as $b){ $stu .= '<tr>'; $stu .= '<td>'.$b['id'].'</td>'; $stu .= '<td>'.$b['name'].'</td>'; $stu .= '<td>'.$b['age'].'</td>'; $stu .= '<td>'.($b['sex'] ? '男' : '女') .'</td>'; $stu .= '<td>'.$b['emall'].'</td>'; $stu .= '<td>'.$b['password'].'</td>'; $stu .= '</tr>'; }; echo $stu; ?> </tbody> </table> <p>总计: <?php echo $total; ?> 人 </p> </body> </html>>
运行实例 »
点击 "运行实例" 按钮查看在线实例