我有一個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);
}
}
從循環中的數據庫迭代獲取是錯誤的。在你的情況下,最好做這樣的事情:'$ sql =「SELECT low,mid,high,verlow,vermin,verhigh FROM vendor WHERE vendor IN(」.implode(',',range(0,count($) )。「)ORDER BY id DESC」;' – MAZux
確保'$ continuetill'的值,如果它是'0',那麼它會爲'$ x'打印'0',你可以使用'foreach()'循環爲你的陣列 –
@MofiqulIslm是的,我已經驗證了它的價值,它是一個錯誤拼寫的函數,我錯過了在腳本的頂部打開錯誤報告 – jskrwyk