PHP combined with Ajax to achieve a mobile phone red envelopes Case _php Example

Source: Internet
Author: User

PHP Ajax has a lot of features will be used in its small series today there is a use of PHP ajax implementation of a red envelope function, the following we look at a PHP Ajax mobile phone to send red envelopes of the program examples, as shown below.

The basic flow of red envelopes in PHP: When the amount of red envelopes and the total amount of input, PHP will be based on these two values to randomly allocate each amount, to ensure that everyone can receive a red envelope, and the amount of each red envelopes vary. That is, the amount of red envelopes each person receives is different, and the total amount of all red envelopes equals the total amount.

PHP Red Envelopes To achieve the principle:

Set the total amount of 10 yuan, there are n individuals to collect randomly:
N=1 First
The red envelope amount =x yuan;
N=2 a second
To ensure that the second red envelope can be issued normally, the first red envelope amount = a random number between 0.01 and 9.99
The second red envelope =10-the first red envelope amount;
N=3 a third.
A random number between red envelopes 1=0.01 to 9.99
A random number of red envelopes 2=0.01 to (10-red envelope 1-0.01)
Red Envelopes 3=10-Red Envelopes 1-Red Envelopes 2
......

So we get a rule, in allocating the current red envelope amount, first reserve the remaining red and white required the minimum amount, and then in 0.01 to the total amount-reserve amount between the random number, the resulting random number is the current amount of red envelopes allocated.

In practical application, the program will first allocate the amount of red envelopes, that is, when the red envelopes, the number of red envelopes and the amount of each red packets are allocated, then the user to rob red envelopes, we randomly return a red envelope to the user.

jquery Code:

$ (function () { 
 $ ("button"). Click (function () { 
 $.ajax ({ 
 type: ' POST ', 
 URL: ' bao.php ', 
 dataType: ' JSON ', 
 beforesend:function () { 
 $ ("#result"). HTML (' Allocating red envelopes '); 
 }, 
 success:function (JSON) { 
 if (json.msg = 1) { 
  var str = '; 
  var res = json.res; 
  $.each (res, 
  function (index, array) { 
  str = ' <p> <span> ' array[' i '] ' </span> a red envelope,
  amount <span> ' array['] ' </span> yuan, balance <span> ' 
  array[' total '] ' yuan </span></p> '; 
  $ ("#result"). html (str); 
 else { 
  $ (' #result '). HTML (' data Error! '); 
 } 
 } 
 }); 
 }); 
});


PHP Code: bao.php

$total =20;//Red Envelope Total amount 
$num =10;//is divided into 10 red envelopes, supports 10 people 
to receive at random $min =0.01;//each person receives at least 0.01 yuan for 
 
($i =1; $i < $num; $i) 
{ 
 $safe _total= ($total-($num-$i) * $min)/($num-$i);//The Random security upper bound 
 $money =mt_rand ($min *100, $safe _total*100) /100; 
 $total = $total-$money; 
 
 Echo ' $i. ' A red envelope: '. $money. ' Yuan, balance: '. $total. ' Yuan '; 
} 
Echo ' $num. ' A red envelope: '. $total. ' Yuan, balance: 0 Yuan ';


The effect is as shown in the figure:

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.