[英]How do I detect the last iteration in while loop
I tried doing this first: 我首先尝试这样做:
$e = 0;
$objectsid = mysql_query("SELECT * FROM xw_char_items WHERE CharId = '$charid' AND ItemCat = 'object' ORDER BY SortOrder ASC");
while($obj = mysql_fetch_array($objectsid)) {
$e++;
if($e==9) break;
$objectsinfo = mysql_query("SELECT * FROM xw_objects WHERE ItemId = '{$obj["ItemId"]}'");
$object = mysql_fetch_array($objectsinfo);
echo "&charid$e={$char["Id"]}";
if($objectsid == end($obj)) {
echo "&intActiveObject=1";
echo "&intObjectsNum=$e";
}
}
Here it never detects the last one. 在这里它永远不会检测到最后一个。 I also tried this: 我也试过这个:
$e = 0;
$len = count($objectsid));
while($obj = mysql_fetch_array($objectsid)) {
$e++;
if($e==9) break;
$objectsinfo = mysql_query("SELECT * FROM xw_objects WHERE ItemId = '{$obj["ItemId"]}'");
$object = mysql_fetch_array($objectsinfo);
mysql_free_result($objectsinfo);
echo "&charid$e={$char["Id"]}";
if ($e == $len - 1) {
echo "&intActiveObject=1";
echo "&intObjectsNum=$e";
}
}
Here it detects every iteration as the last one. 在这里,它将每次迭代检测为最后一次迭代。
Does anyone how to make it detect the last iteration? 有谁能使它检测到最后一次迭代?
The $objectsid
variable is storing the mysql_result
object not the array of your fetched rows. $objectsid
变量存储的是mysql_result
对象,而不是所获取的行的数组。 So even if you apply count($objectsid)
, you will not get the length of your fetched array. 因此,即使您应用count($objectsid)
,也不会获得所获取数组的长度。
So the right method is to use mysql_num_rows()
. 因此正确的方法是使用mysql_num_rows()
。
So Simply do this $len=mysql_num_rows($objectsid);
所以只需这样做$len=mysql_num_rows($objectsid);
and the rest will workout as you are expecting. 其余的将按照您的期望进行锻炼。 :) :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.