Blogger Information
Blog 50
fans 0
comment 0
visits 31523
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
大乐透与分页边界处理
手机用户1580651468
Original
399 people have browsed it

大乐透与分页边界处理

一. 大乐透的实战

一)选球代码

  1. <?php
  2. // 大乐透球类
  3. define('RED_BALLS', range(1, 35));
  4. define('BLUE_BALLS', range(1, 12));
  5. class DoubleColorBall
  6. {
  7. // 红球
  8. private static $redBalls = RED_BALLS;
  9. // 蓝球
  10. private static $blueBalls = BLUE_BALLS;
  11. // 红球中奖号: 5/35
  12. private static $electRedBalls = [];
  13. // 蓝球中奖号: 2/12
  14. private static $electBlueBalls = [];
  15. // 保存试机号
  16. public static $testNos = [];
  17. // 从33个红球中取出5个红球
  18. private static function createRedBalls()
  19. {
  20. // 1. 35选5
  21. for ($i = 0; $i < 5; $i++) {
  22. // 1. 随机取1个,返回一个key
  23. $key = array_rand(self::$redBalls);
  24. // 2. 将这个球添加到红球数组中
  25. array_push(self::$electRedBalls, self::$redBalls[$key]);
  26. // 3. 将选出的红球从原始的33个红球中删除,剩下的再次随机抽取
  27. array_splice(self::$redBalls, $key, 1);
  28. }
  29. // 2. 排序输出
  30. sort(self::$electRedBalls, SORT_NUMERIC);
  31. }
  32. // 从12个蓝球中取出2个蓝球
  33. private static function createBlueBall()
  34. { // 1. 35选5
  35. for ($i = 0; $i < 2; $i++) {
  36. // 1. 随机取1个,返回一个key
  37. $key = array_rand(self::$blueBalls);
  38. // 2. 将这个球添加到球数组中
  39. array_push(self::$electBlueBalls, self::$blueBalls[$key]);
  40. // 3. 将选出的红球从原始的12个红球中删除,剩下的再次随机抽取
  41. array_splice(self::$blueBalls, $key, 1);
  42. }
  43. // 2. 排序输出
  44. sort(self::$electBlueBalls, SORT_NUMERIC);
  45. }
  46. // 生成试机号
  47. private static function createTestNos($n = 5)
  48. {
  49. for ($i = 0; $i < $n; $i++) {
  50. self::$redBalls = RED_BALLS;
  51. self::$blueBalls = BLUE_BALLS;
  52. self::$electRedBalls = [];
  53. self::$electBlueBalls = [];
  54. // 生成红球
  55. self::createRedBalls();
  56. // 生成蓝球
  57. self::createBlueBall();
  58. // 红蓝球合并生成一个中奖试机号
  59. self::$testNos[] = array_merge(self::$electRedBalls, self::$electBlueBalls);
  60. }
  61. }
  62. // 获取试机号,供外部调用(外部只需要访问它,也就是只有它是公开的)
  63. public static function getTestNos($n = 5)
  64. {self::createTestNos($n);
  65. return self::$testNos;
  66. }
  67. }

二)展示html代码

  1. <?php
  2. require __DIR__ . '/Lotto.php';
  3. // 生成数量应该从当前URL中获取
  4. // $n = isset($_GET['n']) ? $_GET['n'] : 5;
  5. // null合并运算符
  6. $n = $_GET['n'] ?? 5;
  7. // print_r($n);
  8. $allBalls=DoubleColorBall::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> <table><tbody>
  27. <?php foreach ($allBalls as $key=> $balls): ?>
  28. <tr>
  29. <!-- 第一列是序号: 默认从1开始 $key+1 -->
  30. <td><?=$key+1?></td>
  31. <?php foreach ($balls as $ball) : ?>
  32. <td><?=$ball?></td>
  33. <?php endforeach; ?>
  34. </tr>
  35. <?php endforeach ?>
  36. </tbody>
  37. </table>
  38. <p>福利彩票大家买,众手托起幸福来</p>
  39. </div>
  40. <script>
  41. function isVip(input) {
  42. if (input.value > 10) {
  43. alert('免费用户只能看10个,看更多升级到VIP,现在升级8折');
  44. input.value = 10;
  45. input.max = 10;
  46. location.reload(true);
  47. return false;
  48. }
  49. }
  50. </script>
  51. </body>
  52. </html>

三)做好的效果图

二. 为分页添加”上一页”,”下一页”功能,边界处理

一)边界处理的关键代码

  1. <p>
  2. <?php
  3. //上一页的小于0的判断
  4. $pages1 = ($page - 1 > 0) ? ($page - 1) : 1;
  5. $pages2 = $_SERVER['PHP_SELF'] . '?page='.$pages1;
  6. ?>
  7. //
  8. <a href="<?= $pages2 ?>">上一页</a>
  9. //
  10. <?php for ($i = 1; $i <= $pages; $i++) : ?>
  11. <?php $url = $_SERVER['PHP_SELF'] . '?page='. $i;
  12. $style = 'style="background:red"';
  13. // $i 是当前的页码, $_GET['page']相同时才是要高亮的页码
  14. $active = ($i == @$_GET['page']) ? $style : null;
  15. ?> <a href="<?= $url ?>" <?= $active ?>><?= $i ?></a> <?php endfor ?>
  16. //
  17. <!-- 下一页的超出页码的判断 -->
  18. <?php
  19. $pages3=($page + 1 < $pages)?($page + 1):$pages;
  20. $pages4 = $_SERVER['PHP_SELF'] . '?page=' .$pages3;
  21. ?>
  22. <a href="<?= $pages4 ?>">下一页</a></p>

二)处理后的效果图

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