2013-10-29 260 views
0

enter image description here enter image description herePHP for()循環問題

我有我一直在堅持了好幾個小時的問題。我玩過很多類型的for()和while()循環。我把它們放在不同的變量不同的位置,跑不同的東西,沒有什麼工作..

我的問題:

爲什麼我的程序給下面的第一個所有用戶都在同一水平?你可以在圖片中清楚地看到Nicolas擁有更多的XP。 (5,000 xp是等級20,如果數據庫中第一個是「Nic5」,那麼它會將「技能等級」更改爲20.

我知道返回的變量$lvl對於加載的每個玩家這就是爲什麼每個玩家是獲得第一個玩家的等級

任何人可以幫助我這個請

注:? 0 =保存了玩家技能等級的經驗一欄

類calculatelevel

class calculatelevel { 

function level($skillnum) 
{ 

$host = "*"; 
$user = "*"; 
$pass = "*"; 
$db = "*"; 

$con = new mysqli($host, $user, $pass, $db) or die($con->error); 
$res = $con->query("SELECT `0` FROM hiscores"); 

$max = 99; 

while($row = $res->fetch_assoc()) { 
    $xp = $row['0']; 

    // Find the appropriate level 
    for ($lvl = 1; $lvl < $max; $lvl++) //this for loop runs 99 times 
    { 
     if ($xp < $this->experience($lvl))//if players xp in skill is less than experience(level 1-99) 
     { 
      // Level found 
      $lvl -= 1; 
      break; 
     } 
    } 
} 
     return $lvl; 
} 

public function experience($lvl) 
{ 
    $xp = 0; 
    for($x = 1; $x < $lvl; $x++) 
    { 
     $xp += floor($x + 300 * pow(2, ($x/7))); 
    } 
    return floor($xp/4); 
} 
} 

方法寫入數據庫信息的頁面。

if($res->num_rows > 0) { 
    echo "<table> 
    <tr> 
     <td>Rank</td> 
     <td>Username</td> 
     <td>Skill Level</td> 
     <td>Total Exp</td>   </tr>"; 

while($row = $res->fetch_assoc()) { 
    echo 'ran'; 
    $calc = new calculatelevel(); 
    $level = $calc->level(0); 

    echo '<tr> 
     <td>'.($count+1).'</td> 
     <td>'. htmlspecialchars($row['username']) .'</td> 
     <td>'.number_format($level).'</td> 
     <td>'.number_format($row['0']).'</td> 
     </tr>'; 
    $count++; 
} 
} 
+2

我不知道你在做什麼:「SELECT'0' FROM hiscores」 - 有一個名爲'0'的字段似乎是我要求的麻煩。 – 2013-10-29 02:59:07

+0

0 =擁有玩家技能水平經驗的表格。 – elune

+0

0不重要,但是,洗脫是正確的。 – Nic

回答

0

您的while循環和for循環的結構不正確。您只查看第一行並每次返回該行的級別。當您找到該行玩家的等級時,您可以使用for循環,然後立即返回該等級。結果:看起來每個人都有相同的等級。

編輯:好的,這裏有一些更多的想法。

首先,名爲0的列正在尋求麻煩,正如Mike W指出的那樣。你說0是一個表,但如果是這樣的話,你的SELECT聲明沒有意義。我會嘗試的第一件事是將列名更改爲不是數字的東西,如xp

其次,如果可能的話,你應該只做一個數據庫連接並在整個請求中使用它。每次運行特定功能時都會打開一個新連接,以便快速綁定服務器。

第三,在當前的代碼中的明顯的問題是這樣的:

功能水平($ skillnum){

// other code here.... 

    // Okay, you load a row's data into $row, with the idea that you will repeat this. 
    while($row = $res->fetch_assoc()) { 
     // $xp is set to the experience points for the user you just loaded 
     $xp = $row['0']; 

     // You now look at each level, starting at 1, to see if the 
     // user's xp is greater than the cutoff for that level 
     for ($lvl = 1; $lvl < $max; $lvl++) //this for loop runs 99 times 
     { 
      // If the user's xp is less than the cutoff... 
      if ($xp < $this->experience($lvl))//if players xp in skill is less than experience(level 1-99) 
      { 
       // ... then you go back down one level... 
       // Level found 
       $lvl -= 1; 
       // ... and quit the for loop! 
       break; 
      } 
     } 
     // okay, you're out of the for loop, so you go back to the while loop... 
     // a new row is loaded... 
     // and $xp and $lvl are both overwritten with that user's values 
    } 

    // So, the while loop has run once for each player... 
    // ... but you are only returning one player's level! 
    return $lvl; 
} 

而且,你已經定義calculatelevel::level需要一個參數$skillnum,但你永遠不使用它在代碼發佈在這裏。

我懷疑你的真實代碼會有另一個小故障,導致它返回第一個玩家的等級,而不是最後一個玩家的等級。這可能是0列名稱的問題; 真的應該改變。

+0

你介意給我一個更具體的修復?正如我所說,我一直堅持這個很長一段時間。 – Nic

+0

我非常難過。請幫我... – Nic

+0

0是一列,而不是一張表。我犯了一個錯字。我取代了休息時間;與返回$ lvl;希望不是返回最後一列,而是返回包含其所在列的播放器XP的$ lvl。一旦它返回$ lvl我需要停止while循環並將該用戶寫入頁面。然後我需要繼續while循環。我相信那是我必須做的,我該怎麼做?此外,關卡功能需要一個參數,因爲它最終可以運行它的技能。但是,現在我只測試一種技能。 – Nic