2020-05-18

<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <style>
        .header{
            width:80%;
            height:80px;
            min-width: 505px;
            margin:0 auto;
            text-align: center;
        }
        .header input{
            height: 60px;
            width: 400px;
            border: 1px solid #ccc;
            box-sizing: border-box;
            vertical-align: middle;
            outline: none;
            text-indent: 30px;
            font-size: 20px;
        }
        .header button{
            background-color: gold;
            width: 100px;
            height: 60px;
            line-height: 60px;
            color: #FFF;
            border: 1px solid #ccc;
            vertical-align: middle;
            outline: none;
        }
        .list{
            width: 80%;
            min-height: 505px;
            background-color: beige;
            margin:0 auto;
        }
        .list span{
            display: inline-block;
            background-color: aquamarine;
            color: #333;
            padding:10px 20px;
            border-radius:5px;
            margin: 20px;
            cursor: pointer;

        }
    </style>
    <title></title>
</head>
<body>
    <div class="header">
        <input type="text" name="number"><button>随机产生菜</button>
    </div>
    <div class="list">
    </div>
    <script>
    $(function(){
        $('button').click(function(){
            $('.list').on('click','span',function(){
                 $(this).css({"backgroundColor":"grey",}) 
                            });
            
            //get请求
            $.get('create.php',{num:$('input').val()},function(e){
                $('.list').html('');
                for(var i=0;i<e.length;i++){
                    $('<span>'+e[i]+'</span>').appendTo($('.list'));
                }
            },'json')
        });
    })
    </script>
</html> 
php
<?php 
$num = (int)$_GET['num'];
//至少产生1名学生
if($num<=0){
    $re= [
        'code' => 1001,
        'message'=>'至少产生一名学生'
    ];
    echo json_encode($re);
    die;
}
//所有的学生
$stu_arr = explode('|',file_get_contents('stu.txt'));
//in_array  判断是否在一个数组中
//mt_rand 产生随机数
$arr = [];  //用户保存学生的编号
for($i=0;$i<$num;$i++){ //用于循环产生学生编号
    $stu_num = mt_rand(0,6); //随机产生
    while(in_array($stu_num,$arr)){ //判断是否产生了相同的学生编号
        $stu_num = mt_rand(0,5);;
    }
    array_push($arr,$stu_num);
}

$re = []; //返回的学生数组
foreach($arr as $k){
    array_push($re,$stu_arr[$k]);
}
echo json_encode($re);
QQ截图20200518213324.png
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。