PHP实现活动人选抽奖功能


Posted in PHP onApril 19, 2017

本文介绍情景为活动人选抽奖,通过简单随机抽取指定人数,依次列举被抽中的人名。示例代码如下:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>活动人选</title>
  </head>
  <body>
  <?php
  $lotterPeopleArr = array();
  $num = 8;

  for($i=0;$i<$num;$i++){
    $lotterPeople = GetRandStr(1);

    while(in_array($lotterPeople, $lotterPeopleArr)){
      $lotterPeople = GetRandStr(1);
    }

    $lotterPeopleArr[$i] = $lotterPeople;
    echo "抽中人:". $lotterPeopleArr[$i].'<br/>';
  }

  // echo '抽中人:'. $lotterPeople;



  /**
   * 生成随机数
   * $type int 默认(字母&&数字) 1 纯字母 2.纯数字 3.英文单词
   */
   function GetRandStr($len='4',$type='3')
  {


    switch($type)
    {
      case '1';
      $chars = array(
          "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
          "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
          "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",
          "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
          "S", "T", "U", "V", "W", "X", "Y", "Z"
      );
      break;
      case '2';
      $chars = array(
          "0", "1", "2",
          "3", "4", "5", "6", "7", "8", "9"
      );
      break;
      case '3';
      $chars = array(
          "小华","小冠","小王","小吴","小朱","小何","小陈","小熊","小斌","小李","小春","小超","小张","小郭","小柯","小梁","小理","小龚","小杰","小余","小洪","小郑","小志","小潘",
      );
      break;
      default;
      $chars = array(
          "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
          "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
          "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",
          "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
          "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2",
          "3", "4", "5", "6", "7", "8", "9"
      );
      break;
    }
    $charsLen = count($chars) - 1;
    shuffle($chars);
    $output = "";
    for ($i=0; $i<$len; $i++)
    {
    $output .= $chars[mt_rand(0, $charsLen)];
    }
    return $output;
  }
  ?>
  </body>
</html>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

PHP 相关文章推荐
提问的智慧
Oct 09 PHP
php获取后台Job管理的实现代码
Jun 10 PHP
thinkphp普通查询与表达式查询实例分析
Nov 24 PHP
php实现parent调用父类的构造方法与被覆写的方法
Feb 11 PHP
Laravel 5 框架入门(四)完结篇
Apr 09 PHP
php隐藏实际地址的文件下载方法
Apr 18 PHP
CI框架使用composer安装的依赖包步骤与方法分析
Nov 21 PHP
PHP编程快速实现数组去重的方法详解
Jul 22 PHP
CMSPRESS 10行代码搞定 PHP无限级分类2
Mar 30 PHP
PHP实现防止表单重复提交功能【基于token验证】
May 24 PHP
解决windows上php xdebug 无法调试的问题
Feb 19 PHP
利用PHP计算有多少小于当前数字的数字方法示例
Aug 26 PHP
thinkphp下MySQL数据库读写分离代码剖析
Apr 18 #PHP
Thinkphp通过一个入口文件如何区分移动端和PC端
Apr 18 #PHP
Yii2汉字转拼音类的实例代码
Apr 18 #PHP
php+resumablejs实现的分块上传 断点续传功能示例
Apr 18 #PHP
ZendFramework2连接数据库操作实例
Apr 18 #PHP
PHP实现的数独求解问题示例
Apr 18 #PHP
PHP使用finfo_file()函数检测上传图片类型的实现方法
Apr 18 #PHP
You might like
function.inc.php超越php
2006/12/09 PHP
php在windows环境下获得cpu内存实时使用率(推荐)
2018/02/08 PHP
解决Laravel blade模板转义html标签的问题
2019/09/03 PHP
javascript 判断整数方法分享
2014/12/16 Javascript
你知道setTimeout是如何运行的吗?
2016/08/16 Javascript
用jquery获取自定义的标签属性的值简单实例
2016/09/17 Javascript
Dropzone.js实现文件拖拽上传功能(附源码下载)
2016/11/22 Javascript
js判断PC端与移动端跳转
2020/12/24 Javascript
javascript基本数据类型和转换
2017/03/17 Javascript
使用 NodeJS+Express 开发服务端的简单介绍
2017/04/07 NodeJs
JS写XSS cookie stealer来窃取密码的步骤详解
2017/11/20 Javascript
微信小程序实现导航栏选项卡效果
2020/06/19 Javascript
微信小程序的部署方法步骤
2018/09/04 Javascript
详解关于webpack多入口热加载很慢的原因
2019/04/24 Javascript
vue相关配置文件详解及多环境配置详细步骤
2020/05/19 Javascript
[41:21]夜魇凡尔赛茶话会 第三期02:看图识人
2021/03/11 DOTA
python操作数据库之sqlite3打开数据库、删除、修改示例
2014/03/13 Python
Python中import机制详解
2017/11/14 Python
使用python实现knn算法
2017/12/20 Python
Python装饰器用法实例总结
2018/05/26 Python
Python实现的KMeans聚类算法实例分析
2018/12/29 Python
Python实现程序判断季节的代码示例
2019/01/28 Python
浅析使用Python搭建http服务器
2019/10/27 Python
kafka-python 获取topic lag值方式
2019/12/23 Python
matplotlib.pyplot.matshow 矩阵可视化实例
2020/06/16 Python
用python实现名片管理系统
2020/06/18 Python
基于CSS3的animation属性实现微信拍一拍动画效果
2020/06/22 HTML / CSS
贝尔帐篷精品店:Bell Tent Boutique
2019/06/12 全球购物
留学推荐信怎么写
2014/01/25 职场文书
环境科学专业优秀毕业生自荐书
2014/02/03 职场文书
求职信格式要求
2014/05/23 职场文书
国庆节演讲稿范文2014
2014/09/19 职场文书
离婚答辩状范文
2015/05/22 职场文书
2016年大学光棍节活动总结
2016/04/05 职场文书
将MySQL的表数据全量导入clichhouse库中
2022/03/21 MySQL
解决springboot druid数据库连接失败后一直重连的方法
2022/04/19 Java/Android