Home >Backend Development >PHP Problem >How to implement lottery program in php
php method to implement the lottery program: first load the jquery library file and "jQueryRotate.js" in the head; then build a multi-dimensional array in "data.php"; then calculate based on the probability set in the array Get the ID that meets the conditions; finally call the getRand method.
Recommended: "PHP Tutorial"
Preparation work
First of all, prepare the materials and draw a lottery The interface uses two pictures, a disk picture and a pointer picture. In actual applications, different disk pictures can be produced according to different needs.
Then create the html page. In the example, we add the following code to the body:
<div class="demo"> <div id="disk"></div> <div id="start"><img src="start.png" id="startbtn"></div> </div>
We use #disk to place the disk background image, control it in css, and use #start to place the pointer Picture start.png.
Then we use CSS to control the position of the pointer and the disk. The code is as follows:
.demo{width:417px; height:417px; position:relative; margin:50px auto} #disk{width:417px; height:417px; background:url(disk.jpg) no-repeat} #start{width:163px; height:320px; position:absolute; top:46px; left:130px;} #start img{cursor:pointer}
jQuery
If we want to make the pointer rotate without using flash, we You can use HTML5 canvas to rotate images, but you need to consider browser compatibility. A jQuery plug-in can completely rotate images (any html elements) and is compatible with major browsers. It is jQueryRotate.js.
Using jQueryRotate.js, you can rotate pictures to any angle, bind mouse events, and set animation effects and callback functions for the rotation process.
The method of use is of course to first load the jquery library file and jQueryRotate.js in the head, and then we can use the following code to realize the pointer rotation.
$(function(){ $("#startbtn").rotate({ bind:{ click:function(){//绑定click单击事件 var a = Math.floor(Math.random() * 360); //生成随机数 $(this).rotate({ duration:3000,//转动时间间隔(转动速度) angle: 0, //开始角度 animateTo:3600+a, //转动角度,10圈+ easing: $.easing.easeOutSine, //动画扩展 callback: function(){ //回调函数 alert('中奖了!'); } }); } } }); });
The above code is implemented: when the pointer "Start Lottery" button is clicked, the pointer starts to rotate, and the rotation angle is 3600 a, that is, after 10 turns, it will rotate to a randomly generated angle of a. When the rotation angle reaches 3600 Stop rotating at degree a.
It should be noted that easing: animation extension needs to be combined with the animation extension plug-in to achieve it. The easing plug-in is introduced in this article: jQuery Easing Animation Effect Extension
This article has completed the process of turning the turntable pointer, but it needs to be combined with lottery control to be considered a complete lottery program. We will follow this This article will introduce the use of PHP to control the lottery probability, and how to use jQuery and PHP to complete the interactive process of the lottery, so stay tuned.
PHP
First, we set the corresponding angle and winning probability according to the prizes on the lottery disk, and we build a Multidimensional array:
$prize_arr = array( '0' => array('id'=>1,'min'=>1,'max'=>29,'prize'=>'一等奖','v'=>1), '1' => array('id'=>2,'min'=>302,'max'=>328,'prize'=>'二等奖','v'=>2), '2' => array('id'=>3,'min'=>242,'max'=>268,'prize'=>'三等奖','v'=>5), '3' => array('id'=>4,'min'=>182,'max'=>208,'prize'=>'四等奖','v'=>7), '4' => array('id'=>5,'min'=>122,'max'=>148,'prize'=>'五等奖','v'=>10), '5' => array('id'=>6,'min'=>62,'max'=>88,'prize'=>'六等奖','v'=>25), '6' => array('id'=>7,'min'=>array(32,92,152,212,272,332), 'max'=>array(58,118,178,238,298,358),'prize'=>'七等奖','v'=>50) );
Array $prize_arr, id is used to identify different awards, min represents the minimum angle corresponding to each award interval in the disk, max represents the maximum angle, such as the minimum angle corresponding to the first prize: 0, The maximum angle is 30. Here we set the max value to 1 and the max value to 29 to prevent the pointer from pointing to the center line of two adjacent prizes after the lottery. Since there are multiple seventh prizes in the disk, we set the angle range corresponding to each seventh prize in the array. Prize represents the content of the award, and v represents the probability of winning. We will find that the sum of v of the seven awards in the array is 100. If the value of v is 1, it means that the probability of winning is 1%, and so on.
Regarding the winning probability algorithm, the article on this site: PHP jQuery implements flip lottery draws introduces the classic probability algorithm. This article directly brings the code.
function getRand($proArr) { $result = ''; //概率数组的总概率精度 $proSum = array_sum($proArr); //概率数组循环 foreach ($proArr as $key => $proCur) { $randNum = mt_rand(1, $proSum); if ($randNum <= $proCur) { $result = $key; break; } else { $proSum -= $proCur; } } unset ($proArr); return $result; }
The function getRand() will calculate the qualified ID based on the probability set in the array. We can then call getRand().
foreach ($prize_arr as $key => $val) { $arr[$val['id']] = $val['v']; } $rid = getRand($arr); //根据概率获取奖项id $res = $prize_arr[$rid-1]; //中奖项 $min = $res['min']; $max = $res['max']; if($res['id']==7){ //七等奖 $i = mt_rand(0,5); $result['angle'] = mt_rand($min[$i],$max[$i]); }else{ $result['angle'] = mt_rand($min,$max); //随机生成一个角度 } $result['prize'] = $res['prize']; echo json_encode($result);
In the code, we call getRand() to obtain the award obtained through probability operation, and then generate an angle value between the minimum angle and the maximum angle according to the angle range configured in the award, and construct an array, Contains angle and prize, and is finally output in json format.
jQuery
Based on the above, we modify the front-end jQuery code. When the "Start Lottery" button is clicked, an ajax request is sent to the background data.php. If the request is successful And return the award information, then rotate the pointer to the angle value returned by data.php.
$(function(){ $("#startbtn").click(function(){ lottery(); }); }); function lottery(){ $.ajax({ type: 'POST', url: 'data.php', dataType: 'json', cache: false, error: function(){ alert('出错了!'); return false; }, success:function(json){ $("#startbtn").unbind('click').css("cursor","default"); var a = json.angle; //角度 var p = json.prize; //奖项 $("#startbtn").rotate({ duration:3000, //转动时间 angle: 0, animateTo:1800+a, //转动角度 easing: $.easing.easeOutSine, callback: function(){ var con = confirm('恭喜你,中得'+p+'\n还要再来一次吗?'); if(con){ lottery(); }else{ return false; } } }); } }); }
We build a custom function lottery(). In lottery() we send a POST request to data.php. If the winning information is successfully returned, the rotate plug-in is called to start rotating. The rotation angle is the angle returned by the background. Decision, here we use 1800 a to represent the angle of rotation, that is, the pointer stops after rotating 6 times a degree, and then we call lottery() when we click the "Start Lottery" button, so the carousel lottery is completed.
The above is the detailed content of How to implement lottery program in php. For more information, please follow other related articles on the PHP Chinese website!