» » Use jQuery + PHP + Mysql to achieve lottery program

 

Use jQuery + PHP + Mysql to achieve lottery program

Author: bamboo06 on 8-11-2014, 03:41, views: 11284

6
Lottery program widely used in real life, due to the different application scenarios draw the way they are varied. This article will use examples to explain how to use jQuery + PHP + Mysql achieve a similar TV with a simple common lottery program.

In this case the lottery program to achieve massive phone numbers from a random sample of a number as the winning numbers, lottery multiple times, was drawn numbers will not be drawn again. Lottery process: Click the "Start" button, the program get number information, scroll the display number, click on the "Stop" button, the number stops rolling, then the number is the winning number is displayed, you can click the "Start" button to continue the draw.
HTML
  <div class="demo">
     <div id="roll"></div><input type="hidden" id="mid" value="">
     <p><input type="button" class="btn" id="start" value="Start"> <input type="button" class="btn" id="stop" value="Stop"></p>
     <div id="result"></div>
  </div>

The above code, we need a #roll to display scrolling numbers, # mid pumping is used to record the number id, then that requires two buttons are used to "start" and "stop" action, and finally also need a # result display lottery results.
CSS
We used to decorate a simple html css page.
.demo{width:300px; margin:60px auto; text-align:center} 
#roll{height:32px; line-height:32px; font-size:24px; color:#f30} 
.btn{width:80px; height:26px; line-height:26px; background:url(btn_bg.gif) 
 repeat-x; border:1px solid #d3d3d3; cursor:pointer} 
#stop{display:none} 
#result{margin-top:20px; line-height:24px; font-size:16px; text-align:center} 

Note that we will default button #stop set to display: none, is beginning to show only the "start" button, click the "Start", sweepstakes underway, the "Stop" button.
jQuery
We first want to achieve is to click the "Start" button to get the draw data from the background by using ajax ie phone number, and then scroll through the regular phone number is displayed, note that each display cell phone number is random, that is not in accordance with a species in order of appearance, we look at the following code:
	start_btn.click(function(){
		$.getJSON('data.php',function(json){
			if(json){
				//var obj = eval(json);//By eval () function can be converted into JSON string object
				var len = json.length;
				_gogo = setInterval(function(){
					var num = Math.floor(Math.random()*len);
					//var id = obj[num]['id'];
					var id = json[num].id;
					//var v = obj[num]['mobile'];
					var v = json[num].mobile;
					$("#roll").html(v);
					$("#mid").val(id);
				},100);
				stop_btn.show();
				start_btn.hide();
			}else{
				$("#roll").html('System can not find the data source, first import the data.');
			}
		});
		//_gogo = setInterval(show_number,100);
	});});

First, we define the variables, convenient subsequent calls. Then, when you click the "Start" button, the page sends an Ajax request to the background data.php, here we use jqeury of getJSON to complete the asynchronous request. When backstage return json data, we passed through the eval () function can be converted into JSON string object obj, in fact, will be converted to an array of json data. In this case, we use setInterval to make a timer, the timer needs to be done inside is: get an array of obj in a random phone number information, and then displayed on the page. Then every 0.1 running timer, so that to achieve the effect of scrolling display lottery numbers. Also shows the "stop" button, hide the "Start" button, then raffle underway.
Next, look at the "stop" action needs to be done.
   stop_btn.click(function(){ 
        clearInterval(_gogo); 
        var mid = $("#mid").val(); 
        $.post("data.php?action=ok",{id:mid},function(msg){ 
            if(msg==1){ 
                var mobile = $("#roll").html(); 
                $("#result").append("<p>"+mobile+"</p>"); 
            } 
            stop_btn.hide(); 
            start_btn.show(); 
        }); 
    }); 

When you click "Stop" button to end means that the draw. Use clearInterval () function stops the timer, access numbers are drawn id, then select the number by $ .post will be sent to the background data.php handle id. Should be marked as being drawn numbers in the database. If the background process is successful, the winning numbers will be added to the front end of the winning results, while hiding the "Stop" button to display the "start" button, you can raffle again.
Note that we use the setInterval () and clearInterval () to set the timer and stop timer, on the use of these two functions we can google or the next Baidu.
PHP
data.php need to do two things, one, by connecting to the database, read phone number information (not wrapped already winning numbers), and then converted into json format output to the front-end; two, the front end by receiving a request to modify the corresponding database the winning numbers in the state, that identifies the number has been winning, next time will not be as lottery numbers.
include_once('connect.php'); //connect db 
 
