1

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?

1 Answers1

-1

The $objectsid variable is storing the mysql_result object not the array of your fetched rows. So even if you apply count($objectsid), you will not get the length of your fetched array.

So the right method is to use mysql_num_rows().

So Simply do this $len=mysql_num_rows($objectsid); and the rest will workout as you are expecting. :)

The SuperKat
  • 234
  • 2
  • 10
  • Please, read this first: http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php – Michas Oct 25 '16 at 01:00
  • I know, but the asker is coding with mysql_* so I had to give him solution in that domain :/. I know he/she shouldn't use it, but still he/she should know about those functions. He/she will eventually find out PDO or mysqli_ – The SuperKat Oct 25 '16 at 05:01