2011-10-27 28 views
-1

我有以下代码:如果,否则,如果,否则停在中间别人的

$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' />";,因此我不想直接回显该值。

有没有解决方法呢?

+2

'小于或等于'符号实际上是<=',而不是'= <',尽管您没有得到解析错误的事实表明您的版本可能也适用 - 但最好做正确的事情......这可能只是你有这个问题的原因。 – DaveRandom

+0

此外,您的'$ {db_name}'的正确语法实际上是'{$ db_name}' - 您的版本可以工作,但会抛出'E_NOTICE',因为PHP正在搜索一个名为'db_name'的未定义常量,找不到一个并使用该名称的字符串值。在这种情况下,你实际上不需要花括号,只需要'$ db_name'就可以完成这项工作。您只需要关联数组,多维数组,变量变量和链接对象属性的复杂语法(如果我忘记了任何东西,请随时纠正我) – DaveRandom

+0

另外,您正在执行'$ row = mysql_fetch_array($ result,MYSQL_NUM)',但是您不要在任何地方使用'$ row' - '$ player'从哪里来? '$ ships_killed'从哪里来? – DaveRandom

回答

1

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

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

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

特斯罗。

+0

特斯洛,该领域getts相当大,这就是为什么我想添加一个更大或相等。我没有其他领域与它进行比较。 – user631756

+0

交换机可能会工作。我会试试看。谢谢 – user631756

+0

@ user631756仅供参考,您仍然可以使用带'switch'的'> =' - 您可以做'switch(TRUE){case $ player [$ ships_killed]> = 10:// do stuff}'并且它可以工作。查看[switch'手册页](http://uk.php.net/manual/en/control-structures.switch.php)上的一些评论 - 它们展示了这种结构的多功能性是。 – DaveRandom

7

不等于这样写的<=,先生。

+0

即使调整后的脚本仍然以4结尾。我已经测试脚本来回应$ player [$ ships_killed]中的值,并根据我的$行给出了正确的数字。 – user631756

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

} 
+0

先生,我真的很感谢你的努力,但是你正试图塑造我的剧本到你的想法,剧本应该是什么,但不是。以上给我一个错误。在问题中的脚本是否功能完整,我只是要求解决else else if语句而不是完全重写。再次感谢您的努力。 – user631756

1

一点更智能的方式

$kills = $player[$ships_killed]; 

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

为您

编号值最终会替换为一个图像,如回声“”,因此我不想回显值directl年。

这个说法没有意义。你可以通过使用变量来设置正确的图片名称,从而更智能地完成这部分任务。你必须学习编程。使用PHP没有编程技能是很难的。但是可能的。