1 遇到表格怎么通过php语言一次性灌入,而不是手敲进去,灵活运用数组遍历,以及字符串的拼接; 问题:为什么第三种方式判断了性别之后,前边的方式的性别也都变成了1和0? 我理解的是这个php的工作原理有关系,先提交到服务器,服务器解析完再返回给网页,请老师解惑,谢谢!
实例
<?php $staffs = [ ['id'=>1, 'name'=>'候亮平', 'age'=>30, 'sex'=>1, 'email'=>'hlp@php.cn', 'password'=>sha1('123456')], ['id'=>2, 'name'=>'赵瑞龙', 'age'=>40, 'sex'=>1, 'email'=>'zrl@php.cn', 'password'=>sha1('123456')], ['id'=>3, 'name'=>'李达康', 'age'=>50, 'sex'=>1, 'email'=>'ldk@php.cn', 'password'=>sha1('123456')], ['id'=>4, 'name'=>'祁同伟', 'age'=>45, 'sex'=>1, 'email'=>'qtw@php.cn', 'password'=>sha1('123456')], ['id'=>5, 'name'=>'高小琴', 'age'=>30, 'sex'=>0, 'email'=>'gxq@php.cn', 'password'=>sha1('123456')], ]; // 员工数量 $total = count($staffs); // 标题 $title = '用户信息表'; // 表格标题 $tableTitle = $title; ?> <!DOCTYPE html> <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 $title ?></caption> <thead> <tr> <th>编号</th> <th>姓名</th> <th>年龄</th> <th>性别</th> <th>邮箱</th> <th>密码</th> </tr> </thead> <tbody> <!-- 表格灌内容方法1 --> <?php foreach ($staffs as $staff) { echo "<tr>"; echo "<td>".$staff['id']."</td>"; echo "<td>".$staff['name']."</td>"; echo "<td>".$staff['age']."</td>"; echo "<td>".$staff['sex']."</td>"; echo "<td>".$staff['email']."</td>"; echo "<td>".$staff['password']."</td>"; echo "</tr>"; } ?> <!-- 表格灌内容方法2 --> <?php $data=" "; foreach ($staffs as $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>"; } echo $data; ?> <!-- 表格灌内容方法3 --> <?php function creatTable($staffs){ $data=""; foreach ($staffs as $staff) { $data .= "<tr>"; $data .="<td>{$staff['id']}</td>"; $data .="<td>{$staff['name']}</td>"; $data .="<td>{$staff['age']}</td>"; $data .="<td>".($staff['sex']?'1':'0')."</td>"; $data .="<td>{$staff['email']}</td>"; $data .="<td>{$staff['password']}</td>"; //双引号能够解析里边的的内容 $data .= "</tr>"; } return $data; } echo creatTable($staffs); ?> </tbody> </table> <p>总计: <?php echo $total; ?>人</p> </body> </html>
运行实例 »
点击 "运行实例" 按钮查看在线实例