如果,否则如果,否则停在elses中间

时间:2011-10-27 09:48:36

标签: php if-statement

我有以下代码:

$result = mysql_query("select * from ${db_name}_users limit 1");
while ($row = mysql_fetch_array($result, MYSQL_NUM)) 
{
    if ($player[$ships_killed] == 1) 
        echo "1";
    else if ($player[$ships_killed] == 2) 
        echo "2";
    else if ($player[$ships_killed] == 3) 
        echo "3";
    else if ($player[$ships_killed] == 4) 
        echo "4";
    else if ($player[$ships_killed] == 5) 
        echo "5";
    else if ($player[$ships_killed] =< 10) 
        echo "10";
    else if ($player[$ships_killed] =< 15) 
        echo "15";
    else if ($player[$ships_killed] =< 20) 
        echo "20";
    else 
        echo "Over Range";    
}   

我对less than or equal标志感到困难,但它没有显示正确的值。例如,当字段显示“11”时,它改为回响“超范围”。

我的问题是特定字段增长很多,我不能用等于大小写覆盖每一个值。

编号值最终将替换为echo "<img src='img/badges/1i.png' />";等图像,因此我不想直接回显该值。

这有解决方法吗?

5 个答案:

答案 0 :(得分:7)

小于等于写成<=,先生。

答案 1 :(得分:1)

您已切换=<符号,它应该是这样的:<=>=。有关详细信息:http://php.net/manual/en/language.operators.comparison.php

顺便说一下,我认为你最好在这里使用switch声明。

答案 2 :(得分:1)

第一个问题是=<符号,这是错误的。请尝试使用<=

此外,您可以使用switch语句,也可以使用计数器,而不是使用嵌套if。

switch ($player[$ships_killed])
{
    case "1": echo "1";
    ...
}

Teslo。

答案 3 :(得分:1)

以下是我认为您的代码应该是:

// I assume you have a DB called '$db_name' and it has a table called 'users'
// If that's not the case, that's probably what it should be
if (!$result = mysql_query("SELECT * FROM $db_name.users LIMIT 1")) exit('MySQL query error!');

while ($row = mysql_fetch_assoc($result)) {

  // Anything in this array, we echo the exact number
  $exactMatches = array(1,2,3,4,5);

  // I assume your actually want to use $row['ships_killed'] to compare,
  // otherwise there is no apparent point to your database query...
  if (in_array($row['ships_killed'],$exactMatches)) {
    // Echo the number and skip to the next row
    // There is only one row at the moment, but your query is so simple
    // that I presume it is not finished
    echo $row['ships_killed'];
    continue;
  }

  // I would have though you want to display the number below the actual number,
  // not the one above it. For example if I have killed 8, you would show 5, not
  // 10 - the code below reflects this

  // If we get this far, there was no exact match
  if ($row['ships_killed'] >= 20) echo "20";
  else if ($row['ships_killed'] >= 15) echo "15";
  else if ($row['ships_killed'] >= 10) echo "10";
  else echo "5";

}

答案 4 :(得分:1)

更聪明的方法

$kills = $player[$ships_killed];

if($kills] < 6) echo $kills;
elseif($kills < 21) echo ceil($kills/5)*5;
else  echo "Over Range";

至于你的

  

编号值最终将被替换为echo“”等图像,因此我不想直接回显该值。

这句话毫无意义。您也可以使用变量来设置正确的图片名称。你必须学习编程。在没有编程技巧的情况下使用PHP有点困难。无论如何。