renheshi 发表于 2017-3-27 06:13:22

php 抽奖程序的书写

  题目:抽奖程序的书写
  背景:从每天投票的人中抽出幸运观众,每个用户只能中奖一次(各种情况都有)
  思路:从数据库中取出所有参与者信息,编写程序抽出中奖者的uid展示,后台审核通过 的话就入库,前台就可以展示。
  关键代码:
  此例中只抽取三名幸运观众

$sql = "select distinct(***),username from *** where daystatus=0 and ***<='{$nowtime}' 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 ) <= 3 ) {}   //如果不多余3名参与者   
else {
$nums = get_award3( $users ) ;//执行抽奖程序,返回中奖者的下标
echo "获奖名单如下:" . "<br /><br />" ;
foreach ( $nums as $key => $value ) {
$award_users[] = $users[$value] ;
echo "uid :" . $users[$value]['uid'] . " 昵称:" . $users[$value]['username'] "<br />" ;
$uids[$key] = $users[$value]['uid'] ;
}
}
$_SESSION['users'] = '' ;
$_SESSION['users'] = $award_users ;//后台审核抽奖名单入库(确定)时用
echo <<< EOF
<a href="checklist.php?action=******>重新抽奖</a><br/>
<a href="checklist.php?action=******">抽奖信息入库</a><br/>   
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 ;
}
页: [1]
查看完整版本: php 抽奖程序的书写