检查Variable是否小于其他,以及哪个小于。 PHP

时间:2016-05-14 16:21:11

标签: php

我一直在研究我的评分系统并且来了这个。 我有4个变种。

var nflTeams = new Bloodhound({
  datumTokenizer: Bloodhound.tokenizers.obj.whitespace("team"),
  queryTokenizer: Bloodhound.tokenizers.whitespace,
  local: [{
    "team": "San Francisco 49ers",
    "id": "49ers"
  }, {
    "team": "Chicago Bears",
    "id": "Bears"
  }, {
    "team": "Cincinnati Bengals",
    "id": "Bengals"
  }, {
    "team": "Buffalo Bills",
    "id": "Bills"
  }, {
    "team": "Denver Broncos",
    "id": "Broncos"
  }, {
    "team": "Cleveland Browns",
    "id": "Browns"
  }, {
    "team": "Tampa Bay Buccaneers",
    "id": "Buccaneers"
  }, {
    "team": "Arizona Cardinals",
    "id": "Cardinals"
  }, {
    "team": "San Diego Chargers",
    "id": "Chargers"
  }, {
    "team": "Kansas City Chiefs",
    "id": "Chiefs"
  }, {
    "team": "Indianapolis Colts",
    "id": "Colts"
  }, {
    "team": "Dallas Cowboys",
    "id": "Cowboys"
  }, {
    "team": "Miami Dolphins",
    "id": "Dolphins"
  }, {
    "team": "Philadelphia Eagles",
    "id": "Eagles"
  }, {
    "team": "Atlanta Falcons",
    "id": "Falcons"
  }, {
    "team": "New York Giants",
    "id": "Giants"
  }, {
    "team": "Jacksonville Jaguars",
    "id": "Jaguars"
  }, {
    "team": "New York Jets",
    "id": "Jets"
  }, {
    "team": "Detroit Lions",
    "id": "Lions"
  }, {
    "team": "Green Bay Packers",
    "id": "Packers"
  }, {
    "team": "Carolina Panthers",
    "id": "Panthers"
  }, {
    "team": "New England Patriots",
    "id": "Patriots"
  }, {
    "team": "Oakland Raiders",
    "id": "Raiders"
  }, {
    "team": "St.Louis Rams",
    "id": "Rams"
  }, {
    "team": "Baltimore Ravens",
    "id": "Ravens"
  }, {
    "team": "Washington Redskins",
    "id": "Redskins"
  }, {
    "team": "New Orlean Saints",
    "id": "Saints"
  }, {
    "team": "Seattle Seahawks",
    "id": "Seahawks"
  }, {
    "team": "Pittsburgh Steelers",
    "id": "Steelers"
  }, {
    "team": "Houston Texans",
    "id": "Texans"
  }, {
    "team": "Tennesse Titans",
    "id": "Titans"
  }, {
    "team": "Minnesota Vikings",
    "id": "Vikings"
  }]
})

var adapter = nflTeams.ttAdapter();

$("#default-suggestions .typeahead").typeahead({
    hint: true,
    minLength: 1,
    highlight: true
  }, {
    name: "nfl-teams",
    display: "value",
    source: adapter,
    templates: {
      suggestion: function(data) {
        return "<li>" + data.team + "</li>"
      }
    }
  })
  .on("typeahead:render", function(e, data) {
    console.log(e, data);
    $(".tt-hint").attr("placeholder", data.id)
    .css("left", "calc(" + e.target.value.length * 7 + "px)")
  })
  .on("input", function() {
    if (this.value === "" || /^\s+$/.test(this.value)) {
      $(".tt-hint").attr("placeholder", "")
    }
  })

我想知道新分数是否低于其他3分,如果是,那么哪些分数。评分系统要求您获得尽可能低的分数。

我有以下代码:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="http://twitter.github.io/typeahead.js/releases/latest/typeahead.bundle.js"></script>

<div id="default-suggestions">
  <input class="typeahead" type="text" placeholder="NFL Teams">
</div>

但我想知道的是,如果我必须继续使用所有这些if语句,还是有更短更简单的东西?我需要替换它小于的分数,而不是大于它的分数。得分1 2和3都是球员的统计数据。如果我必须继续使用所有if语句,那么代码将如何显示(它让我的逻辑变得莫名其妙)?

5 个答案:

答案 0 :(得分:0)

您可以执行以下操作:

编辑:当您使用数据库时,您将执行与此类似的查询:

SELECT scores FROM scores_table将表名和列名替换为相应的数据。

$scores = [$score1, $score2] // add as many as you like

$new_score = $scores[0]; // assign a baseline
foreach ($scores as $score) {
    if ($score < $new_score) {
        $new_score = $score;
    } 
 } 

希望有所帮助。

答案 1 :(得分:0)

你应该使用数组

$scores = array(8, 15, 10); //previous scores

$new_score = 5;

$new_score_smallest = true;

foreach($scores as $score) {
 if($score < $new_score) {
  $new_score_smallest = false;
 }
}

if($new_score_smallest) {
 echo "Best score!";
}
else {
 echo "Not the best score :(";
}

如果你只想记住3个最佳成绩:

$scores = array(5, 6, 8);
$new_score = 7;

for($i = 0; $i < count($scores); $i++) {
    if($new_score < $scores[$i]) {
        $scores[$i] = $new_score;
        break;
    }
}

答案 2 :(得分:0)

您可以使用array_searchmin

$newscore = 2;
$scores = array($score1, $score2, $score3);
if($newscore < min($scores)){
$scores[array_search(min($scores), $scores)] = $newscore;
}
如果$score低于

数组$newscore最低分将会更新

答案 3 :(得分:0)

<?php

$newscore = 78;
$score1 = 23;
$score2 = 201;
$score3 = 107;

$max = max([$score1, $score2, $score3]);

if ($max < $newscore) {
    echo "New best score ! ({$newscore})";
} else {
    echo "Not the best score !\nCurrent: {$newscore}\nBest: {$max}";
}

答案 4 :(得分:0)

将您的分数放在数组或任何可迭代的内容中(如查询结果,无论您使用的是PDO还是mysqli)。

让我们说你的分数在$scores数组中(例如,如果$score是PDOStatement则会相同)并且有序(在查询中使用ORDER ASC) :

$scores = [105, 201, 305];

$newscore = '186';
$hiscore = false;
$beaten = [];

foreach ($score as $k => $score) {
    if ($newscore > $score) {
        $hiscore = true;
        $beaten[] = $score;
        unset($scores[$k]);
    } 
}

if ($hiscore) {
    echo 'New high score!'.PHP_EOL;
    echo 'Better than '.implode(', ', $beaten).PHP_EOL;
    echo 'But not better than '.implode(', ', $scores);
} else {
    echo 'Try harder!';
}