需求描述:
要求从每天投票的人中抽出幸运观众,每个用户只能中奖一次(各种情况都有)。
实现思路:
从数据库中取出所有参与者信息,编写程序抽出中奖者的uid 展示,后台审核通过入库,前台即可展示。
关键代码:
此例中只抽取三名幸运观众
<?php
/**
* php抽奖程序核心代码
* edit www.
*/
$sql = “select distinct(***),username from *** where daystatus=0 and ***=’{$yesterday}’ and daystatus=0″ ;
$res = mysql_query()( $sql, $g_mysql ) or sys_exit( “系统忙,请稍候再试”, mysql_error() ) ;
$users = mysql_fetch_all( $res ) ; //获取所有参与者信息,每个人的信息唯一
$award_users = array() ;//建立中奖数组
if ( count( $users ) else {
$nums = get_award3( $users ) ; //执行抽奖程序,返回中奖者的下标
echo “获奖名单如下:” . ”
” ;
foreach ( $nums as $key => $value ) {
$award_users[] = $users[$value] ;
echo “uid :” . $users[$value]['uid'] . ” 昵称:” . $users[$value]['username'] ”
” ;
$uids[$key] = $users[$value]['uid'] ;
}
}
$_SESSION['users'] = ” ;
$_SESSION['users'] = $award_users ; //后台审核抽奖名单入库(确定)时用
echo <<< EOF
抽奖信息入库
EOF;
function get_award3( $users ) {//抽奖函数,为了防止有重复,多了个判断
$count = count( $users ) ;
$i = 0 ;
$result = array() ;
while ( $i < 3 ) {
$j = rand( 0, $count – 1 ) ;
if ( ! in_array( $j, $result ) ) {
$result[$i] = $j ;
$i++ ;
}
}
return $result ;
}
?>
大家可以参考下以上代码的实现思路,有兴趣的朋友,自己动手实现一个完整的php抽奖程序吧。
您可能感兴趣的文章:
php 权重计算方法(抽奖)
学习php抽奖程序之几率算法
php抽奖小程序代码一例
php分时间段的抽奖程序代码
php抽奖计算的例子
一个用于学习的php抽奖程序,代码如下:
<?php
/**
* php抽奖函数
*
* @param integer $first 起始编号
* @param integer $last 结束编号
* @param integer $total 获奖人数
*
* @return string
*
*/
function isWinner($first, $last, $total)
{
$winner = array();
for ($i=0;;$i++)
{
$number = mt_rand($first, $last);
if (!in_array($number, $winner))
$winner[] = $number; // 如果数组中没有该数,将其加入到数组
if (count($winner) == $total) break;
}
return implode(' ', $winner);
}
// for test
echo isWinner(1, 100, 5);
?>
很简单吧,仅供初学的朋友练习与研究之用吧。
您可能感兴趣的文章:
php 权重计算方法(抽奖)学习php抽奖程序之几率算法
php抽奖程序(抽取三名幸运观众)的核心代码
php分时间段的抽奖程序代码
php抽奖计算的例子
对于本文介绍的php抽奖程序,作如下说明:
抽奖概率为:奖品总数为概率
抽奖程序 分时间段
一,数据库:
1,activity_lyq_lottery奖品表
lotteryid
name礼品名称
sum 礼品总数
time_start 开始时间
time_end结束时间
addtime 时间
probability 中奖概率大于0:表示概率0:表示奖品数为概率
isdisable 是否开启0开启1关闭
listorder 排序 默认正序
msg 发送短信或显示信息
content 备注
angle 转盘角度奖品所在的角度
time_1 时间段个数限制 个数限制,-1表示不限制,0表示不发
time_2
time_3
time_4
time_5
time_6
time_7
time_8
time_9
time_10
time_11
time_12
time_13
time_14
time_15
time_16
time_17
2,activity_lyq_lottery_log抽奖日志
logid ID
uid 用户ID
tel 手机号
addtime 抽奖时间
ip 抽奖IP
lotteryid 奖品ID
iswinning 是否抽中1抽中0无
result 返状态回值
hao 随机号
二,主程序代码
require_once (APP_ROOT . "./class/class_lib.php");
require_once libfile ( 'class/StoreProcs' );
pc_base::load_app_class('table_sub','',0);
/**
* 大转盘
* @author Administrator
*
*/
class lib_dzp_lottery extends app_lib {
/**
* 奖品表
*/
public $lottery = 'activity_lyq_lottery';
/**
* 奖品日志表
*/
public $lottery_log = 'activity_lyq_lottery_log';
/**
* n等奖品
*/
private $level = array();
/**
* 奖品信息
*/
private $lotteryArray = array();
/***
* 随机数结束位数
*/
private $max = 30000;
/**
* 类别 true:默认false:掌苏
*/
private $type=true;
/**
* 中奖专业户
*/
private $tel_zy=false;
/**
* 读取配置
*/
public function load_setting() {
$data=getcache('lyqdzp_setting');
define('TIME', time()); //当前时间
define('L_START', strtotime($data['l_start'])); //报名开始时间
define('L_END', strtotime($data['l_end'])); //报名结束时间,-1意思昨天 23:59:59
define('L_DAY_START',$data['l_day_start']); //每天开始时间
define('L_DAY_END', $data['l_day_end']);//每天结束时间
define('L_DAY_CISHU', $data['l_day_cishu']);//每天获得奖品次数
define('L_CISHU', $data['l_cishu']);//共计获得奖品次数
define('L_MIN', 20);//最少来福币
define('L_LFB', $data['l_lfb']);//减少来福币
define('L_ZY', $data['l_zy']); //专业户
define('L_QUANSHU', $data['l_quanshu']);//圈数,大转盘开始圈数
define('L_KONGJIANG', intval($data['l_kongjiang']));//空奖概率
$host='www.****.com'; //现网
if($_SERVER['HTTP_HOST']=='221.****0'){
$host='221.*****'; //测试机
}elseif($_SERVER['HTTP_HOST']=='125.com'){
$host='125life.com';//本地
}elseif(strpos($_SERVER['HTTP_HOST'],'192.168.')==0){
$host=$_SERVER['HTTP_HOST'].'/125e.com';//本地
}
define('L_SITE', 'http://'.$host.'/'); //域名最后加/
define('L_URL', L_SITE.'index.php');//访问地址
//只能通过flash地址访问
$s=L_SITE.'flash/dzp/i.swf';
if(preg_replace('/swf.*$/','swf',$_SERVER['HTTP_REFERER'])!=$s&&$this->type){
exit('老兄你已经得到得够多了,请通过正规途径摇奖!');
}
$this->load();
}
/**
* 黑名单
*/
private $heimingdan = array();
/**
* 读取奖品配置
*/
public function load() {
$this->setlottery ();
$this->heimingdan = getcache ( 'tel', 'blanklist' );
}
private function setlottery() {
$time=strtotime(date('Y-m-d',TIME));
$sql = "select u.*";
$sql .= ", (select COUNT(l.lotteryid) from " . DB::table ( $this->lottery_log ) . " AS l where u.lotteryid=l.lotteryid and l.iswinning='1')as num ";
$sql .= " from " . DB::table ( $this->lottery ) . " AS u ";
$sql .= " where 1 ";
$sql .= " and u.isdisable='0' ";
$sql .= " and u.time_start<='$time' ";
$sql .= " and u.time_end>='$time' ";
$sql .= " ORDER BY u.listorder ";
$d = DB::fetch_all ( $sql );
$sum = 0;
foreach ( $d as $r ) {
$r ['sum_over'] = intval ( $r ['sum'] ) - intval ( $r ['num'] );
if ($r ['sum'] > 0) {
$sum += $r ['sum'];
}
$r['msg']=str_replace()('{name}', $r['name'],$r['msg']);
$this->lotteryArray [$r ['lotteryid']] = $r;
}
if($sum<100) $sum=1000;
$m = 1;
$probability = 0;
$newsum=0;
//80%空奖+20%奖
$this->max = $sum + $sum * (L_KONGJIANG/100) / ((100-L_KONGJIANG)/100);
foreach ( $this->lotteryArray as $r ) {
if ($r ['sum'] > 0) {
$newsum=$r ['sum'];
if($r['probability']>0){
$probability=0;
$probability=ceil($r ['sum']*100/$r['probability']);
if($probability>0){
$newsum=$probability;
}
}
$this->level [$r ['lotteryid']] ['start'] = $m;
$this->level [$r ['lotteryid']] ['end'] = $m + $newsum;
$m = $m + $r ['sum'];
}
}
}
/**
* 检测用户是否中奖 0 谢谢参与,其他 返回奖品data数据
* @param array $user array(uid,tel,ip)
*/
public function check($user) {
if (intval ( $user ['uid'] ) < 1) {
//没有登录
return 0;
}
if(intval(TIME)<intval(L_START)){
//您来早了,活动还没有开始!
return -21;
}
if(intval(TIME)>intval(L_END)){
//已经过了活动时间,非常遗憾,谢谢!
return -22;
}
if ($this->get_user_lfb ( $user ['uid'] ) < L_MIN) {
//$this->credit ( $user ['uid'], -11, $logid );
return -11; //您的来福币余额不足
}
$data = array(
'uid' => $user ['uid'],
'tel' => $user ['tel'],
'ip' => $user ['ip'],
'addtime' => time ()
);
$logid = DB::insert ( $this->lottery_log, $data, true );
//检查用户 每天 已中奖品数
if ($this->get_lottery ( $user ['uid'], strtotime ( date ( 'Y-m-d', TIME ) ), strtotime ( "+1day", strtotime ( date ( 'Y-m-d', TIME ) ) ), true ) >= L_DAY_CISHU) {
$this->credit ( $user ['uid'], -12, $logid );
//很遗憾,你本次什么也没有抽到!
return -12;
}
//检查用户 全部 已中奖品数
if ($this->get_lottery ( $user ['uid'], 0, 0, true ) > L_CISHU&&L_CISHU>0) {
$this->credit ( $user ['uid'], -12, $logid );
//很遗憾,你本次什么也没有抽到!
return -15;
}
//检测黑名单,设置概率
if ($this->checkhei ( $user ['tel'] ) > 0) {
$this->credit ( $user ['uid'], -13, $logid );
//很遗憾,你本次什么也没有抽到!
return -13;
}
$lotteryid = $this->choujiang();
$this->credit ( $user ['uid'], $lotteryid<1?$lotteryid:0, $logid, $lotteryid ,$this->hao);
if ($lotteryid > 0) {
return $this->lotteryArray [$lotteryid];
} else {
return -4;
}
//-1奖品已发完
//-2本时段奖品已玩
//-3不发奖品
//-4奖品有问题
//-11您的来福币余额不足
//-12很遗憾,你本次什么也没有抽到!
//-13黑名单里面,很遗憾,你本次什么也没有抽到!
//-14中奖专业户禁止中高讲高奖品
//-15已超过yizhong总奖品数
//-16 掌苏传递手机 号错误
//-21您来早了,活动还没有开始!
//-22已经过了活动时间,非常遗憾,谢谢!
//-23对不起,现在不是活动时间,活动时间为每天
}
/**
* 显示
* @param unknown_type $data
*/
public function show($data){
$ok=0;//3中奖
$zz=0;//转盘度数
$result='';//奖品名称
$lotteryid=0;//奖品ID
if(is_array($data)){
$ok=3;
$result=$data['msg'];
$zz=$data['angle']+L_QUANSHU*360;
$lotteryid=$data['lotteryid'];
}else{
$m='';
switch($data){
case 0:{
$m= '请先登录!';
}break;
case -11:{
$m= '您的来福币余额不足!';
}break;
case -12:{
$m= '很遗憾,你本次什么也没有抽到!';
}break;
case -21:{
$m= '您来早了,活动还没有开始!';
}break;
case -22:{
$m= '已经过了活动时间,非常遗憾,谢谢!';
}break;
case -23:{
$m= '对不起,现在不是活动时间,活动时间为每天'.L_DAY_START.':00-'.L_DAY_END.':00';
}break;
default:{
$m= '很遗憾,你本次什么也没有抽到!';
}break;
}
$result=$m;
$ok=3;//3中奖
$zz=360+L_QUANSHU*360;//再接再厉
}
header("Expires:Mon,26Jul199705:00:00GMT");
header("Cache-Control:no-cache,must-revalidate");
header("Pragma:no-cache");
header("Content-type:text/html;charset=utf-8");
if(!$this->type){
echo json_encode(array('return'=>$lotteryid,
'msg'=>$result));
}else{
echo '<?xml version="1.0" encoding="UTF-8"?>'."\n";
echo '<gift>'."\n";
echo '<shift ok="'.$ok.'" zz="'.$zz.'" add="9" down="0" lid="0" result="'.$result.'"/>'."\n";
echo '</gift>'."\n";
}
exit;
}
/**
* 抽奖的号
*/
private $hao=0;
/**
* 抽奖开始
*/
private function choujiang() {
$j = 0;
$lotteryid = 0;
$j = mt_rand ( 1, $this->max );
$this->hao=$j;
foreach ( $this->level as $k => $r ) {
if ($r ['start'] <= $j && $r ['end'] > $j) {
$lotteryid = $k;
break;
}
}
$d = array(
1 => array(
'time' => strtotime ( date ( 'Y-m-d 00:00:00', TIME ) ),
'time_end' => strtotime ( date ( 'Y-m-d 07:59:59', TIME ) )
)
);
for ($i=2;$i<=17;$i++){
$d[$i]=array(
'time' => strtotime ( date ( 'Y-m-d '.($i+6).':00:00', TIME ) ),
'time_end' => strtotime ( date ( 'Y-m-d '.($i+6).':59:59', TIME ) )
);
}
if($lotteryid>0){
$l=$this->lotteryArray [$lotteryid];
$sql = "select count(*) m from " . DB::table ( $this->lottery_log );
$sql .= " where lotteryid='$lotteryid' ";
$s= intval ( DB::result_first ( $sql ) );
$sum_over=$l['sum']-$s;
if($sum_over>0){
//本时段 奖品数
$sum=0;
foreach ($d as $k=>$r){
if(TIME>=$r['time']&&TIME<=$r['time_end']){
if($l['time_'.$k]>0){//个数限制
$sum=$this->get_lottery_id(0,$r['time'],$r['time_end']+1,$lotteryid);
if($sum>=$l['time_'.$k]){
//本时段奖品已玩
$lotteryid=-2;
break;
}
}elseif ($l['time_'.$k]==0){
//不发奖品
$lotteryid=-3;
break;
}else{
//-1 不限制
}
}
}
if($this->tel_zy && $lotteryid>0&&$lotteryid<4){
$lotteryid=-14;
}
}else{
//奖品已发完
$lotteryid=-1;
}
}else{
//奖品有问题
$lotteryid=-4;
}
return $lotteryid;
}
/**
* 检测黑名单和普通用户,概率
* @param string $tel
* @return 大于1 表示本次禁止抽
*/
private function checkhei($tel) {
$n = mt_rand ( 1, 100 );
if (in_array ( $tel, $this->heimingdan ['tel'] ) && L_ZY > 0) {
$this->tel_zy=true;
if ($n > L_ZY) {
return 1;
}
}else{
//号码检测
if (!self::check_tel($tel)) {
return 1;
}
}
return 0;
}
/**
* 合计奖品数
* @param intval $uid
* @param intval $start
* @param intval $end
* @param bool $bool true 获得奖品数 false 不统计
* @return number
*/
private function get_lottery($uid = 0, $start = 0, $end = 0, $bool = false) {
$sql = "select count(*) m from " . DB::table ( $this->lottery_log );
$sql .= " where 1 ";
if (intval ( $start ) > 0) {
$sql .= " and addtime>='$start' ";
}
if (intval ( $end ) > 0) {
$sql .= " and addtime<'$end' ";
}
if ($bool) {
$sql .= " and lotteryid>0 ";
}
if (intval ( $uid ) > 0) {
$sql .= " and uid='$uid' ";
}
return intval ( DB::result_first ( $sql ) );
}
/**
* 合计奖品数
* @param intval $uid
* @param intval $start
* @param intval $end
* @param intval $bool 奖品id
* @return number
*/
private function get_lottery_id($uid = 0, $start = 0, $end = 0, $lotteryid = 0) {
$sql = "select count(*) m from " . DB::table ( $this->lottery_log );
$sql .= " where 1 ";
if (intval ( $start ) > 0) {
$sql .= " and addtime>='$start' ";
}
if (intval ( $end ) > 0) {
$sql .= " and addtime<'$end' ";
}
if ($lotteryid>=0) {
$sql .= " and lotteryid='$lotteryid' ";
}
if (intval ( $uid ) > 0) {
$sql .= " and uid='$uid' ";
}
return intval ( DB::result_first ( $sql ) );
}
/**
* 获取用户来福币
* @param intval $uid
* @return number
*/
public function get_user_lfb($uid) {
$sql = "select extcredits2 from sz_common_member_count ";
$sql .= " where 1 ";
$sql .= " and uid='$uid' ";
return intval ( DB::result_first ( $sql ) );
}
/**
* 记录日志和更新用户来福币
* @param intval $uid
* @param intval $r 返回值
* @param intval $logid
* @param intval $lotteryid
*/
private function credit($uid, $result, $logid = 0, $lotteryid = 0,$hao=0) {
$data = array();
if ($lotteryid > 0) {
$data ['lotteryid'] = $lotteryid;
$data ['iswinning'] = 1;
}
$data ['result'] = $result;
$data ['hao'] = $hao;
$data ['HTTP_REFERER'] = $_SERVER['HTTP_REFERER'];
DB::Update ( $this->lottery_log, $data, " logid='$logid' " );
if (L_LFB > 0) {
$sql = 'UPDATE sz_common_member_count SET extcredits2=extcredits2-' . L_LFB . ' WHERE uid=' . $uid;
DB::query ( $sql );
$this->credit_log ( $uid, 'reduce', L_LFB, 1, '积分清零活动' );
}
}
/**
*手机号检查
*/
private function check_tel($tel){
$different_network=table_sub::get_different_network();
if (!preg_match("/$different_network/",$tel)) {
return false;
}
return true;
}
/**
* 存储过程链接
*/
private $StoreProcs;
/**
* 来福币日志 存储过程
* @param intval $uid
* @param string $op add增加 reduce 消耗
* @param intval $creditvalue 来福币
* @param intval $credittype 1来福币 2来福点
* @param string $oreason 说明
*/
private function credit_log($uid, $op, $creditvalue, $credittype, $oreason) {
if (! $this->StoreProcs) {
$this->StoreProcs = new StoreProcs ();
}
//调用存储过程
$this->StoreProcs->query ( "call app_credit_log($uid,'$op','$credittype',1,$creditvalue,'$oreason')" );
}
}
三,调用:
$dzp=new lib_dzp_lottery();
$u=array('uid'=>$_G ['uid'],'tel'=>$_G ['tel'],'ip'=> $_G ['clientip']);
$dzp->load_setting();
$dzp->show($dzp->check($u));
?>
您可能感兴趣的文章:
php 权重计算方法(抽奖)学习php抽奖程序之几率算法
php抽奖程序(抽取三名幸运观众)的核心代码
php抽奖小程序代码一例
php抽奖计算的例子