PHP + Ajax mobile phone red packet program example-PHP source code

Source: Internet
Author: User
PHP + Ajax has many functions that will be used in its small series. Today we have a login function implemented using PHP + Ajax. Let's look at an example of a PHP + Ajax mobile phone red packet sending program, the details are as follows. PHP + Ajax has many functions that will be used in its small series. Today we have a login function implemented using PHP + Ajax. Let's look at an example of a PHP + Ajax mobile phone red packet sending program, the details are as follows.

Script ec (2); script

PHP red packet sending process: after entering the number and total amount of red packets, PHP will randomly allocate each amount based on the two values to ensure that everyone can receive a red packet, the amount of each red packet varies. That is, the amount of red packets received by each person must be different, and the total amount of all red packets is equal to the total amount.

Php red envelope implementation principle:

Set the total amount to 10 yuan, and N people will receive them at random:

N = 1 first

The amount of the red packet is x RMB;

N = 2 second

To ensure that the second red envelope can be normally issued, the first red envelope amount is a random number between 0.01 and 9.99.

The second red envelope = 10-The amount of the first red envelope;

N = 3 third

Red packet 1 = a random number between 0.01 and 9.99

Red packet 2 = a random number between 0.01 and (10-red packet 1-0.01)

Red packet 3 = 10-red packet 1-red packet 2

......

Therefore, we get a rule that, when allocating the current amount of red packets, we reserve the minimum amount required for the remaining red and white, and then take a random number between 0.01 and the total amount-the reserved amount, the random number is the amount allocated to the current red packet.

In actual application, the program first allocates the red envelope amount, that is, when the red envelope is issued, the number of red envelopes and the amount of each red envelope are allocated, then when the user grabs the red envelope, we can return a random red envelope to the user.
JQuery code:

$ (Function (){
$ ("Button"). click (function (){
$. Ajax ({
Type: 'post ',
Url: 'bao. php ',
DataType: 'json ',
BeforeSend: function (){
$ ("# Result" 2.16.html ('allocating hongba ');
},
Success: function (json ){
If (json. msg = 1 ){
Var str = '';
Var res = json. res;
$. Each (res,
Function (index, array ){
Str + ='

The '+ array [' I '] +' red packets,
Amount '+ array ['money'] +' yuan, balance '+
Array ['Total'] + 'yuan

';
});
$ ("# Result" example .html (str );
} Else {
$ ("# Result" pai.html ('data error! ');
}
}
});
});
});
PHP code: bao. php

$ Total = 20; // total amount of Red Packets
$ Num = 10; // 10 red packets, which can be randomly received by 10 people
$ Min = 0.01; // each person can receive at least 0.01 yuan

For ($ I = 1; $ I <$ num; $ I ++)
{
$ Safe_total = ($ total-($ num-$ I) * $ min)/($ num-$ I); // random security Upper Limit
$ Money = mt_rand ($ min * 100, $ safe_total * 100)/100;
$ Total = $ total-$ money;

Echo 'quarter '. $ I.' red envelopes: '. $ money.' yuan, balance: '. $ total.' yuan ';
}
Echo 'quarter '. $ num.' red packet: '. $ total.' yuan, balance: 0 yuan ';

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.