php - 用php创建网球计分系统
问题描述
当它关闭时,我正在回答下面的问题。一个多小时前我标记它重新开放,但版主似乎在度假,所以这里再次提出问题,这次有答案。另一个 StackExchange 社区的版主向我建议,这是最佳实践。
最初由 Eric Feillant 发布:
我从 php 开始,需要您的建议,因为我的小脚本无法正常工作。
$newpoints1p = $points1p['p1_points_score']; //<-- get it from one MySQL select request.
$AD='AD';
if (isset($_POST['p1poplus']))
{
if ($newpoints1p <= 15 )
{
$newpoints1p += 15; //<-- increment 15 OK
}
else if ($newpoints1p == 30 && $newpoints1p <= 40) { $newpoints1p += 10; } //<-- incrément 10 OK
else if ($newpoints1p <= 50) {$newpoints1p = $AD;} //<-- display AD OK
else $newpoints1p = 0; //<-- Here is the pb, Display 15 as we attempt to have zéro ? because of (AD) string ?
/* Will have the same trouble to décrémentbecause of 'AD' ? */
} if(isset($_POST['p1pomoins']))
{
if ($newpoints1p >= 40)
{
$newpoints1p -= 10;
}
else if ($newpoints1p <= 30 && $newpoints1p > 0) { $newpoints1p -= 15; }
}
解决方案
我查看了你的脚本,但它做的事情比它必须的要复杂得多,所以我决定重写它而不是解决哪里出了问题。下面的代码将成功地做你想做的事。
<?php
// The points you get from the database
$sPoints = "0";
// An array with all the possible scores, with the beginning value at the end for looping
$aPoints = array("0", "15", "30", "40", "AD", "0");
// Find the index in the array
$iPoint_index = array_search ($sPoints, $aPoints);
// Get the index of the next item
$iPoint_index++;
// Return the value of the next item
$sPoints = $aPoints[$iPoint_index];
// display this on the page.
echo $sPoints;
推荐阅读
- graphql - 按映射字段过滤 GraphQL 查询不返回任何结果
- javascript - 当我从 php 回显它时,函数中的 URL 会发生变化
- css - 如何使 Bootstrap Navigation 和 Carousel 以 100vh 组合在一起?
- gatling - 在 Gatling 中,如何将我的测试应用程序监听的 https 端口设置为 9443?
- html - 网站布局仅在 chrome 浏览器上有所不同
- java - 从 firebase 获取数据并添加到卡片视图
- excel - 我应该如何使用 VBA 在 Excel 中创建图形?
- excel - 在两个不同的excel文件vb.net的两个不同列中查找重复数据
- javascript - 无法读取未定义的属性“innerText”?代码结构说明 必填
- r - 来自ggplot的透明.png在文本上有白色的别名边框?