Blogger Information
Blog 26
fans 0
comment 0
visits 11924
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php实现体彩七星彩源码;为分页添加"上一页","下一页"功能,含边界处理
高空中的云
Original
847 people have browsed it

php体彩七星彩源码


跟双色球的区别就两个地方:

  1. 七星彩是数字随机范围是每次从0~9之间选一个数字,重复6次;而双色球是从33个数字中每次选走1个,然后再从剩余的数字中选一个,重复6次.
  2. 七星彩最后一个数字是0~14选1,双色球是1~16选1
    因此,双色球代码要改动的是,每次取完数字,不需要把该数字移除,选过之后无需排序.
  1. <?php
  2. define('RED_BALLS', range(0, 9));
  3. define('BLUE_BALLS', range(0, 14));
  4. class SevenStarsBall
  5. {
  6. // 红球
  7. private static $redBalls = RED_BALLS;
  8. // 蓝球
  9. private static $blueBalls = BLUE_BALLS;
  10. // 红球中奖号: 1/10, 重复6次
  11. private static $electRedBalls = [];
  12. // 蓝球中奖号: 1/15
  13. private static $electBlueBall;
  14. // 保存试机号
  15. public static $testNos = [];
  16. // 从33个红球中取出6个红球
  17. private static function createRedBalls()
  18. {
  19. // 1. 33选6
  20. for ($i = 0; $i < 6; $i++) {
  21. // 1. 随机取1个,返回一个key
  22. $key = array_rand(self::$redBalls);
  23. // 2. 将这个球添加到红球数组中
  24. array_push(self::$electRedBalls, self::$redBalls[$key]);
  25. // 3. 将选出的红球从原始的33个红球中删除,剩下的再次随机抽取
  26. // 重复操作6次,所以不用移除
  27. // array_splice(self::$redBalls, $key, 1);
  28. }
  29. // 2. 排序输出
  30. // 也不用排序
  31. // sort(self::$electRedBalls, SORT_NUMERIC);
  32. }
  33. // 从16个蓝球中取出1个蓝球
  34. private static function createBlueBall()
  35. {
  36. self::$electBlueBall = array_rand(array_flip(self::$blueBalls));
  37. }
  38. // 生成试机号
  39. private static function createTestNos($n = 5)
  40. {
  41. for ($i = 0; $i < $n; $i++) {
  42. self::$redBalls = RED_BALLS;
  43. self::$blueBalls = BLUE_BALLS;
  44. self::$electRedBalls = [];
  45. // 生成红球
  46. self::createRedBalls();
  47. // 生成蓝球
  48. self::createBlueBall();
  49. // 红蓝球合并生成一个中奖试机号
  50. self::$testNos[] = array_merge(self::$electRedBalls, [self::$electBlueBall]);
  51. }
  52. }
  53. // 获取试机号,供外部调用(外部只需要访问它,也就是只有它是公开的)
  54. public static function getTestNos($n=5)
  55. {
  56. self::createTestNos($n);
  57. return self::$testNos;
  58. }
  59. }

前端代码

  1. <?php
  2. require __DIR__ . '/sevenstars.php';
  3. // 生成数量应该从当前URL中获取
  4. // $n = isset($_GET['n']) ? $_GET['n'] : 5;
  5. // null合并运算符
  6. $n = $_GET['n'] ?? 5;
  7. // print_r($n);
  8. $allBalls=SevenStarsBall::getTestNos($n);
  9. // print_r(DoubleColorBall::$testNos);
  10. ?>
  11. <!DOCTYPE html>
  12. <html lang="zh-CN">
  13. <head>
  14. <meta charset="UTF-8" />
  15. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  16. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  17. <title>生成七星彩中奖试机号</title>
  18. <link rel="stylesheet" href="style.css" />
  19. </head>
  20. <body>
  21. <div class="lottery">
  22. <h2>七星彩试机号</h2>
  23. <form>
  24. <input type="number" name="n" min="1" value="<?=$_GET['n'] ?? 5?>" onchange="isVip(this)">
  25. <button>生成试机号</button>
  26. </form>
  27. <table>
  28. <tbody>
  29. <?php foreach ($allBalls as $key=> $balls): ?>
  30. <tr>
  31. <!-- 第一列是序号: 默认从1开始 $key+1 -->
  32. <td><?=$key+1?></td>
  33. <?php foreach ($balls as $ball) : ?>
  34. <td><?=$ball?></td>
  35. <?php endforeach; ?>
  36. </tr>
  37. <?php endforeach ?>
  38. </tbody>
  39. </table>
  40. <p>福利彩票大家买,众手托起幸福来</p>
  41. </div>
  42. <script>
  43. function isVip(input) {
  44. if (input.value > 10) {
  45. alert('免费用户只能看10个,看更多升级到VIP,现在升级8折');
  46. input.value = 10;
  47. input.max = 10;
  48. location.reload(true);
  49. return false;
  50. }
  51. }
  52. </script>
  53. </body>
  54. </html>

列表分页

  • 边界处理
  1. <a href="<?=$_SERVER['PHP_SELF']. '?page='. ( ($page-1) > 1 ? ($page-1) : 1 ) ?>">上一页</a>
  2. <a href="<?=$_SERVER['PHP_SELF']. '?page='. ( ($page+1) < $pages ? ($page+1) : $pages )?>">下一页</a>
  • 中间隐藏页码的简单原理
    分情况考虑总页码数量,例如:
  1. pages == 1, 则不显示页码,不显示上一页下一页
  2. 1<pages<=10,则全部显示
  3. 总页码数量超过10
    3.1 默认只显示前3个页码和后3个页码,显示上一页下一页,其他页码用…代替
    3.2 如翻页,则按顺序额外显示当前页码相邻3个页码,其他不变
Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:完成的不错
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!