博客列表 >07-22作业:数组PHP替代foreach的应用

07-22作业:数组PHP替代foreach的应用

子傅
子傅原创
2019年07月23日 00:10:561016浏览

作业一:

创建一个数组,使用foreach与它的替代语法,在html中输出数组内容;

11.png

作业二:

创建一个表单, 演示get和post的数据处理过程,通过选项卡以GET方式传输数据到本页,以PHP的foreach循环查找符合

条件的数组值,并显示出来。

22.png


实例

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>PHP基础foreach的应用</title>
    <style>
        .box1{
                     width: 650px;
                     margin:100px auto;
                     position: relative;
                 }
        .box2{
            width: 650px;
            margin:100px auto;
            position: relative;
        }
        label{

        }
        select{
            width: 120px;
            height: 25px;
            margin-bottom: 5px;
        }
        table{
            width: 600px;
            border: 1px solid #444444;
            text-align: center;
            border-collapse: collapse;
        }
        thead tr{
            background-color: lightgreen;
        }
        thead th:nth-child(1){
            width: 70px;
        }
        thead th:nth-child(2){
            width: 80px;
        }
        thead th:nth-child(3){
            width: 50px;
        }
        thead th:nth-child(4){
            width: 50px;
        }
        thead th:nth-child(5){
            width: 80px;
        }
        thead th:nth-child(6){
            width: 100px;
        }
        thead th:nth-child(7){
            width: 170px;
        }

        tbody td{
            border: 1px solid #444444;
        }
    </style>
</head>
<?php
$userInfoArr =[
    ["id"=>"001","name"=>"道无涯","sex"=>"1","age"=>"99","address"=>"天池","loginId"=>"20190718001","work"=>"隐者","tel"=>"1380000001","email"=>"wuyan@php.cn","birthday"=>"1901-05-08"],
    ["id"=>"002","name"=>"武痴","sex"=>"1","age"=>"38","address"=>"天山","loginId"=>"20190718002","work"=>"打手","tel"=>"1380000002","email"=>"wuchi@php.cn","birthday"=>"1980-09-09"],
    ["id"=>"003","name"=>"妖姬","sex"=>"0","age"=>"22","address"=>"苗疆","loginId"=>"20190718003","work"=>"法师","tel"=>"1380000003","email"=>"yaoji@php.cn","birthday"=>"1990-11-07"],
    ["id"=>"004","name"=>"赤炼","sex"=>"0","age"=>"14","address"=>"峨眉","loginId"=>"20190718004","work"=>"歌姬","tel"=>"1380000004","email"=>"chilian@php.cn","birthday"=>"2003-06-30"]
];


?>
<body>
<div class="box1">
    <form action="#" method="get">
    <label for="name">查询人物信息:</label>
    <select name="name" id="name">
    <option value="001" selected>道无涯</option>
    <option value="002">武痴</option>
    <option value="003">妖姬</option>
    <option value="004">赤炼</option>
</select>
     <button type="submit">确定查询</button>
    </form>
<table>
    <thead>
      <tr>
        <th>用户ID</th>
        <th>姓名</th>
        <th>年龄</th>
        <th>性别</th>
        <th>籍贯</th>
        <th>角色</th>
        <th>电话</th>
      </tr>
    </thead>
    <tbody>
    <?php
    $id = $_GET['name'];

    foreach($userInfoArr as $key=>$val){
        if($id == $val['id']){
    ?>
    <tr>
        <td><?php echo $val['id']?></td>
        <td><?php echo $val['name']?></td>
        <td><?php echo $val['age']?></td>
        <td><?php echo $val['sex']?></td>
        <td><?php echo $val['address']?></td>
        <td><?php echo $val['work']?></td>
        <td><?php echo $val['tel']?></td>
    </tr>
    <?php }}?>
    </tbody>
    <tfoot></tfoot>
</table>
</div>

<div class="box2">
    <table>
        <thead>
        <tr>
            <th>用户ID</th>
            <th>姓名</th>
            <th>年龄</th>
            <th>性别</th>
            <th>籍贯</th>
            <th>角色</th>
            <th>电话</th>
        </tr>
        </thead>
        <tbody>
        <?php foreach($userInfoArr as $val){?>
            <tr>
                <td><?php echo $val['id']?></td>
                <td><?php echo $val['name']?></td>
                <td><?php echo $val['age']?></td>
                <td><?php echo $val['sex']?></td>
                <td><?php echo $val['address']?></td>
                <td><?php echo $val['work']?></td>
                <td><?php echo $val['tel']?></td>
            </tr>
        <?php }?>
        </tbody>
        <tfoot></tfoot>
    </table>

</div>
</body>
</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例


声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议