2017-10-07 97 views
0

我有一個for循環來循環訪問數組,然後運行與每個元素相關的數據庫查詢,然後調用一個打印出與其相關的東西的函數。該數組的長度爲12個元素,但for循環永遠不會超過元素0.它沒有錯誤或失敗,它只是在第一個元素後沒有做任何事情。我證實,通過將echo $x;echo $vendorsname[$x];置於每個循環週期的開始,並且足夠肯定,它只能將0回顯到頁面。PHP For Loop只運行一次

$continuetill = count($vendorsname); 

for ($x = 0; $x < $continuetill; $x++) 
{ 

echo $x; 
echo $vendorsname[$x]; 

$sql="SELECT low,mid,high,verlow,vermin,verhigh FROM vendors WHERE vendor = ".$x." ORDER BY id DESC LIMIT 1"; 

if ($result=mysqli_query($conn,$sql)) 
    { 
    // Fetch one and one row 
    while ($row=mysqli_fetch_row($result)) 
    { 
     $low = $row[0]; 
     $mid = $row[1]; 
     $high = $row[2]; 
     $verlow = $row[3]; 
     $vermid = $row[4]; 
     $verhigh = $row[5]; 



     if(($low > $mid) && ($low > $high)) 
     { 
     likely295Message($vendorsname[$x]); 
     } 
     elseif (($high > $low) && ($high > $mid) && ($high < 15)) 
     { 
     possibly300Message($vendorsname[$x]); 
     } 
     elseif (($high > $low) && ($high > $mid) && ($high >= 15)) 
     { 
     likely300Message($vendorsname[$x]); 
     } 
     elseif (($mid > $low) && ($mid > $high)) 
     { 
     likely296Message($vendorsname[$x]); 

     }else 
     { 
     unknownMessage($vendorsname[$x]); 
     } 


     if(($verlow != 0) || ($vermid != 0) || ($verhigh != 0)) 
     { 
     if(($verlow > $vermid) && ($verlow > $verhigh)) 
     { 
      verified295Message($vendorsname[$x]); 
      changeBackgroundBack($vendorsname[$x]); 
      changeImage($vendorsname[$x]); 

     } 
     elseif (($verhigh > $verlow) && ($verhigh > $vermid)) 
     { 
      verified300($vendorsname[$x]); 
      changeBackground($vendorsname[$x]); 
      changeImage($vendorsname[$x]); 
     } 
     elseif (($vermid > $verlow) && ($vermid > $verhigh)) 
     { 
      verified296($vendorsname[$x]); 
      changeBackgroundBack($vendorsname[$x]); 
      changeImage($vendorsname[$x]); 
     } 
     } 

    } 

    mysqli_free_result($result); 
} 
} 
+0

從循環中的數據庫迭代獲取是錯誤的。在你的情況下,最好做這樣的事情:'$ sql =「SELECT low,mid,high,verlow,vermin,verhigh FROM vendor WHERE vendor IN(」.implode(',',range(0,count($) )。「)ORDER BY id DESC」;' – MAZux

+0

確保'$ continuetill'的值,如果它是'0',那麼它會爲'$ x'打印'0',你可以使用'foreach()'循環爲你的陣列 –

+0

@MofiqulIslm是的,我已經驗證了它的價值,它是一個錯誤拼寫的函數,我錯過了在腳本的頂部打開錯誤報告 – jskrwyk

回答

0

確保您打開了錯誤顯示。在腳本開頭添加:

ini_set('display_errors', 1); 

確保您沒有任何錯誤。

+0

循環中運行的sql語句和mysqli_query它應該創建該語句,並對數組中的每個元素運行查詢,以便每次循環都運行它 – jskrwyk

+0

對不起,我誤讀了,我以爲問題是用'while' –

+0

是的三重檢查。但即使它們不存在,它仍然應該打印該回聲$ x; for for循環的每個循環都只有print 0,所以只運行一次。 – jskrwyk