随机抽奖程序

刘德胜 341次浏览

摘要:第一步:创建数据表CREATETABLEIFNOTEXISTS`member`( `id`int(11)NOTNULLAUTO_INCREMENT, `banji`varchar(50)COLLATEutf8_unicode_ciDEFAULTNULLCOMM...

第一步:创建数据表

CREATE TABLE IF NOT EXISTS `member` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `banji` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '班级',
  `studentdid` int(11) DEFAULT NULL COMMENT '学号',
  `name` varchar(13) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '姓名',
  `jiangxing` varchar(9) COLLATE utf8_unicode_ci DEFAULT '0' COMMENT '是否已抽取',
  `cctime` int(11) DEFAULT '0' COMMENT '抽取时间',
  PRIMARY KEY (`id`),
  UNIQUE KEY `id` (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci ROW_FORMAT=DYNAMIC AUTO_INCREMENT=1 ;

第二步:创建主程序代码

<?php 
/*** 
    * 文件名称:rand.php
    * 功能:随机抽取学生
    * 开发时间: 2019-02-21
    * 开发:刘德胜
    * Q  Q:80507419 
    * 网站:http://www.hebazi.net 
    */
$banji = $job->query("SELECT DISTINCT `banji` FROM `member`");
$member = $job->query("SELECT banji,name FROM `member` where jiangxing!=0 order by cctime desc");
?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>随机抽取学生</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="public/layui/css/layui.css"  media="all">
    <script src="public/js/jquery.min.js"></script>
    <style>
        html,body{width: 100%;height:100%;}
        body{background: url(dpbj.jpg);height: 100%;width:100%;overflow: hidden;background-size:100% 100%;}    
        .layui-card {
            margin: 10px;
        }   
        .layui-input-block {
            margin-left: 0px; 
        } 
        .layui-card-header {
            font-size: 20px;
            font-weight: 900;
        } 
        .layui-card {
            background-color:rgba(255,255,255,0.7);
        }
        .main{
            margin:100px;
            border:2px solid #ccc;
            line-height: 100px;
            text-align: center;
            font-weight: 900;
            font-size: 30px;
            margin-bottom: 20px;
            background: rgba(0,0,0,0.8);
            color:#fff;
        }  
        .bnt{
            margin:20px;
            text-align: center;
        } 
        .box{
            margin:10px;
            padding: 5px;
            font-weight: 900;
            font-size:18px;
            border:1px solid #ccc;
            background: #ccc;
            color:#000;
        }                 
    </style>
</head>
<body>
<br><br><br><br>
<div class="layui-fluid" >
    <div class="layui-row">
        <div class="layui-col-md4">
            <div class="layui-card">
              <div class="layui-card-header">选择班级</div>
              <div class="layui-card-body">
                <form class="layui-form" action="">
                  <div class="layui-form-item">
                    <div class="layui-input-block">
                    <?php foreach($banji as $k=>$v){ ?>
                      <input type="checkbox" name="like[<?=$k?>]" value="<?=$v['banji']?>" title="<?=$v['banji']?>">
                    <?php } ?>
                    </div>
                  </div>
                </form>
              </div>
            </div>
            <div class="layui-card">
              <div class="layui-card-header">已抽选名单</div>
              <div class="layui-card-body" style="min-height: 300px;">
                <div class="layui-row" id="ycc">
                    <?php foreach($member as $v){ ?>
                    <div class="layui-col-md6" style="margin:10px auto;">
                        <span class="box"><?=$v['banji']?>-<?=$v['name']?></span>
                    </div>
                <?php } ?>
                </div>

              </div>
            </div>
        </div>
        <div class="layui-col-md8">            
            <div class="layui-card">
              <div class="layui-card-header">随机抽取学生</div>
              <div class="layui-card-body">
                    <div class="main" id="nameid">
                        暂未抽取
                    </div>
                    <div class="bnt">
                        <div class="layui-btn-group">
                          <button type="button" class="layui-btn layui-btn-lg" onclick="startfun()" id="btn">开始抽取</button>
                          <button type="button" class="layui-btn layui-btn-lg layui-btn-danger layui-btn-disabled" id="btna">停止抽取</button>
                        </div>
                    </div>
              </div>
            </div>
        </div>
    </div>
</div> 
<input type="hidden" id="banjiname">
</body>
<script src="public/layui/layui.js" charset="utf-8"></script>
<script>
//Demo
layui.use('form', function(){
  var form = layui.form;  
  //监听提交
  form.on('submit(formDemo)', function(data){
    layer.msg(JSON.stringify(data.field));
    return false;
  });
});
function startuser(){
    $("#banjiname").val("");
    var banjiname = $("#banjiname").val();
    $("input[type='checkbox']").each(function () {
        if ($(this).is(":checked")) {
            banjiname = banjiname + "|" + $(this).val();
            $("#banjiname").val(banjiname);
        }
    });
    banji = $("#banjiname").val();
    $.post("ajax.php",{banji:banji},function(data){
        if(data==6){
            alert("请选择班级");
            stopfun(1);
        }else if(data==5){
            alert("该班级已抽取完,请重新选项班级!");
            stopfun(1);
        } else{
            $("#nameid").html(data);
        }        
    });
}
function startfun(){
    $("#btn").removeAttr("onclick");
    $("#btn").addClass("layui-btn-disabled ");

    $("#btna").removeClass("layui-btn-disabled "); 
    $("#btna").attr("onclick","stopfun(2)"); 

    idstart = setInterval(startuser,100);   
}
function stopfun(s){    
    clearInterval(idstart); 
    $("#btn").removeClass("layui-btn-disabled "); 
    $("#btn").attr("onclick","startfun()");  

    $("#btna").removeAttr("onclick");
    $("#btna").addClass("layui-btn-disabled ");    
    if(s!=1){
        banji = $("#nameid").html();
        $.post("ajax_jg.php",{banji:banji},function(data){
            if(data!=5){
                words = data.split('|')
                $("#ycc").prepend(words[0]);
                $("#nameid").html(words[1]);
            }      

        }); 
    }
}
</script>
</html>

第三步:创建请求程序代码

<?php 
/*** 
	* 文件名称:ajax.php
	* 功能:随机抽取
  * 开发时间: 2019-02-21
  * 开发:刘德胜
  * Q  Q:80507419  
  * 网站:http://www.hebazi.net 
	*/

$banji = $_POST['banji'];
if(empty($banji)){
  echo 6;
  exit;
}
$arr   = explode("|",$banji);
$where = '';
foreach($arr as $v){
  if(!empty($v)){
    $where .= " banji='{$v}' or";
  }
}

$where  = rtrim($where,"or");
$nun    = $job->find("SELECT count(*) FROM `member` where ($where) and jiangxing=0");
if($nun[0]<1){
  file_put_contents("1.txt", $nun[0]);
  echo 5;
  exit;
}

$member = $job->getone("SELECT * FROM `member` where ($where) and jiangxing=0  ORDER BY RAND()  LIMIT 1");
echo $member['banji'].'-'.$member['name'];

第四步:创建最终抽取人员代码

<?php 
/*** 
	* 文件名称:ajax_jg.php
	* 功能:最终结果
  * 开发时间: 2019-02-21
  * 开发:刘德胜
  * Q  Q:80507419 
  * 网站:http://www.hebazi.net    
	*/
$banji = $_POST['banji'];
$arr   = explode("-",$banji);
$banji = $arr[0];
$name  = $arr[1];
$time  = time();
$member = $job->getone("SELECT name FROM `member` where  banji = '$banji' and name='$name'");
if(empty($member)){
  echo 5;
  exit;
}

mysql_query("UPDATE `member` SET `jiangxing`='1',`cctime`=$time where banji = '$banji' and name='$name'");
?>
<div class="layui-col-md6" style="margin:10px auto;">
    <span class="box"><?php echo $banji.':'.$name;?></span>
</div>|<?php echo $banji.'-'.$name;?>