Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8043429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T04:53:53+00:00 2026-06-05T04:53:53+00:00

In the code below I want to apply the object $activeclass as a DIV

  • 0

In the code below I want to apply the object “$activeclass” as a DIV class. I thought the end pointer I included would apply this only to the last iteration of the array, but instead it is applying the class to all iterations.

<div id="right_bottom">
                <?
                $content = is_array($pagedata->content) ? $pagedata->content : array($pagedata->content);
                foreach($content as $item){
                $activeclass = end($content) ? 'active' : ' ';
                    ?>
                    <div id="right_side">
                           <div id="<?=$item->id?>" class="side_items <?=$activeclass?>">
                             <a class="content" href="<?=$item->id?>"><img src="<?=PROTOCOL?>//<?=DOMAIN?>/img/content/<?=$item->image?>"><br />
                             <strong><?=$item->title?></strong></a><br />
                             <h2><?=date('F j, Y',  strtotime($item->published))?></h2><br />
                         </div>
                    </div>
                    <?
                }
                ?>
</div>

Any ideas where I am making a mistake? How can I apply the class $activeclass only to the last iteration of my “foreach” statement?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T04:53:55+00:00Added an answer on June 5, 2026 at 4:53 am

    The simplest way is to keep a count:

    $i = 0; $size = count( $content);
    foreach( $content as $item) {
        $i++;
        $activeclass = ( $i < $size) ? '' : 'active';
    }
    

    Alternatively, you can compare the last element with the current element (if your array is consecutively numerically indexed starting with 0 [Thanks to webbiedave for pointing out the assumptions made by this method]):

    $last = count( $content) - 1;
    foreach( $content as $item) {
        $activeclass = ( $content[$last] === $item) ? 'active' : '';
    }
    

    Note that this approach will not work if your array has duplicate items.

    Finally, you can compare indexes in the following way:

    // Numerical or associative
    $keys = array_keys($content); 
    $key = array_pop($keys); // Assigned to variables thanks to webbiedave
    
    // Consecutive numerically indexed
    $key = count( $content) - 1; 
    
    foreach( $content as $current_key => $item) {
        $activeclass = ( $current_key === $key) ? 'active' : '';
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

See code below: I want Page 1 to alert #testing... This works in C
I have the code below and I want to loop this every 5 seconds.
Using the code below, I want to keep the same menu div opened in
Consider the code below. I do not want to create multiple instances of class
With the following code below I want to be able to apply a CSS
This is my code below and I want to make the value in the
I want to convert function object to function. I wrote this code, but it
For the below code: <div id=main> <div class=one></div> <div class=two></div> <div class=three></div> </div> I
In the PHP code below i want to select all the column values of
The code below does not work but meant to illustrate what I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.