2011-10-27 26 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沒有編程技能是很難的。但是可能的。