我有一个在php游戏中使用的函数,其中有一个for循环检查房间id。基本上,for循环只运行一次,它应该运行两次。下面的代码块说明了(第一个带有$ i的循环):
// successful sql login & $player is correctly set with '', and all below sql
// statements execute correctly. $count is 2
function setMonstersLocation($player,$rooms_id){
if (mysql_result(mysql_query("SELECT state FROM present WHERE player = $player AND items_id IS NULL AND monsters_id IS NULL AND quantity IS NULL AND rooms_id IS NOT NULL"),0) == 0){
$sql = mysql_query("SELECT COUNT(*) FROM present WHERE player = $player AND items_id IS NULL AND monsters_id > 100 AND monsters_id < 300");
$count = mysql_result($sql,0);
$sql = mysql_query("SELECT monsters_id FROM present WHERE player = $player AND items_id IS NULL AND monsters_id > 100 AND monsters_id < 300 ORDER BY monsters_id");
for ($i = 0; $i < $count; $i+= 1) {
$sql = mysql_query("SELECT monsters_id FROM present WHERE player = $player AND items_id IS NULL AND monsters_id > 100 AND monsters_id < 300 ORDER BY monsters_id");
$result = mysql_result($sql,$i);
if (mysql_result(mysql_query("SELECT COUNT(*) FROM present WHERE player = $player AND items_id IS NULL AND monsters_id=$result AND quantity = 1"),0) == 1){
$final = -1;
$array[1] = $rooms_id - 1;
$array[2] = $rooms_id + 1;
$array[3] = $rooms_id - 100;
$array[4] = $rooms_id + 100;
$array[5] = $rooms_id - 200;
$array[6] = $rooms_id - 101;
$array[7] = $rooms_id - 99;
$array[8] = $rooms_id - 2;
$array[9] = $rooms_id + 200;
$array[10] = $rooms_id + 101;
$array[11] = $rooms_id + 99;
$array[12] = $rooms_id + 2;
if ($result < 200){
for ($j = 1; $j < 5; $j+=1){
$sql = mysql_query("SELECT COUNT(*) FROM present WHERE player = $player AND items_id IS NULL AND monsters_id IS NOT NULL AND rooms_id={$array[$j]}");
$count = mysql_result($sql,0);
$sql = mysql_query("SELECT COUNT(*) FROM rooms WHERE (rooms_id={$array[$j]})");
$roompresent = mysql_result($sql,0);
if ($count == 0 and $final == -1 and $roompresent == 1){
$final = $array[$j];
}
}
} else if ($result > 200){
for ($k = 1; $k < 13; $k+=1){
$sql = mysql_query("SELECT COUNT(*) FROM present WHERE (player = $player AND items_id IS NULL AND monsters_id IS NOT NULL AND rooms_id={$array[$k]})");
$count = mysql_result($sql,0);
$sql = mysql_query("SELECT COUNT(*) FROM rooms WHERE (rooms_id={$array[$k]})");
$roompresent = mysql_result($sql,0);
if ($count == 0 and $final == -1 and $roompresent == 1){ $final = $array[$k]; }
}
if ($final == -1){
$sql = mysql_query("SELECT COUNT(*) FROM rooms");
$count = mysql_result($sql,0);
$sql = mysql_query("SELECT rooms_id FROM rooms");
for ($l = 0; $l < $count; $l+=1){
$rooms = mysql_query($sql,$l);
$sql = mysql_query("SELECT COUNT(*) FROM present WHERE player = $player AND items_id IS NULL AND monsters_id IS NOT NULL AND rooms_id=$rooms");
if (mysql_result($sql,0) == 0 and $final == -1){ $final = $rooms; }
}
}
}
if ($final != -1){ $sql = mysql_query("UPDATE present SET rooms_id=$final WHERE player = $player AND items_id IS NULL AND monsters_id=$result;"); }
else { $sql = mysql_query("UPDATE present SET rooms_id=-1 WHERE player = $player AND items_id IS NULL AND monsters_id=$result");}
}
}
$sql = mysql_query("UPDATE present SET state = 1 WHERE player = $player AND items_id IS NULL AND monsters_id IS NULL AND quantity IS NULL AND rooms_id IS NOT NULL");
} else {
$sql = mysql_query("UPDATE present SET state = 0 WHERE player = $player AND items_id IS NULL AND monsters_id IS NULL AND quantity IS NULL AND rooms_id IS NOT NULL");
}
}
虽然$ count为2,但for循环只运行一次。每次for循环运行时,我都通过在mysql表中的值中加1来验证这一点。该值仅增加一次。
顶部的if语句是每隔一个时钟运行该函数。
答案 0 :(得分:2)
for循环的条件部分在循环的每次迭代中进行计算,因此您无法在主循环内更改$count
的值。您需要将变量名称更改为更有意义的名称。
试试这个:
// successful sql login & $player is correctly set with '', and all below sql
// statements execute correctly. $count is 2
function setMonstersLocation($player,$rooms_id){
if (mysql_result(mysql_query("SELECT state FROM present WHERE player = $player AND items_id IS NULL AND monsters_id IS NULL AND quantity IS NULL AND rooms_id IS NOT NULL"),0) == 0){
$sql = mysql_query("SELECT COUNT(*) FROM present WHERE player = $player AND items_id IS NULL AND monsters_id > 100 AND monsters_id < 300");
// Note here I've changed $count to $playerCount, because it is a count of the
// number of players.
$playerCount = mysql_result($sql,0);
$sql = mysql_query("SELECT monsters_id FROM present WHERE player = $player AND items_id IS NULL AND monsters_id > 100 AND monsters_id < 300 ORDER BY monsters_id");
for ($i = 0; $i < $playerCount; $i+= 1) {
$sql = mysql_query("SELECT monsters_id FROM present WHERE player = $player AND items_id IS NULL AND monsters_id > 100 AND monsters_id < 300 ORDER BY monsters_id");
$result = mysql_result($sql,$i);
if (mysql_result(mysql_query("SELECT COUNT(*) FROM present WHERE player = $player AND items_id IS NULL AND monsters_id=$result AND quantity = 1"),0) == 1){
$final = -1;
$array[1] = $rooms_id - 1;
$array[2] = $rooms_id + 1;
$array[3] = $rooms_id - 100;
$array[4] = $rooms_id + 100;
$array[5] = $rooms_id - 200;
$array[6] = $rooms_id - 101;
$array[7] = $rooms_id - 99;
$array[8] = $rooms_id - 2;
$array[9] = $rooms_id + 200;
$array[10] = $rooms_id + 101;
$array[11] = $rooms_id + 99;
$array[12] = $rooms_id + 2;
if ($result < 200){
for ($j = 1; $j < 5; $j+=1){
$sql = mysql_query("SELECT COUNT(*) FROM present WHERE player = $player AND items_id IS NULL AND monsters_id IS NOT NULL AND rooms_id={$array[$j]}");
// Note here I've change $count to $presentCount, because you're
// counting the number of presents? (Whatever presents are)
$presentCount = mysql_result($sql,0);
$sql = mysql_query("SELECT COUNT(*) FROM rooms WHERE (rooms_id={$array[$j]})");
$roompresent = mysql_result($sql,0);
if ($presentCount == 0 and $final == -1 and $roompresent == 1){
$final = $array[$j];
}
}
} else if ($result > 200){
for ($k = 1; $k < 13; $k+=1){
$sql = mysql_query("SELECT COUNT(*) FROM present WHERE (player = $player AND items_id IS NULL AND monsters_id IS NOT NULL AND rooms_id={$array[$k]})");
// You can use $presentCount again here, because it's a
// different code block to the previous one.
// This doesn't mean its in a different SCOPE, however.
$presentCount = mysql_result($sql,0);
$sql = mysql_query("SELECT COUNT(*) FROM rooms WHERE (rooms_id={$array[$k]})");
$roompresent = mysql_result($sql,0);
if ($presentCount == 0 and $final == -1 and $roompresent == 1){ $final = $array[$k]; }
}
if ($final == -1){
$sql = mysql_query("SELECT COUNT(*) FROM rooms");
// Note here I've change $count to $roomsCount, because you're
// counting the number of rooms.
$roomsCount = mysql_result($sql,0);
$sql = mysql_query("SELECT rooms_id FROM rooms");
for ($l = 0; $l < $roomsCount; $l+=1){
$rooms = mysql_query($sql,$l);
$sql = mysql_query("SELECT COUNT(*) FROM present WHERE player = $player AND items_id IS NULL AND monsters_id IS NOT NULL AND rooms_id=$rooms");
if (mysql_result($sql,0) == 0 and $final == -1){ $final = $rooms; }
}
}
}
if ($final != -1){ $sql = mysql_query("UPDATE present SET rooms_id=$final WHERE player = $player AND items_id IS NULL AND monsters_id=$result;"); }
else { $sql = mysql_query("UPDATE present SET rooms_id=-1 WHERE player = $player AND items_id IS NULL AND monsters_id=$result");}
}
}
$sql = mysql_query("UPDATE present SET state = 1 WHERE player = $player AND items_id IS NULL AND monsters_id IS NULL AND quantity IS NULL AND rooms_id IS NOT NULL");
} else {
$sql = mysql_query("UPDATE present SET state = 0 WHERE player = $player AND items_id IS NULL AND monsters_id IS NULL AND quantity IS NULL AND rooms_id IS NOT NULL");
}