birdd
May 5, 2006, 11:29am
1
I would like to know if it is possible to during a loop be able to tell if the current record it is working on is the last record?
while ($row = mysql_fetch_array($result)) {
echo "<li><a href='".$row['link']."'>".$row['title']."</a></li>";
}
What I want is on the last record that it will do this:
echo "<li class='last'><a href='".$row['link']."'>".$row['title']."</a></li>";
Is it possible to do that? Or do i need to loop (or just move to the last record) before i do that loop that echo’s the data to find the last record first?
You could use the mysql_num_rows function to ascertain the number of rows in the result. Then in your loop run a counter.
if($counter == mysql_num_rows($result)){
echo "..";
}else{
echo "..";
}
If you start the counter at 0, remember to increment the counter before the if statement.
Not sure if this is the best way but should work.
if your id-field is auto_increment you could use [url=“http://de3.php.net/manual/en/function.mysql-insert-id.php ”]mysql_insert_id()
, eg
if ($row['id'] = mysql_insert_id())
{
echo "<li class='last'><a href='".$row['link']."'>".$row['title']."</a></li>";
}
else
{
echo "<li><a href='".$row['link']."'>".$row['title']."</a></li>";
}
But that only would make sense, if the last entry is even the last entry inserted, eg you’re not looking for a specific result set.