$action = $_GET['action']; 
if($action==""){ //read date, return json 
    $query = mysql_query("select * from member where status=0"); 
        while($row=mysql_fetch_array($query)){ 
        $arr[] = array( 
            'id' => $row['id'], 
            'mobile' => substr($row['mobile'],0,3)."****".substr($row['mobile'],-4,4) 
        ); 
    } 
    echo json_encode($arr); 
}else{ //Identification winning numbers
    $id = $_POST['id']; 
    $sql = "update member set status=1 where id=$id"; 
    $query = mysql_query($sql); 
    if($query){ 
        echo '1'; 
    } 
} 

We can see the data sheet member has a field called status, this field is used to identify whether or not winning. 1 indicates that win, 0 for winning. This background php program is operational database, and then returns the corresponding information to the front end.
MYSQL
Finally, the member table structure information attached.
CREATE TABLE `member` ( 
  `id` int(11) NOT NULL auto_increment, 
  `mobile` varchar(20) NOT NULL, 
  `status` tinyint(1) NOT NULL default '0', 
  PRIMARY KEY  (`id`) 
) ENGINE=MyISAM  DEFAULT CHARSET=utf8; 

About lottery program, depending on the needs of different applications have different manifestations. Next we have articles on how to implement in accordance with different probabilities of lottery program, so stay tuned.

Category: PHP Scripts / Javascript

Dear visitor, you are browsing our website as Guest.
We strongly recommend you to register and login to view hidden contents.
<
  • 1 Comment
  • 0 Articles
5 November 2016 11:05

obunga

Reply
  • Group: Members
  • РRegistered date: 5.11.2016
  • Status: Currently Offline
 
Hi bamboo6. I have tried working with the example but could not get mine to run. can you link me up with a download link to see where I'm going wrong?

<
  • 3 Comments
  • 221 Article
23 November 2016 00:22

bamboo06

Reply
  • Group: Administrators
  • РRegistered date: 24.10.2014
  • Status: Currently Offline
 
Quote: obunga
Hi bamboo6. I have tried working with the example but could not get mine to run. can you link me up with a download link to see where I'm going wrong?

You can use save as fun and download them all.

Quote: fauzi
nice coding , and i hope can download it tenks

You can use save as fun and download them all.

<
  • 0 Comments
  • 0 Articles
1 December 2016 04:54

Terungwa

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
How can I set the maximum number of times this script should run. So if i want to accomplish only 6 draws and script should auto stop. please provide clue. Thanks

<
  • 1 Comment
  • 0 Articles
14 December 2016 08:14

ronmi

Reply
  • Group: Members
  • РRegistered date: 14.12.2016
  • Status: Currently Offline
 
did anyone get the script to work? I'm still unable to make it work.

<
  • 0 Comments
  • 0 Articles
20 April 2017 11:03

CesMamEbra

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
Buy Cialis England viagra prescription Best Price For Pfizer Viagra Prix Amoxicillin 100mg cialis price Amoxicillin For Bladder Infection Amoxicillin To Buy Online generic viagra Victoria Bc Viagra Discount Card For Effexor Xr cialis online Discount Pharmacy From Canada Cephalexin To Treat Clamydia viagra Discount Fedex Shipping Acticin Medication No Rx Quand Prendre Baclofene Buy Cialis Viagra En Canada Wellbutrin For Sale viagra Original Kamagra Jelly Purchase Levothyroxine 125mcg Buy Cialis Kamagra Gel Sachets Buying Tretinoin In Singapore cialis Venta Comprar Viagra Buy Clomid And Nolvadex Online viagra Amoxicillin Causes Gallstones Cialis Online Pharmacy cialis price Buy Clomid Without Prescription Uk Amoxicillin For Bladder Infection In Dogs viagra Cialis Per Dimagrire

<
  • 0 Comments
  • 0 Articles
10 August 2017 11:36

Darinsix

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
http://revia.phartesdomusa.org buy low dose naltrexone canada

Information
Comment on the news site is possible only within (days) days from the date of publication.