• I have this code that count the amount of user with certain roles and displays the amount in a list, which works fine.
    But now I want to add a last class to the last list item, which doesn’t work.

    This is the code I have sofar:

    $result = count_users();
    		$show = array('trainer', 'sportvereniging', 'sportschool');
    		foreach($result['avail_roles'] as $role => $count){
    				if( in_array($role, $show)) {
    					if ($role == (count($result) - 1)) {
    						echo 'last';
    					}
    					echo '<li><span class="count-num">' . $count . '</span><br><span class="count-group">' . $role, 's</span></li>';
    }
    }
    echo '</ul>';

Viewing 3 replies - 1 through 3 (of 3 total)
  • Since you can do this with CSS as well.

    For example
    If you have below list

    <ul class="ul_class">
            <li><a title="" href=""></a></li>
            <li><a title="" href=""></a></li>
            <li><a title="" href=""></a></li>
            <li><a title="" href=""></a></li>
    </ul>

    So class can be added like this.

    .ul_class:last-of-type {
        //Set your style over here.
    }
    Thread Starter udarts

    (@udarts)

    Thanks, I already knew that, but though that browser compatibility is causing an issue with css3. That’s why I was looking for a solution with a class.

    Suggest that you count the number of results outside the loop. Each time through the loop count down by one, if there are none left then you are at the last one.

    Your code as posted both expects role to be a text match in your array, while also being a number, this does not make sense.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Getting last role when counting user with certain roles’ is closed to new replies.