PHP滚动抽奖代码

页面:

 
   <table style="width: 100%"   cellpadding="2" cellspacing="1" border="0">
		<colgroup>
			<col style="width: 30%"/>
			<col style="width: 70%"/>
		</colgroup>
		<tr>
			<td colspan="2" style="text-align: center">
				<br>
			</td>
		</tr>
		<tr style="display: none;" id="content" >
			<td colspan="2" style="font-size:40px;text-align: center;">
				<div  id="roll" style="width:100%;height:50%;font-weight: bold;margin-top:7.1%">

				</div>
			</td>
		</tr>
		<tr id="imgbut">
			<td colspan="2" style="text-align: center" >
				<div style="height: 50%">
					<img src="/oa/public/css/img/prize.png" id="start" style="width:50%;height:360px">
				</div>
			</td>
		</tr>
	</table>

js代码:


<script type="text/javascript">
	$(function(){
		var _gogo;
		var start_btn = $("#start");
		start_btn.click(function(){
			time=0;
			$.getJSON('/oa/public/index.php/staffinfo/question/getdata/',function(json){
				if(json){
					var obj = eval(json);//将JSON字符串转化为对象
					var len = obj['ids'].length;
					_gogo = setInterval(function(){
						var numrand = getRandomArrayElements(obj['ids'],5);//获取随机数
						var text='';
						time+=1;
						for (var i=0;i<numrand.length;i++)
						{
							text+='<span class="realname" style="margin-left: 25%;float: left;color: 2a475c;">'+obj['question'][numrand[i]]['realname']+'</span><span class="phone" style="margin-right: 25%; float: right;color: orangered;">    '+obj['question'][numrand[i]]['phone']+"</span><br><br>"
						}
						$("#roll").html(text);
						if(time>500){
							clearInterval(_gogo);
						}
					},1); //每隔0.1秒执行一次
					start_btn.hide();
					$("#content").css('display','');
				}else{
					$("#roll").html('系统找不到数据源,请先导入数据。');
				}
			});
		});
	});
	function getRandomArrayElements(arr, count) {
		var shuffled = arr.slice(0), i = arr.length, min = i - count, temp, index;
		while (i-- > min) {
			index = Math.floor((i + 1) * Math.random());
			temp = shuffled[index];
			shuffled[index] = shuffled[i];
			shuffled[i] = temp;
		}
		return shuffled.slice(min);
	}

</script>

控制器:

 
        public function tvshowAction(){
		$prize = $this->_request->get('prize');
		$this->view->prize=$prize;
	}
	public  function getdataAction() {
		global $db;
		$ids = $db->fetchCol('SELECT id FROM oa_user_question WHERE id IN (SELECT min(id) FROM oa_user_question GROUP BY phone) and is_show=1; ');
		$questions = $db->fetchAll('select * from oa_user_question where is_show=1');
		$arr=array();
		foreach($questions as $question){
			$question['realname']=mb_strlen($question['realname'])>8?(substr($question['realname'],0,8).'...'):$question['realname'];
			$question['realname']=iconv('gbk', 'utf-8', $question['realname']);
			foreach($ids as $id){
				if($question['id']==$id){
					$arr[$id]=$question;
				}
			}
		}
		$result=array(
			'ids'=>$ids,
			'question'=>$arr
		);
		echo json_encode($result );
		die;
	}

猜你喜欢

转载自blog.csdn.net/ymk0375/article/details/80080354