Giving something a 'Last' Class

If you want to apply a different class to the last item in a list generated by PHP, start off with Setting the $lastOne variable to the end of your list

<?php
    $lastOne = end($your_array);

This stores the details of the last one in your list into the lastOne variable - we'll compare later.

You may need to modify your existing while/foreach loop, adding in the $i variable to your item. If i is already used, then pick any other letter.

<?php
    foreach($your_array as $i => $item){

Then on the list item or table row that you want to apply the last class to, compare your $lastOne with the current one your are looping through.

<div class="box <?php echo ($lastOne == $item ? '  last' : '' ?>>
Mike Street

Written by Mike Street

Mike is a front-end developer from Brighton, UK. He spends his time writing, cycling and coding. You can find Mike on Twitter.