Forums

get the last post in my for each (3 posts)

  1. poliquinp
    Member
    Posted 6 months ago #

    His there any way I could know when I reach the last element into my foreach?

    foreach($posts as $post)
    {
    // Show some posts

    // If it is the last post in the foreach
    if(LAST) {Show something special}
    }

  2. vtxyzzy
    Member
    Posted 6 months ago #

    Try something like this:

    $last = count($posts);
    foreach($posts as $post)
    {
    // Show some posts
    
    // If it is the last post in the foreach
    if(++$post_count == $last) {Show something special}
    }
  3. jasf87
    Member
    Posted 4 months ago #

    Thank you. It worked well for me.

Reply

You must log in to post.

About this Topic