Home  >  Article  >  Backend Development  >  How to implement lottery program in php

How to implement lottery program in php

藏色散人
藏色散人Original
2020-07-27 10:26:005870browse

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.

How to implement lottery program in php

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(&#39;中奖了!&#39;); 
                        } 
                 }); 
            } 
        } 
    }); 
});

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( 
    &#39;0&#39; => array(&#39;id&#39;=>1,&#39;min&#39;=>1,&#39;max&#39;=>29,&#39;prize&#39;=>&#39;一等奖&#39;,&#39;v&#39;=>1), 
    &#39;1&#39; => array(&#39;id&#39;=>2,&#39;min&#39;=>302,&#39;max&#39;=>328,&#39;prize&#39;=>&#39;二等奖&#39;,&#39;v&#39;=>2), 
    &#39;2&#39; => array(&#39;id&#39;=>3,&#39;min&#39;=>242,&#39;max&#39;=>268,&#39;prize&#39;=>&#39;三等奖&#39;,&#39;v&#39;=>5), 
    &#39;3&#39; => array(&#39;id&#39;=>4,&#39;min&#39;=>182,&#39;max&#39;=>208,&#39;prize&#39;=>&#39;四等奖&#39;,&#39;v&#39;=>7), 
    &#39;4&#39; => array(&#39;id&#39;=>5,&#39;min&#39;=>122,&#39;max&#39;=>148,&#39;prize&#39;=>&#39;五等奖&#39;,&#39;v&#39;=>10), 
    &#39;5&#39; => array(&#39;id&#39;=>6,&#39;min&#39;=>62,&#39;max&#39;=>88,&#39;prize&#39;=>&#39;六等奖&#39;,&#39;v&#39;=>25), 
    &#39;6&#39; => array(&#39;id&#39;=>7,&#39;min&#39;=>array(32,92,152,212,272,332), 
&#39;max&#39;=>array(58,118,178,238,298,358),&#39;prize&#39;=>&#39;七等奖&#39;,&#39;v&#39;=>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 = &#39;&#39;; 
 
    //概率数组的总概率精度 
    $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[&#39;id&#39;]] = $val[&#39;v&#39;]; 
} 
 
$rid = getRand($arr); //根据概率获取奖项id 
 
$res = $prize_arr[$rid-1]; //中奖项 
$min = $res[&#39;min&#39;]; 
$max = $res[&#39;max&#39;]; 
if($res[&#39;id&#39;]==7){ //七等奖 
    $i = mt_rand(0,5); 
    $result[&#39;angle&#39;] = mt_rand($min[$i],$max[$i]); 
}else{ 
    $result[&#39;angle&#39;] = mt_rand($min,$max); //随机生成一个角度 
} 
$result[&#39;prize&#39;] = $res[&#39;prize&#39;]; 
 
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: &#39;POST&#39;, 
        url: &#39;data.php&#39;, 
        dataType: &#39;json&#39;, 
        cache: false, 
        error: function(){ 
            alert(&#39;出错了!&#39;); 
            return false; 
        }, 
        success:function(json){ 
            $("#startbtn").unbind(&#39;click&#39;).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(&#39;恭喜你,中得&#39;+p+&#39;\n还要再来一次吗?&#39;); 
                    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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn