首页 > 解决方案 > 用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

解决方案


我查看了你的脚本,但它做的事情比它必须的要复杂得多,所以我决定重写它而不是解决哪里出了问题。下面的代码将成功地做你想做的事。

<?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;

推荐阅读